Don’t miss understand the word fishy . You can use that word as suspect orshady . I just want to expose those shady things to light for beginners.

1. Finding the Terminal Width and height

1
2
3
4
5
6
7
defmodule TerminalWidthHeight do
  def term_dimensionns do
    {:ok,width} = :io.columns
    {:ok,height} = :io.rows
    IO.inspect {width,height}
  end
end

it works on resizing your terminal also

2. Underscores in Number representation

You can specify underscore decimal numbers may contain underscores  (_ ) these are often used to separate groups of three digits when writing large numbers, so one million could be written 1_000_000 (or perhaps 100_0000 in China and Japan or 10_00_000 in India). You can also perform any arithmetic operations as they are just numbers.

1
2
number = 1_000_777
number + 7_000

3. Function Execution Time

1
2
3
4
5
6
7
defmodule ExecutionTime do
  def time_of(function, args) do
    {time, result} = :timer.tc(function, args)
    IO.puts "Time: #{time}ms"
    IO.puts "Result: #{result}"
  end
end

4. Empty Map pattern matching

1
2
3
%{} = %{name: "blackode"} # pattern matches
%{name: "blackode"} = %{} # pattern do not match here.
%{name: "blackode"} = %{name: "blackode",age: "hello"}#match pattern

5.Infinite Streams

1
(1..10_000_000)|> Enum.map(&(&1))|> Enum.take(5)

The above line takes almost 8 seconds

1
(1..10_000_000)|> Stream.map(&(&1))|> Enum.take(5)

See the magic after running those two lines.

6. Character Lists [ vs ] Numbers

1
2
[67,65,84]
'CAT'            # Out put of above list.

In the list if all the numbers are of printable characters then they are not more considered as **number** unlike we expected. So, be careful with the lists.

7. Single line Module and Definitions — No more End

There is no need to specify the end for every time you write the module.

1
2
3
4
5
defmodule M, do: (
                   def hi, do: (IO.puts "Hi!")
                   def hello, do: (IO.puts "Hello!!")
                   def bye, do: (IO.puts "Bye!!")
                  )

You can use just the braces for separating the multiple lines of code in do block.

8. #iex:break

There will be situation that when you type mistake that you no where go to back or end the that line of code.

1
2
3
4
5
iex(1)> ["ab
...(1)> c"
...(1)> "
...(1)> ]
...(1)> #iex:break ** (TokenMissingError) iex:1: incomplete expressionThis helps you to come out of the loop.

9. Shell Variables in IEx

If you want to check the all the variables which are defined in the current iex shell just do binding()

1
binding()

This returns the list of key value pairs with variable_name as key and its bound value.

10. Chained anonymous functions

1
2
wish = fn -> fn -> fn -> IO.write "this is chain" end end end
wish.().().()   # usage

11. Multiple or conditions — in

1
number = fn(num) when num in [1,2,3,4,5] -> IO.write num end

This code works only if you pass num value 1<=num<=5 other wise it returns non match function class error .

As the medium is meant for medium articles which are to be medium in size. So I am keeping this short and sweet and will meet on next part which will cover more topics…

Just got a thought of talking on this article and I want to show the code snippets by executing my self. So, I made a YouTube video . The audio is not good, please bear with me.. soon I will improve the audio quality.

Happy Coding !