This is just the other way of writing Multiple OR conditions. This is not the recommended approach because in regular approach when the condition evaluates to true , it stops executing the remaining conditions which saves time of evaluation unlike this approach which evaluates all conditions first in list. This is just bad but good for discoveries.
# Regular Approachfind =fn(x) when x>10or x<5or x==7-> x end# Our Hackhell =fn(x) whentruein [x>10,x<5,x==7] -> x end
2. i( term) Elixir Term Type and Meta Data
Prints information about the data type of any given term. Try that in iex and see the magic.
iex>i(1..5)
3. iex Custom Configuration - iex Decoration
Copy the content into a file and save the file as .iex.exs in your ~ home directory and see the magic. You can also download the file HERE
defmoduleMySigilsdo#returns the downcasing string if option l is given then returns the list of downcase lettersdefsigil_l(string,[]), do: String.downcase(string)defsigil_l(string,[?l]), do: String.downcase(string) |>String.graphemes#returns the upcasing string if option l is given then returns the list of downcase lettersdefsigil_u(string,[]), do: String.upcase(string)defsigil_u(string,[?l]), do: String.upcase(string) |>String.graphemesend
defmoduleBugErrordodefexception message: "BUG BUG .."# message is the defaultend
Usage
$ iex bug_error.exiex>raiseBugError** (BugError) BUGBUG..iex>raiseBugError, message: "I am Bug.."#here passing the message dynamic** (BugError) I am Bug..
6. Get a Value from Nested Maps Easily
The get_in function can be used to retrieve a nested value in nested maps using a list of keys.
nested_map = %{ name: %{ first_name: "blackode"} } # Example of Nested Mapfirst_name =get_in(nested_map, [:name, :first_name]) # Retrieving the Key# Returns nil for missing value nil=get_in(nested_map, [:name, :last_name]) # returns nil when key is not present
The special form with is used to chain a sequence of matches in order and finally return the result of do: if all the clauses match. However, if one of the clauses does not match, its result of the miss matched expression is immediately returned.
iex>with1<-1+0,2<-3+1, do: IO.puts"all matched"4## since 2 <- 3+1 is not matched so the result of 3+1 is returned
8. Writing Protocols
Define a Protocol
A Protocol is a way to dispatch to a particular implementation of a function based on the type of the parameter. The macros defprotocol and defimpl are used to define Protocols and Protocol implementations respectively for different types in the following example.
defprotocolTripledodeftriple(input) enddefimplTriple, for: Integerdodeftriple(int) do int *3endenddefimplTriple, for: Listdodeftriple(list) do list ++ list ++ list endend