/ playground Blog

Blimp Cheat Sheet

Basics

x = 42
name = "hello"
flag = true
items = [1, 2, 3]
data = %{name: "bob", age: 30}

Pipes

items |> length(_)
items |> reverse(_)

Actors

actor Counter do
  state count: Int :: 0

  on :increment do
    become count: count + 1
    reply count + 1
  end

  on :get do
    reply count
  end
end

Spawn + Message Send

c = spawn Counter
c <- :increment
c <- :increment
c <- :get

Guards

actor Account do
  state balance: Int :: 100

  on :withdraw(amount) when amount > 0 do
    become balance: balance - amount
    reply balance - amount
  end

  on :withdraw(amount) when amount <= 0 do
    reply :error
  end
end

Closures

double = fn(x) do x * 2 end
double(5)

Map / Filter / Reduce

nums = [1, 2, 3, 4, 5]
map(nums, fn(x) do x * 10 end)
filter(nums, fn(x) do x > 2 end)
reduce(nums, 0, fn(acc, x) do acc + x end)

Spread Operators

double = fn(x) do x * 2 end
...[1, 2, 3], double
..[1, 2, 3], fn(x) do x end
... = map (returns list), .. = each (returns :ok)

For Loop

for x in [10, 20, 30] do
  x + 1
end

Situation (Pattern Matching)

situation :green do
  :red -> "stop"
  :green -> "go"
  :yellow -> "slow"
end

State Machine

actor Light do
  state color: Atom :: :red

  on :next do
    situation color do
      :red -> become color: :green
      :green -> become color: :yellow
      :yellow -> become color: :red
    end
    reply color
  end
end

l = spawn Light
l <- :next
l <- :next
l <- :next

Self Send

actor Doubler do
  state val: Int :: 0

  on :set(n) do
    become val: n
    reply n
  end

  on :double do
    self <- :set(val * 2)
    reply val
  end
end

Holes (Agent Directives)

situation payment do
  :valid -> charge(payment)
  _ # Hole: handle invalid payment
end
The comment after _ is a directive to the AI agent