|
28 | 28 | (t/assert= (keep-indexed (constantly nil) []) []) |
29 | 29 | (t/assert= (keep-indexed (fn [i x] [i x]) [:a :b]) [[0 :a] [1 :b]]) |
30 | 30 |
|
31 | | - (t/assert= (transduce (keep-indexed (constantly true)) conj []) []) |
32 | | - (t/assert= (transduce (keep-indexed (constantly nil)) conj []) []) |
33 | | - (t/assert= (transduce (keep-indexed (fn [i x] [i x])) conj [:a :b]) [[0 :a] [1 :b]])) |
| 31 | + (t/assert= (transduce (keep-indexed (constantly true)) conj [:a :b]) [true true]) |
| 32 | + (t/assert= (transduce (keep-indexed (constantly nil)) conj [:a :b]) []) |
| 33 | + (t/assert= (transduce (keep-indexed (fn [i x] [i x])) conj [:a :b]) [[0 :a] [1 :b]]) |
| 34 | + |
| 35 | + (let [even-index? (fn [i x] |
| 36 | + (when (even? i) |
| 37 | + x))] |
| 38 | + (t/assert= (transduce (keep-indexed even-index?) conj [:a :b :c :d]) |
| 39 | + [:a :c]) |
| 40 | + (t/assert= (transduce (map-indexed even-index?) conj [:a :b :c :d]) |
| 41 | + [:a nil :c nil]) |
| 42 | + |
| 43 | + (t/assert= (transduce (comp (keep-indexed even-index?) |
| 44 | + (take 3)) |
| 45 | + conj |
| 46 | + [:a :b :c :d :e :f]) |
| 47 | + [:a :c :e]) |
| 48 | + (t/assert= (transduce (comp (map-indexed even-index?) |
| 49 | + (take 3)) |
| 50 | + conj |
| 51 | + [:a :b :c :d]) |
| 52 | + [:a nil :c]))) |
34 | 53 |
|
35 | 54 | (t/deftest test-reductions |
36 | 55 | (t/assert= (reductions + nil) |
|
542 | 561 | (t/assert= (transduce (take 0) conj [1 2 3 4]) []) |
543 | 562 | (t/assert= (transduce (take 1) conj [1 2 3 4]) [1]) |
544 | 563 | (t/assert= (transduce (take 2) conj [1 2 3 4]) [1 2]) |
545 | | - (t/assert= (transduce (take 3) conj [1 2 3 4]) [1 2 3])) |
| 564 | + (t/assert= (transduce (take 3) conj [1 2 3 4]) [1 2 3]) |
| 565 | + (t/assert= (transduce (take 10) conj [1 2 3 4]) [1 2 3 4]) |
| 566 | + (t/assert= (transduce (comp (take 2) (take 1)) conj [1 2 3 4]) [1]) |
| 567 | + (t/assert= (transduce (comp (take 1) (take 2)) conj [1 2 3 4]) [1]) |
| 568 | + |
| 569 | + (let [call-count (atom 0) |
| 570 | + inc-call-count! (fn [x] |
| 571 | + (swap! call-count inc) |
| 572 | + x)] |
| 573 | + (t/assert= (transduce (comp (map inc-call-count!) (take 2)) conj (range 10)) |
| 574 | + [0 1]) |
| 575 | + (t/assert= @call-count 2) |
| 576 | + (t/assert= (transduce (comp (take 2) (map inc-call-count!)) conj (range 10)) |
| 577 | + [0 1]) |
| 578 | + (t/assert= @call-count 4))) |
546 | 579 |
|
547 | 580 | (t/deftest test-drop |
548 | 581 | (t/assert= (drop 0 [1 2 3 4]) [1 2 3 4]) |
|
552 | 585 | (t/assert= (transduce (drop 0) conj [1 2 3 4]) [1 2 3 4]) |
553 | 586 | (t/assert= (transduce (drop 1) conj [1 2 3 4]) [2 3 4]) |
554 | 587 | (t/assert= (transduce (drop 2) conj [1 2 3 4]) [3 4]) |
555 | | - (t/assert= (transduce (drop 3) conj [1 2 3 4]) [4])) |
| 588 | + (t/assert= (transduce (drop 3) conj [1 2 3 4]) [4]) |
| 589 | + (t/assert= (transduce (drop 10) conj [1 2 3 4]) []) |
| 590 | + (t/assert= (transduce (comp (drop 1) (take 2)) conj [1 2 3 4]) [2 3]) |
| 591 | + (t/assert= (transduce (comp (take 2) (drop 1)) conj [1 2 3 4]) [2])) |
556 | 592 |
|
557 | 593 | (t/deftest test-take-while |
558 | 594 | (t/assert= (take-while pos? [1 2 3 -1]) [1 2 3]) |
559 | 595 | (t/assert= (take-while pos? [-1 2]) ()) |
560 | 596 | (t/assert= (transduce (take-while even?) conj [2 4 6 7 8]) [2 4 6]) |
561 | 597 | (t/assert= (transduce (take-while even?) conj [0 2] [1 4 6]) [0 2]) |
562 | | - (t/assert= (transduce (take-while even?) conj [1 3] [2 4 6 7 8]) [1 3 2 4 6])) |
| 598 | + (t/assert= (transduce (take-while even?) conj [1 3] [2 4 6 7 8]) [1 3 2 4 6]) |
| 599 | + (t/assert= (transduce (comp (take-while even?) (take 2)) conj [1 3] [2 4 6 7 8]) [1 3 2 4])) |
563 | 600 |
|
564 | 601 | (t/deftest test-drop-while |
565 | 602 | (t/assert= (drop-while pos? [1 2 3 -1]) [-1]) |
566 | 603 | (t/assert= (drop-while pos? [-1 2]) [-1 2]) |
567 | 604 | (t/assert= (transduce (drop-while even?) conj [2 4 6 7 8]) [7 8]) |
568 | 605 | (t/assert= (transduce (drop-while even?) conj [0 2] [1 4 6]) [0 2 1 4 6]) |
569 | | - (t/assert= (transduce (drop-while even?) conj [0 2] [2 4 6 7 8]) [0 2 7 8])) |
| 606 | + (t/assert= (transduce (drop-while even?) conj [0 2] [2 4 6 7 8]) [0 2 7 8]) |
| 607 | + (t/assert= (transduce (comp (drop-while even?) (take 2)) conj [0 2] [2 4 6 7 8]) [0 2 7 8])) |
570 | 608 |
|
571 | 609 | (t/deftest test-cycle |
572 | 610 | (t/assert= (cycle ()) ()) |
|
0 commit comments