The Trap Is a Mailbox
A light trap is the laziest possible collector. It emits, it waits, and it holds whatever wanders in until you come back. Nothing gets processed in the moment — moths arrive at 11pm, at 2am, at the grey edge of dawn, in whatever order the night sends them, and the trap just accumulates. You do the sorting later, over coffee, with the whole catch sitting still on the egg cartons.
That’s a mailbox. Specifically it’s the thing Ericsson built Erlang around in 1986: a process that receives messages into an unbounded queue and handles them on its own schedule, decoupled from whoever sent them. Senders never block waiting for the trap to be ready. The moth doesn’t care whether you’re asleep.
defmodule Trap do
def open, do: loop([])
defp loop(caught) do
receive do
{:moth, species} -> loop([species | caught])
{:dawn, from} -> send(from, {:tally, Enum.frequencies(caught)})
end
end
end
trap = spawn(&Trap.open/0)
for m <- ~w(yellow_underwing setaceous yellow_underwing elephant_hawk),
do: send(trap, {:moth, m})
send(trap, {:dawn, self()})
receive do {:tally, t} -> IO.inspect(t) end
Run it and the queue drains to %{"yellow_underwing" => 2, "setaceous" => 1, "elephant_hawk" => 1} regardless of arrival timing. The pattern match inside receive is selective — you can pull the hawk-moths out of the mailbox first and leave everything else buffered, which is how you actually work a trap: the big showy ones get identified and released before they warm up and bolt.
Bash has no mailbox, but it has a file and background jobs, which is enough to fake the shape:
box=$(mktemp) # the egg carton
for m in yellow_underwing setaceous yellow_underwing elephant_hawk; do
( echo "$m" >> "$box" ) & # each moth logs itself, whenever
done
wait # ...until dawn
sort "$box" | uniq -c # count the catch
rm -f "$box"
Short appends are atomic, so concurrent arrivals don’t shred each other’s lines. What you give up is selectivity: sort | uniq -c drains the whole file in one pass and one order — there’s no reaching into the queue to identify the elephant hawk-moth before the rest of the night’s catch.