Skip to content

Instantly share code, notes, and snippets.

@vikashvikram
Created March 28, 2018 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikashvikram/1e442d6fb753b400fef3ea83b6e7e253 to your computer and use it in GitHub Desktop.
Save vikashvikram/1e442d6fb753b400fef3ea83b6e7e253 to your computer and use it in GitHub Desktop.
Elixir Cheat sheet
# Vikram March 28, 2018
# Based on video on elixir by Derek
# commands to run: iex, c("elix_tut.ex"), M.main
defmodule M do
def main do
# string_stuff()
# math_stuff()
# compare_stuff()
# decision_stuff()
# tuple_stuff()
# list_stuff()
# map_stuff()
# pattern_stuff()
# anonymous_function()
# recursion_stuff()
# loop_stuff()
# enum_stuff()
# error_stuff()
# additional_stuff()
concurrency_stuff()
end
def concurrency_stuff do
spawn(fn() -> loop(50, 1) end)
spawn(fn() -> loop(100, 50) end)
send(self(), {:french, "Vikram"})
receive do
{:german, name} -> IO.puts("Guten tag #{name}")
{:french, name} -> IO.puts("Bonjour #{name}")
{:english, name} -> IO.puts("Hello #{name}")
after
500 -> IO.puts "Time up"
end
end
def error_stuff do
err = try do
5 / 0
rescue
ArithmeticError -> "Cant divide by Zero"
end
IO.puts err
end
def additional_stuff do
dbl_list = for n <- [1,2,3], do: n*2
IO.inspect dbl_list
even_list = for n <- [1,2,3,4], rem(n, 2) == 0, do: n
IO.inspect even_list
end
def enum_stuff do
IO.puts "Even list: #{Enum.all?([1, 2, 3], fn(n) -> rem(n, 2) == 0 end)}"
IO.puts "Even list any: #{Enum.any?([1, 2, 3], fn(n) -> rem(n, 2) == 0 end)}"
Enum.each([1, 2, 3], fn(n) -> IO.puts n end)
dbl_list = Enum.map([1,2,3], fn(n) -> n*2 end)
IO.inspect dbl_list
sum_vals = Enum.reduce([1,2,3], fn(n, sum) -> n+sum end)
IO.puts "Sum: #{sum_vals}"
IO.inspect Enum.uniq([1,2,2,3,3])
end
def loop_stuff do
IO.puts "Sum: #{sum([1, 2, 3])}"
loop(5, 1)
end
def sum([]), do: 0
def sum([h|t]), do: h + sum(t)
def loop(0, _), do: nil
def loop(max, min) do
if max < min do
loop(0, min)
else
IO.puts "Num: #{max}"
loop(max - 1, min)
end
end
def recursion_stuff do
IO.puts "factorial of 4 is : #{factorial(4)}"
end
def factorial(num) do
if num <= 1 do
1
else
res = num * factorial(num - 1)
res
end
end
def anonymous_function do
get_sum = fn (x, y) -> x+y end
IO.puts "5 + 4 = #{get_sum.(5, 4)}"
get_less = &(&1 - &2)
IO.puts "4 - 2 = #{get_less.(4, 2)}"
add_sum = fn
{x, y} -> IO.puts "#{x} + #{y} = #{x+y}"
{x, y, z} -> IO.puts "#{x} + #{y} + #{z} = #{x+y+z}"
end
add_sum.({1, 2, 3})
add_sum.({1, 2})
IO.puts do_it()
end
# fn with default values for arguments
def do_it(x \\ 1, y \\ 1) do
x + y
end
def pattern_stuff do
[_, [_, a]] = [10, [20, 30]]
IO.puts "a is #{a}"
end
def map_stuff do
capitals = %{"UP" => "Lucknow", "Haryana" => "Chandigarh"}
IO.puts "Capital of UP is #{capitals["UP"]}"
capitals2 = %{up: "Lucknow", haryana: "Chandigarh"}
IO.puts "Capital of Haryna is #{capitals2.haryana}"
capitals3 = Dict.put_new(capitals, "Tamilnadu", "Chennai")
IO.inspect capitals3
end
def list_stuff do
list1 = [1,2,3]
list2 = [4,5,6]
list3 = list1 ++ list2
list4 = list3 -- list1
IO.puts 6 in list4
[head | tail] = list3
IO.puts "head: #{head}"
IO.write "Tail: "
IO.inspect tail
IO.inspect [97, 98]
IO.inspect [97, 98], charlists: :as_lists
Enum.each tail, fn item ->
IO.puts item
end
display_list(["This", "is", "a", "sentence"])
IO.inspect List.delete(list3, 4)
IO.inspect List.delete_at(list3, 0)
IO.inspect List.insert_at(list3, 0, 9)
IO.puts List.first(list3)
IO.puts List.last(list3)
another_list = [name: 'vikash', height: 6.1]
IO.inspect another_list
end
def display_list([word | words]) do
IO.puts word
display_list(words)
end
def display_list([]), do: nil
def tuple_stuff do
my_stats = {183.5, 6, :"Vikash Vikram"}
IO.puts "tuple: #{is_tuple(my_stats)}"
my_stats2 = Tuple.append(my_stats, 34)
IO.puts "age: #{elem(my_stats2, 3)}"
IO.puts "Size: #{tuple_size(my_stats2)}"
my_stats3 = Tuple.delete_at(my_stats2, 0)
my_stats4 = Tuple.insert_at(my_stats3, 0, 1984)
many_zeroes = Tuple.duplicate(0, 5)
{weight, height, name} = {85, 6.1, "Vikash Vikram"}
IO.puts "weight: #{weight}"
end
def string_stuff do
IO.puts "Equals: #{"Egg" === "egg"}"
my_str = "Sentence"
IO.puts "Length: #{String.length(my_str)}"
longer_string = my_str <> " " <> "is longer"
IO.puts "My ? #{String.contains?(longer_string, "is")}"
IO.puts "First: #{String.first(longer_string)}"
IO.puts "Index 4: #{String.at(longer_string, 4)}"
IO.puts "Substring: #{String.slice(longer_string, 4, 10)}"
IO.inspect String.split(longer_string, " ")
IO.puts String.reverse(my_str)
IO.puts String.upcase(longer_string)
IO.puts String.downcase(longer_string)
IO.puts String.capitalize(longer_string)
4 * 10 |> IO.puts
end
def math_stuff do
IO.puts "5 + 4 = #{5+4}"
IO.puts "5 - 4 = #{5-4}"
IO.puts "5 * 4 = #{5*4}"
IO.puts "5 / 4 = #{5/4}"
IO.puts "5 div 4 = #{div(5, 4)}"
IO.puts "5 rem 4 = #{rem(5, 4)}"
end
def compare_stuff do
IO.puts "4 == 4.0 : #{4 == 4.0}"
IO.puts "4 === 4.0 : #{4 === 4.0}"
IO.puts "4 != 4.0 : #{4 != 4.0}"
IO.puts "4 !== 4.0 : #{4 !== 4.0}"
age = 16
IO.puts "Vote and Drive: #{(age >= 16) and (age >= 18)}"
IO.puts "Vote or Drive: #{(age >= 16) or (age >= 18)}"
IO.puts not true
end
def decision_stuff do
age = 16
if age >= 18 do
IO.puts "You can Vote"
else
IO.puts "You can't Vote"
end
unless age === 18 do
IO.puts "You are not 18"
else
IO.puts "You are 18"
end
cond do
age >= 18 -> IO.puts "You can vote"
age >= 16 -> IO.puts "You can drive"
age >= 14 -> IO.puts "You can wait"
true -> IO.puts "default"
end
case 2 do
1 -> IO.puts "Entered 1"
2 -> IO.puts "Entered 2"
_ -> IO.puts "default"
end
IO.puts "Ternary: #{if age > 18, do: "Can vote", else: "Can't vote"}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment