Last active
July 22, 2017 14:47
-
-
Save ray-sh/77b75e0193ec30af3ec7ae433967f629 to your computer and use it in GitHub Desktop.
Demo how to use Enum.reduce in elixir
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
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