take-while
stops after the first failing test. Since the first element fails the test (it isn't greater than five), it never gets to the last one. If you want all elements that pass the test regardless of where they appear in the sequence, use filter
instead.
Anonymous function with take-while not working?
-
23-09-2022 - |
Question
A simple example should demonstrate my problem:
First I define a simple variable:
(def a '(["one" 1] ["two" 2] ["nine" 9]))
;; CASE 1: (This works correctly)
(take-while #(< (second %) 5) a)
Returns: (["one" 1] ["two" 2])
;; CASE 2: (This does not seem to work correctly)
;; The only difference is the '>' instead of '<'
(take-while #(> (second %) 5) a)
Returns: ()
It seems to me that CASE 2 should return (["nine" 9]) ?
Is there a way to debug this to see what I'm missing?
Thanks!
Solution
OTHER TIPS
The order of arguments to >
is important, try
(take-while #(> 5 (second %)) a)
;=> (["one" 1] ["two" 2])
(drop-while #(> 5 (second %)) a)
;=> (["nine" 9])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow