Elixir demonstrate if, case and cond keyword







The following code demonstrate how we can use if, case and cond keyword.

defmodule Test do

  def bigger(a) do  
    if (a > 2) do
      :ok
    else
      :error
   end
 end

def getSize(a) do  

 case a do
    n when n in 1..20 ->  
        :small
    n when n in 21..30 ->
        :med
    n when n in 31..40->
       :big
   end
  end

def getSizeCond(a) do  

 cond do
  a > 30 ->
     :big
  a > 20 ->
     :med
  a > 10 ->
      :small
   end  
 end
end


So you can quickly run some case checking using code below :-

case Test.bigger(2) do
 :ok -> 1
 :error -> -1
end






Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm