Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# my blog : https://trangell.com/fa/
# public : http://iranonrails.ir
people = [
%{ name: "Grumpy", height: 1.24 },
%{ name: "Dave", height: 1.88 },
%{ name: "Dopey", height: 1.32 },
%{ name: "Shaquille", height: 2.16 },
%{ name: "Sneezy", height: 1.28 }
]
defmodule HotelRoom do
def book(%{name: name, height: height})
when height > 1.9 do
IO.puts "Need extra long bed for #{name}"
end
def book(%{name: name, height: height})
when height < 1.3 do
IO.puts "Need low shower controls for #{name}"
end
def book(person) do
IO.puts "Need regular bed for #{person.name}"
end
end
people |> Enum.each(&HotelRoom.book/1)
# Enum.each(people, fn(x) -> HotelRoom.book(x) end)
# Need low shower controls for Grumpy
# Need regular bed for Dave
# Need regular bed for Dopey
# Need extra long bed for Shaquille
# Need low shower controls for Sneezy
# good
%{2 => state} = %{ 1 => :ok , 2 => :error }
state
# error
%{ item => :ok } = %{ 1 => :ok , 2 => :error }
# pin operator
iex(7)> data = %{ name: "shahryar" , state: "Tx", likes: "Swift" }
%{likes: "Swift", name: "shahryar", state: "Tx"}
iex(8)> for key <- [ :name, :likes ] do
...(8)> %{^key => value } = data
...(8)> value
...(8)> end
["shahryar", "Swift"]
# update map
new_map %{old_map | key => value.. }
m = %{ a: 1, b: 2 , c: 3 }
%{ a: 1, b: 2 , c: 3 }
m1 = %{ m | b: "two" , c: "three" }
%{ a: 1, b: "two" , c: "three" }
m2 = %{ m1 | a: "one"}
%{a: "one", b: "two", c: "three" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment