Skip to content

Instantly share code, notes, and snippets.

@bturnbull
Created August 30, 2016 18:48
Show Gist options
  • Save bturnbull/f2bcb733d92a667e85eee17100c11c21 to your computer and use it in GitHub Desktop.
Save bturnbull/f2bcb733d92a667e85eee17100c11c21 to your computer and use it in GitHub Desktop.
Elixir Stream.transform Accumulation
# 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