Enums in Elixir are represented using abbreviations. We can display any structure while you tell us how to reduce it.
The whole idea of ββStream is that you can create those reduction functions. Take the map as an example:
def map(enumerable, f) do Lazy[enumerable: enumerable, fun: fn(f1) -> fn(entry, acc) -> f1.(f.(entry), acc) end end] end
You get an enumerated value, and you want to match each element using the f function. The tape version receives the actual reduction function f1 and returns a new function that receives entry and acc (the same arguments f1 will receive) and then calls f.(entry) , effectively matching the element before calling f1 (reduction function). Notice how we match the elements one at a time.
A flat map version of this is likely to be something like this:
def flat_map(enumerable, f) do Lazy[enumerable: enumerable, fun: fn(f1) -> fn(entry, acc) -> Enumerable.reduce(f.(entry), acc, f1) end end] end
Now, every time you call f.(entry) , you get the list back and you want to iterate over each element of this new list, rather than iterating over the list as a whole.
I have not tried the code above (and maybe I missed some details), but that Streams work in general.
source share