Reputation: 177
I need to filter a map, but the filter should compare the value with the previous one and the filtered map should have just values that has diferent signal of the previous.
Here is a example:
Map to filter: {:key1 100 :key2 10 :key3 -20 :key4 -10 :key5 10}
Return: {:key3 -20 :key5 10}
Any sugestion of how can I do this?
Upvotes: 0
Views: 1585
Reputation: 7223
I might use partition-by which chunks up a sequence based on when the sequence changes value. We can tell partition-by
to look at the "signum" of the values, and then only take the first result in each chunk.
(->> {:key1 100 :key2 10 :key3 -20 :key4 -10 :key5 10}
(partition-by #(Integer/signum (val %)))
rest
(map first))
=> ([:key3 -20] [:key5 10])
Upvotes: 2