Expression icon indicating copy to clipboard operation
Expression copied to clipboard

Alias `choose` on `Seq` as `flatMap` (or `flat_map` or `flatmap`)

Open vreuter opened this issue 1 year ago • 5 comments

IMO, "choose" sounds more like "filter", and I'd expect the argument to be a function like Callable[[A], bool]when dealing with a Seq[A], which then gives back the elements which satisfied the predicate. Or even some sort of randomised element selector. But what's really happening is each element in the collection is being mapped to an optional according to the passed function, and then the result is "flattened" back by collapsing down those wrapped structures to the original values, while discarding the empty values. One of the "flat map" variants thus feels more intuitive as a name, at least having it as an alias for the choose member of Seq. This would also match this pair of naming and behavior from e.g. Scala collections.

vreuter avatar Nov 22 '24 15:11 vreuter

Hi. The operator you are looking for is called collect.

dbrattli avatar Jan 08 '25 19:01 dbrattli

Hi @dbrattli , I don't see it in the API docs for Seq; where is it defined?

vreuter avatar Jan 09 '25 00:01 vreuter

Looks like the documentation is not being updated. I'll look into it this weekend.

dbrattli avatar Jan 10 '25 06:01 dbrattli

Hey @dbrattli, I’ve been thinking about this, and I believe we should add an alias function called flat_map. It feels more natural and intuitive. What do you think?

neel-suthar avatar Mar 09 '25 04:03 neel-suthar

There is so many ways to name this function collect, select_many, bind, merge_map, map_merge, flat_map, ´concat_map. The more intuitive name would be map_merge` since it flattens after the transformation (map). But anyways, Expression tries to be consistent in the naming with F#, ref: https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-seqmodule.html#collect

dbrattli avatar Mar 09 '25 07:03 dbrattli