Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure sequence operation

Tags:

clojure

How to get a new sequence from an old sequence, the elements of the new one are from the old one until a condition is met

Suppose the condition is #(> % 0)

'(1 2 3 0 3 2 0 1) returns 1, 2, 3

'(0 1 2 3) returns empty seq

'(1 2 3) returns everything.

Note it's not same as filter.

like image 693
kakarukeys Avatar asked Mar 14 '26 04:03

kakarukeys


1 Answers

You probably want to use take-while:

(take-while #(> % 0) '(1 2 3 0 3 2 0 1))
=> (1 2 3)
like image 79
mikera Avatar answered Mar 17 '26 00:03

mikera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!