This file contains 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
defmodule LazyPermutations do | |
def permutations(list) do | |
list | |
|> Enum.sort | |
|> Stream.unfold fn | |
[] -> nil | |
p -> {p, next_permutation(p)} | |
end | |
end |
This file contains 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
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 |