File tree Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Expand file tree Collapse file tree 2 files changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -8858,15 +8858,17 @@ reduces them without incurring seq initialization"
8858
8858
" Returns a lazy seq of the intermediate values of the reduction (as
8859
8859
per reduce) of coll by f, starting with init."
8860
8860
([f coll]
8861
- (lazy-seq
8862
- (if-let [s (seq coll)]
8863
- (reductions f (first s) (rest s))
8864
- (list (f )))))
8861
+ (lazy-seq
8862
+ (if-let [s (seq coll)]
8863
+ (reductions f (first s) (rest s))
8864
+ (list (f )))))
8865
8865
([f init coll]
8866
+ (if (reduced? init)
8867
+ (list @init)
8866
8868
(cons init
8867
- (lazy-seq
8868
- (when-let [s (seq coll)]
8869
- (reductions f (f init (first s)) (rest s)))))))
8869
+ (lazy-seq
8870
+ (when-let [s (seq coll)]
8871
+ (reductions f (f init (first s)) (rest s) )))))))
8870
8872
8871
8873
(defn juxt
8872
8874
" Takes a set of functions and returns a fn that is the juxtaposition
Original file line number Diff line number Diff line change 3150
3150
(is (= [" " ] (s/split-lines " " )))
3151
3151
(is (= [] (s/split-lines " \n\n\n " ))))
3152
3152
3153
+ (deftest test-reductions-obeys-reduced
3154
+ (is (= [0 :x ]
3155
+ (reductions (constantly (reduced :x ))
3156
+ (range ))))
3157
+ (is (= [:x ]
3158
+ (reductions (fn [acc x] x)
3159
+ (reduced :x )
3160
+ (range ))))
3161
+ (is (= [2 6 12 12 ]
3162
+ (reductions (fn [acc x]
3163
+ (if (= x :stop )
3164
+ (reduced acc)
3165
+ (+ acc x)))
3166
+ [2 4 6 :stop 8 10 ]))))
3167
+
3153
3168
(comment
3154
3169
; ; ObjMap
3155
3170
; ; (let [ks (map (partial str "foo") (range 500))
You can’t perform that action at this time.
0 commit comments