Skip to content

Commit d1abb8a

Browse files
authored
Fix keep and keep-indexed two-arity forms (#962) (#964)
Fix for #962
1 parent 5eb4170 commit d1abb8a

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
### Fixed
1212
* Fixed an issue where attempting to run a namespace from the CLI could fail in certain cases (#957)
13+
* Fixed an issue with `keep` and `keep-indexed` two-arity forms not preserving the transformed values (#962)
1314

1415
## [v0.1.0]
1516
### Added

src/basilisp/core.lpy

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,10 +2861,10 @@
28612861
([f coll]
28622862
(lazy-seq
28632863
(when (seq coll)
2864-
(let [elem (first coll)]
2865-
(if-not (nil? (f elem))
2866-
(cons elem (keep f (rest coll)))
2867-
(keep f (rest coll))))))))
2864+
(let [elem (f (first coll))]
2865+
(if (nil? elem)
2866+
(keep f (rest coll))
2867+
(cons elem (keep f (rest coll)))))))))
28682868

28692869
(defn keep-indexed
28702870
"Return a lazy-sequence of non- ``nil`` results of ``(f index elem)`` for elements
@@ -2887,11 +2887,10 @@
28872887
[rng coll]
28882888
(lazy-seq
28892889
(when (seq coll)
2890-
(let [idx (first rng)
2891-
elem (first coll)]
2892-
(if-not (nil? (f idx elem))
2893-
(cons elem (keep-idx (rest rng) (rest coll)))
2894-
(keep-idx (rest rng) (rest coll)))))))]
2890+
(let [elem (f (first rng) (first coll))]
2891+
(if (nil? elem)
2892+
(keep-idx (rest rng) (rest coll))
2893+
(cons elem (keep-idx (rest rng) (rest coll))))))))]
28952894
(keep-idx (range) coll))))
28962895

28972896
(defn take

tests/basilisp/test_core_fns.lpy

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -899,40 +899,54 @@
899899
'(2 4 6) odd? [1 2 3 4 5 6])))
900900

901901
(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))))))
917925

918926
(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"
921929
(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]))
927935

928-
(testing "higher order function"
929-
(let [f (fn [i v] v)]
936+
(testing "higher order function"
930937
(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))))))
936950

937951
(deftest take-test
938952
(testing "transducer"

0 commit comments

Comments
 (0)