I've recently started learning elixir, and the following is my attempt at fetching the local IP address of the system.
It can definitely be improved further, but as this would be my first ever project in a functional language; I'd be open to any criticisms.
defmodule Systemip do def private_ipv4() do {:ok, addrs} = :inet.getif() filtered = Enum.filter( addrs, fn address -> ip = elem(address, 0) is_private_ipv4(ip) end ) elem(hd(filtered), 0) end defp is_private_ipv4(ip) do case ip do {10, _x, _y, _z} -> true {192, 168, _x, _y} -> true {172, 16, _x, _y} -> true {172, 32, _x, _y} -> true _ -> false end end end
This would be part of a larger project, which is still under construction. The IPs are fetched using the erlang call to :inet.getif
, the formatting was done by mix format
command.