Everything from the Erlang and OTP is available on LFE. Although, you can pretty much write code in a more loose form, you can still implements behaviors that are already present on the OTP and others.
Processes
Receive and send are still the same. Each cause on the receive are still made of a list of pairs in the form (message, handler). The two wildcard option to end a receive, or many forms, is using else or _.
(receive
(’a (io:format “The atom \”a\””))
((cons 'a num) (io:format “A list with atom \”a\” and a number ~p” (list num)))
((tuple 'a num) (io:format “A tuple with atom \”a\” and a number ~p ” (list num)))
(else (io:format "Wildcard pattern. Same as _.")))
Sending a message is still the same pid ! message, but with parenthesis (! pid message).
(let ((p (spawn (lambda ()
(receive
(else (io:format “wildcard case~n”)))))))
(! p 1)))
Creating a gen_server
defmodule is a macro that generates 2 forms.
lfe> (macroexpand ‘(defmodule k))
(progn (define-module k () ()) (defmacro MODULE () `’k))
To be more generic just like the ?MODULE in Erlang, we use (MODULE) that is a macro that gives the correct module atom name.
(defmodule my-app-server
(behavior gen_server)
(export
(init 1)
(terminate 2)
(start 0)
(handle_call 3)
(handle_cast 2)))
(defun init (_args)
(logger:info “Starting simple gen_server...”)
(tuple 'ok '()))
(defun terminate (_reason _state)
‘normal)
(defun start ()
(gen_server:start_link (MODULE) (list) (list)))
(defun handle_call (request from state)
(tuple 'noreply state))
(defun handle_cast (request state)
(tuple 'noreply state))
It still the same declaration and usage of any behavior like application, supervisor, gen_server and others.