Skip to content

Instantly share code, notes, and snippets.

@ray-sh
Last active July 22, 2017 14:47
Show Gist options
  • Save ray-sh/77b75e0193ec30af3ec7ae433967f629 to your computer and use it in GitHub Desktop.
Save ray-sh/77b75e0193ec30af3ec7ae433967f629 to your computer and use it in GitHub Desktop.
Demo how to use Enum.reduce in elixir
1>Find the max number of a list
2>Remove some elements of a Map
3>Remove some elements of a List
4>Map/reduce
defmodule MyReduce do
@moduledoc false
def max_number_of_list(list) do
Enum.reduce list,List.first(list),fn(x,acc) ->
if(x > acc) do
acc = x
else
acc = acc
end
end
end
def remove_minus_of_list(list) do
Enum.reduce list, [], fn(x,acc) ->
if (x>=0), do: [x | acc], else: acc
end
end
def remove_ele_from_map(map,value) do
Enum.reduce map, map, fn
({k,v},acc) when value == v -> Map.delete(acc,k)
(_,acc) -> acc
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment