Skip to content

Instantly share code, notes, and snippets.

@nyaray
Created August 3, 2019 17:00
Show Gist options
  • Save nyaray/133da6749416e26a0903116311fa8750 to your computer and use it in GitHub Desktop.
Save nyaray/133da6749416e26a0903116311fa8750 to your computer and use it in GitHub Desktop.
Run-length encoding
defmodule RLE do
defp run_length_encode(terms, encoding) do
terms = Enum.map(terms, &encoding[&1])
Enum.reduce(tl(terms), [{hd(terms), 1}], fn
c, [{c, count} | rest] -> [{c, count + 1} | rest]
c, [{c_prime, count} | rest] -> [{c, 1}, {c_prime, count} | rest]
end)
|> Enum.map(fn {c, count} -> "#{c}#{inspect(count)}" end)
|> Enum.reverse()
|> Enum.join()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment