weavejester / medley

A lightweight library of useful Clojure functions
Eclipse Public License 1.0
865 stars 66 forks source link

Suggested addition: removev and keepv #94

Open velios opened 1 week ago

velios commented 1 week ago

Hello. First of all, thank you very much for the great library.

Follow the description useful, mostly pure functions that are "missing" from clojure.core. Among the functions that I feel so is removev and keepv. At some point I came across Clojure's deadly sin article about lazy calculations. And since then I prefer to write flows in most cases in eager manner like this, instead of using "default" lazy with map approach:

(->> (repeat 1000 10)
      (mapv inc)
      (mapv inc)
      (mapv #(* % 2))
      (mapv inc)
      (mapv inc))

We also have a function in the standard library that continues this logic. filterv But for some reason unknown to me, there are no functions removev and keepv, which, together with mapv and filter, are often used in thread last macro(->>)

Also, in my practice, I work mostly with small data sequenes, not infinite. Besides being faster it is also more logical when using debuggers such as Flowstorm's stepper.

True, we can use transducers instead and it will even be faster. But this approach is more unusual, especially when you work in a team.

(into []
       (comp (map inc)
             (map inc)
             (map #(* % 2))
             (map inc)
             (map inc))
       (repeat 1000 10))

Example of possible usage:

(->> [1 2 3 4 5 6 11 12 13 14]
      (mapv inc)
      (filterv odd?)
      (medley/removev #(> % 10))
      (medley/keepv #(when (odd? %) %)))
;; => [3 5 7]

Let me know if this is an acceptable proposal, but you do not have time to implement it and I will try to prepare a PR.