Created
August 30, 2016 18:48
-
-
Save bturnbull/f2bcb733d92a667e85eee17100c11c21 to your computer and use it in GitHub Desktop.
Elixir Stream.transform Accumulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simplified representation of source data | |
data = ["A", "B", "B", "A", "C", "A", "D"] | |
Stream.transform(data, "", fn(line, acc) -> | |
if line == "A" do | |
if acc == "", do: {[], line}, else: {[acc], line} | |
else | |
{[], acc <> line} | |
end | |
end) |> Enum.to_list | |
# Desired: ["ABB", "AC", "AD"] | |
# Result: ["ABB", "AC"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment