Skip to content

Instantly share code, notes, and snippets.

View tallakt's full-sized avatar

Tallak Tveide tallakt

View GitHub Profile
@tallakt
tallakt / lazy_permutations.exs
Last active January 2, 2021 04:42 — forked from martinsvalin/lazy_permutations.exs
Lazy permutations in Elixir
defmodule LazyPermutations do
def permutations(list) do
list
|> Enum.sort
|> Stream.unfold fn
[] -> nil
p -> {p, next_permutation(p)}
end
end
@tallakt
tallakt / bubble.exs
Last active February 10, 2017 08:16
defmodule Bubble do
def sort(list) do
do_sort list, {[], false}
end
defp do_sort([a, b | tail], {acc, _}) when b < a do
do_sort [a | tail], {[b | acc], true}
end
defp do_sort([a | tail], {acc, swapped?}) do