Skip to content

Instantly share code, notes, and snippets.

@sandeshsoni
Created February 6, 2016 03:40
Show Gist options
  • Save sandeshsoni/6bb12b18e965ce77155f to your computer and use it in GitHub Desktop.
Save sandeshsoni/6bb12b18e965ce77155f to your computer and use it in GitHub Desktop.
Introducing Elixir to Rubyist
# This is a comment
# The first thing in any language
next_integer_after = fn x -> x+1
end
# This is an annoymous function.
# called inside function as next_integer_after(3)
defmodule ModuleName do
# this is a module
end
defmodule SomeModule do
def public_method do
end
end
# called as public_method or SomeModule.public_method
defmodule SomeModule do
defp private_method do
end
end
# called inside module by just private_method
# one more common stuff we do. Concatenation of strings
first_name = "lorem"
last_name = "epsum"
full_name = first_name <> " " <> last_name
# lets make it a simple anonymous function,
greet_person = fn {greeting, first_name, last_name} ->
greeting <> first_name <> last_name
end
# But we may not always have greeting present.
# How about default greet ?
greet_person = fn {greeting // "hi", first_name, last_name} ->
greeting <> first_name <> last_name
end
called as greet_person.({"good morning", "Anil", "W"})
# But we are so many people here.
# ok, lets have one more.
greet_team = fn
when {greeting // "hi", team} ->
greeting <> " " <> team
end
{greeting // "hi", first_name, last_name} ->
greeting <> first_name <> last_name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment