|
899 | 899 | '(2 4 6) odd? [1 2 3 4 5 6])))
|
900 | 900 |
|
901 | 901 | (deftest keep-test
|
902 |
| - (testing "transducer" |
903 |
| - (are [res input] (= res (into [] (keep identity) input)) |
904 |
| - [] [] |
905 |
| - [:a :b :c] [:a :b :c] |
906 |
| - [:a :b :c] [:a :b nil :c] |
907 |
| - [:a :b :c] [:a :b nil nil :c] |
908 |
| - [:a :b :c :d] [:a :b nil :c nil nil :d])) |
909 |
| - |
910 |
| - (testing "higher order function" |
911 |
| - (are [res input] (= res (keep identity input)) |
912 |
| - '() [] |
913 |
| - '(:a :b :c) [:a :b :c] |
914 |
| - '(:a :b :c) [:a :b nil :c] |
915 |
| - '(:a :b :c) [:a :b nil nil :c] |
916 |
| - '(:a :b :c :d) [:a :b nil :c nil nil :d]))) |
| 902 | + (let [transform (fn [x] (when x (-> x name symbol)))] |
| 903 | + (testing "transducer" |
| 904 | + (are [res input] (= res (into [] (keep transform) input)) |
| 905 | + '[] [] |
| 906 | + '[a b c] [:a :b :c] |
| 907 | + '[a b c] [:a :b nil :c] |
| 908 | + '[a b c] [:a :b nil nil :c] |
| 909 | + '[a b c d] [:a :b nil :c nil nil :d])) |
| 910 | + |
| 911 | + (testing "higher order function" |
| 912 | + (are [res input] (= res (keep transform input)) |
| 913 | + '() [] |
| 914 | + '(a b c) [:a :b :c] |
| 915 | + '(a b c) [:a :b nil :c] |
| 916 | + '(a b c) [:a :b nil nil :c] |
| 917 | + '(a b c d) [:a :b nil :c nil nil :d])) |
| 918 | + |
| 919 | + (testing "lazy" |
| 920 | + (let [counter (atom 0) |
| 921 | + vals (keep (partial swap! counter +) (repeat 1 10))] |
| 922 | + (is (= 0 @counter)) |
| 923 | + (seq vals) |
| 924 | + (is (= 10 @counter)))))) |
917 | 925 |
|
918 | 926 | (deftest keep-indexed-test
|
919 |
| - (testing "transducer" |
920 |
| - (let [f (fn [i v] v)] |
| 927 | + (let [f (fn [i x] (when x [i (-> x name symbol)]))] |
| 928 | + (testing "transducer" |
921 | 929 | (are [res input] (= res (into [] (keep-indexed f) input))
|
922 |
| - [] [] |
923 |
| - [:a :b :c] [:a :b :c] |
924 |
| - [:a :b :c] [:a :b nil :c] |
925 |
| - [:a :b :c] [:a :b nil nil :c] |
926 |
| - [:a :b :c :d] [:a :b nil :c nil nil :d]))) |
| 930 | + '[] [] |
| 931 | + '[[0 a] [1 b] [2 c]] [:a :b :c] |
| 932 | + '[[0 a] [1 b] [3 c]] [:a :b nil :c] |
| 933 | + '[[0 a] [1 b] [4 c]] [:a :b nil nil :c] |
| 934 | + '[[0 a] [1 b] [3 c] [6 d]] [:a :b nil :c nil nil :d])) |
927 | 935 |
|
928 |
| - (testing "higher order function" |
929 |
| - (let [f (fn [i v] v)] |
| 936 | + (testing "higher order function" |
930 | 937 | (are [res input] (= res (keep-indexed f input))
|
931 |
| - '() [] |
932 |
| - '(:a :b :c) [:a :b :c] |
933 |
| - '(:a :b :c) [:a :b nil :c] |
934 |
| - '(:a :b :c) [:a :b nil nil :c] |
935 |
| - '(:a :b :c :d) [:a :b nil :c nil nil :d])))) |
| 938 | + '() [] |
| 939 | + '([0 a] [1 b] [2 c]) [:a :b :c] |
| 940 | + '([0 a] [1 b] [3 c]) [:a :b nil :c] |
| 941 | + '([0 a] [1 b] [4 c]) [:a :b nil nil :c] |
| 942 | + '([0 a] [1 b] [3 c] [6 d]) [:a :b nil :c nil nil :d])) |
| 943 | + |
| 944 | + (testing "lazy" |
| 945 | + (let [counter (atom 0) |
| 946 | + vals (keep-indexed (fn [i x] (swap! counter + x)) (repeat 1 10))] |
| 947 | + (is (= 0 @counter)) |
| 948 | + (seq vals) |
| 949 | + (is (= 10 @counter)))))) |
936 | 950 |
|
937 | 951 | (deftest take-test
|
938 | 952 | (testing "transducer"
|
|
0 commit comments