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

ionic2 cordova build android - Unable resolve gradle 2.2.3

OpenCover code coverage for .Net Core

Using Custom DLL with IronPython / Scripts