I have a list that I want to split based on the transition from structure type B to A. So, for example, I have the following:
iex(1)> defmodule A, do: defstruct [] {:module, A ... iex(2)> defmodule B, do: defstruct [] {:module, B ... iex(3)> values = [ %A{}, %A{}, %B{}, %B{}, %B{}, %A{}, %A{}, %B{} ] [%A{}, %A{}, %B{}, %B{}, %B{}, %A{}, %A{}, %B{}]
I want this data to be broken down into a 2-element list containing:
[ [ %A{}, %A{}, %B{}, %B{}, %B{} ], [ %A{}, %A{}, %B{} ] ]
If at first the input should have been all A or all B, the output would not have changed, since there was no transition B-> A.
I believe Enum.chunk_by/2 is the way to go, but itβs hard for me to figure out how to maintain the context of the previous element in order to know when to split.
What does this idiomatic solution look like?
elixir
Nick veys
source share