Replies: 1 comment 4 replies
-
The above doesn't handle the case where Here's my suggestion: (defn partition-by
[f coll]
(def res @[])
(var prev nil)
(loop [e :in coll
:let [curr (f e)]]
(cond
(zero? (length res))
(array/push res @[e])
(= curr prev)
(array/push (last res) e)
(array/push res @[e]))
(set prev curr))
res) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I see that Janet already has
partition
, but I wanted to be able to step through an array and create sub-arrays of it whenever the result of some(f e)
(callingf
on an elemente
of the array) changed.I initially thought
f
should be a function of two args --- the curr and next array elems --- but it looks simpler if I just check to see if(f e)
changes.Here's what I came up with:
How does that look? Suggestions for improvement appreciated.
Beta Was this translation helpful? Give feedback.
All reactions