Quick Start

3 Hitting the Code

It may not seem like it, but we're off to a pretty fast start. If you had to do everything we've done, manually, you'd have given up by now. Seriously.

Time to put the brakes on, though, 'cause you're gonna want to see this next part in slow motion.

3.1 REPL Me Up!

Sure thing :-) Just do this from your new LFE project directory:

make shell-no-deps

This should give you something that looks like the following:

Erlang R16B (erts-5.10.1) [source] [smp:8:8] [async-threads:10] [hipe] ...

LFE Shell V5.10.1 (abort with ^G)
>

We're not going to spend a lot of time in the REPL right now, but feel free to take it for a spin :-)

> (+ 1 2 3 4 5 6)
21
> (* 2 (+ 1 2 3 4 5 6))
42
> (lists:foldl (lambda (n acc) (+ n acc)) 0 (lists:seq 1 6))
21

Enough fancy stuff! What about code? What does a project look like?

3.2 Sample Code

Well, you've already seen one! Let's print it here for your viewing pleasure, though. Here's the starter project you have, with just the one module:

(defmodule my-test-lib
  (export all))

(defun my-adder (x y)
  (+ x y))

It has a corresponding test module:

(defmodule my-test-lib_tests
  (export all)
  (import
    (from lfeunit-util
      (check-failed-assert 2)
      (check-wrong-assert-exception 2))))

(include-lib "deps/lfeunit/include/lfeunit-macros.lfe")

(deftest my-adder
  (is-equal 4 (my-test-lib:my-adder 2 2)))

Almost couldn't be simpler.

Here's something a little more involved you may enjoy, from the examples in the LFE source code:

(defmodule messenger-back
 (export (print-result 0) (send-message 2)))

(defun print-result ()
  (receive
    ((tuple pid msg)
      (io:format "Received message: '~s'~n" (list msg))
      (io:format "Sending message to process ~p ...~n" (list pid))
      (! pid (tuple msg))
      (print-result))))

(defun send-message (calling-pid msg)
  (let ((spawned-pid (spawn 'messenger-back 'print-result ())))
    (! spawned-pid (tuple calling-pid msg))))

That bit of code demonstrates one of Erlang's core features in lovely Lisp syntax :-) (For those that don't read Erlang yet, when loaded into the REPL, that example shows some bidirectional message passing between the LFE shell and a spawned process.)

If you'd like to learn more about using the LFE REPL, be sure to read the REPL Section of the User Guide.

If you want to step through a tutorial on Erlang's light-weight threds (per the example code above), you'll want to read this tutorial.

Next Stop

We did promise a bit more information, so we're going to do that next and then wrap up the quick start and point you in the direction of your next LFE adventure ...