How is a double loop implemented in Elixir? -
how double loop implemented in elixir?
i interested in writing double loop compare naivesort quicksort implementation.
then realized don't know how write double loop in elixir!
so, how double loop written in elixir? keep in mind i'm trying naivesort...
an idiomatic way might use list comprehensions like:
defmodule quick def sort([first | rest]) sort(for(n <- rest, n < first, do: n)) ++ [first] ++ sort(for(n <- rest, n >= first, do: n)) end def sort([]) [] end end iex(5)> quick.sort [5,4,3,2,1] [1, 2, 3, 4, 5]
of course quick sort lends quite nicely recursive solution since algorithm "sort items smaller me, add me, add items larger me". expressed in elixir (and erlang).
the for
list comprehension. builds list based upon generator (the n <- rest
part , filter (the n < first
part).
Comments
Post a Comment