0

What is the rule behind the following behavior:

user=> (take-while #(> 10 %) [2 9 4 12 3 99])
(2 9 4)
user=> (take-while #(> % 10) [2 9 4 12 3 99])
()

Thanks!

0

2 Answers 2

2

There is a pretty big difference between "Is 10 bigger than this number?" and "Is this number bigger than 10?". Those are, respectively, the two predicates you have written, and so of course take-while behaves differently when given each predicate.

Sign up to request clarification or add additional context in comments.

2 Comments

why the second call does not return (12 99)?
Why should it? take-while is not filter
1

Because take-while stops at the first false

(take-while #(> % 10) [2 9 4 12 3 99])

so the very first check is false it then stops and gives you an empty list. This isnt really made clear in https://clojuredocs.org/clojure.core/take-while but its implied for example take

(take-while #(> % 10) [ 11 15 2 9 4 12 3 99])

will output

(11 15)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.