Skip to content

Coming from Elixir

Much of Elixir carries over: every value is immutable, |> pipes, case with pattern matching, %{"a" => 1} maps, Map and Enum-style functions called through their module, string interpolation, supervised tasks, and tests beside the code. What differs most: Nomi is statically typed, so every case must cover every shape, failures are Result and Maybe values rather than {:error, _} tuples, there are no atoms, and tasks talk over channels rather than through a mailbox each.

The cheat sheet lists the rules in one place.

ElixirNomi
def f(x) do .. endfn f(x: Int) -> Int .. end, with parameter types and no do
Enum.map(xs, &(&1 * 2))Iter.map(xs, &(&1 * 2)), lazy until Iter.to_list
Enum.reduce(xs, 0, fn x, acc -> .. end)Iter.reduce(xs, 0, fn acc, x -> ..): the accumulator comes first
{:ok, v}, {:error, e}Ok(v), Err(e), and a postfix ? instead of with
nilNone, from Maybe<T>
:atomAn enum variant: .Ready
{a, b}, elem(t, 0)(a, b), t.first
%{name: "Ada"} (atom keys){name: "Ada"}, an anonymous struct
cond do, if .. docase alone on its line for cond, and if .. then .. else .. end for if. Without else, Elixir’s gives nil when the condition fails; Nomi’s is a statement with no value, and its branch must be Unit or leave. Nothing stands in for nil there: Elixir’s if c, do: x is if c then Some(x) else None.
7 / 2 is 3.5; div(7, 2), rem(-7, 3)/ on two Ints is div: 7 / 2 is 3, truncated toward zero. % is rem: -7 % 3 is -1. Convert an operand with Int.to_float for a Float result. The divisor must be a NonZeroInt: a literal other than 0 is one, and so is d after if d == 0 then return ...
spawn, send, receiveTask.spawn in concurrent … end, and channels
File.open(path, fn file -> .. end)defer Reader.close_quietly(r) on the line after opening; it runs when its block ends, and the checker requires it for a resource
ElixirNomi
%User{name: "Ada"}, %{"a" => 1}User{name: "Ada"}, {"a" => 1}: no %
"#{x}""${x}"
Decimal.new("1.50") from the decimal package1.50d, a Decimal literal, in the language: 0.1d + 0.2d == 0.3d. See Exact arithmetic with Decimal.
when guardswhen guards, the same
defpA declaration without pub
defprotocol Speak, defimpl Speak, for: Doginterface Speak, impl Speak for Dog
alias MyApp.Db.Posts, then Posts.find(1)import db::posts, then posts.find(1): a file is used by its path’s last part. import db imports the directory, and db::posts.find(1) reaches the file through it.
SupervisorSupervisor.new in boot
GenServer, with call and castActor.start, Actor.call and Actor.cast from std::actors: one task that folds each message into its state
The application callback, start/2, which starts the supervision tree; settings read with Application.get_envfn boot() -> Result<App, String> builds an App struct, and starts any supervisors, before main runs. Any function reads a field as App.repo. A test replaces one with with App.repo = ... See App Fields.
ExUnittest "name" … end
Phoenix’s get "/users/:id", UserController, :show, with %{"id" => id} a stringRoute.to_with("GET /users/{id}", show) with fn show(Request, user: {id: Int}) -> Response. The handler gets user.id already parsed, /users/ann gets a 404, and the pattern is checked when the program is. See HTTP and Frontends.
defmodule Orders do
  def total(orders) do
    orders
    |> Enum.filter(&(&1.status == :paid))
    |> Enum.sum_by(& &1.amount)
  end

  def parse_amount(text) do
    case Integer.parse(text) do
      {n, ""} when n >= 0 -> {:ok, n}
      _ -> {:error, "not an amount: #{text}"}
    end
  end
end
// FILE: orders.nomi
enum Status
    Paid
    Open
end

struct Order
    status: Status
    amount: Int
end

fn total(orders: List<Order>) -> Int
    orders
    |> Iter.filter(&(&1.status == .Paid))
    |> Iter.sum_by(.amount)
end

fn parse_amount(text: String) -> Result<Int, String>
    case String.to_int(text)
        Some(n) when n >= 0 -> Ok(n)
        _                   -> Err("not an amount: ${text}")
    end
end

// FILE: orders_test.nomi
import orders.{Order, parse_amount, total}

test "the same program"
    orders = [Order{status: .Paid, amount: 5}, Order{status: .Open, amount: 7}]

    assert total(orders) == 5
    assert parse_amount("12") == Ok(12)
    assert parse_amount("-1") == Err("not an amount: -1")
end

.amount is a field accessor, a function that reads the field. .Paid is Status.Paid, with the enum taken from the type the comparison expects.