Skip to content

Instantly share code, notes, and snippets.

@schmalz
Created October 10, 2020 16:58
Show Gist options
  • Save schmalz/3b4e8176914f2b55cb4fffe56402d7f8 to your computer and use it in GitHub Desktop.
Save schmalz/3b4e8176914f2b55cb4fffe56402d7f8 to your computer and use it in GitHub Desktop.
LFe tutorial 19 - message passing
(defmodule lfe-tut-19
(export (start 0)
(ping 2)
(pong 0)))
(defun ping
([0 pong-pid]
(! pong-pid 'finished)
(lfe_io:format "ping finished~n" ()))
([n pong-pid]
(! pong-pid (tuple 'ping (self)))
(receive
('pong
(lfe_io:format "ping received pong~n" ())))
(ping (- n 1) pong-pid)))
(defun pong []
(receive
('finished
(lfe_io:format "pong received finished, pong finished~n" ()))
((tuple 'ping ping-pid)
(lfe_io:format "pong received ping~n" ())
(! ping-pid 'pong)
(pong))))
(defun start []
(let ((pong-pid (spawn 'lfe-tut-19 'pong ())))
(spawn 'lfe-tut-19 'ping (list 3 pong-pid))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment