prelude-ls icon indicating copy to clipboard operation
prelude-ls copied to clipboard

added fold-obj-to-list

Open homam opened this issue 9 years ago • 2 comments

fold-obj-to-list can save an extra map:

array
    |> group-by key
    |> obj-to-pairs
    |> map ([k, vs]) -> merge k, vs
array
    |> group-by key
    |> fold-obj-to-list (k, vs) -> merge k, vs

It's defined like:

# (a -> b -> c) -> {a: b} -> [c]
fold-obj-to-list = (f, object) -->
  [f key, value for key, value of object]

homam avatar Oct 25 '15 10:10 homam

I'm increasingly worried about "because it saves an extra loop" as a reason to add a new utility function. Should we also have fold-obj-to-list-then-filter because it saves an extra filter? Arbitrary amounts of loop fusion could be done this way (and many like concat-map and partition already exist), but as you demonstrate, they are logically redundant with existing operations.

That said, I've often wanted to fold an object into a list. Consider this a slightly reluctant +1.

Your comment's code blocks call it fold-obj-to-pairs. Those should be fold-obj-to-list right?

anko avatar Oct 25 '15 11:10 anko

(group-by f) >> obj-to-pairs >> (map ([k, v]) -> merge k, v) is a very common pattern when dealing with tabular data like the result of SQL queries. I wish LiveScript had a compiler that supported fusion but ...

I think there's a need for an abstract obj-to-pairs: obj-to-pairs = fold-obj-to-list (a, b) -> [a, b]

homam avatar Oct 25 '15 11:10 homam