Lisp Flavoured Erlang and Pynchon, oh my!

Posted on 6 August, 2015 by Eric Bailey
Tags: lfe, lisp, beam, metaprogramming, open-source

So, lately I've been getting increasingly into Lisp Flavoured Erlang (LFE). If you haven't tried it yet, or even if you have, check out the Exercism.io track I helped organize. My latest endeavour is porting Robert Levy's swiss-arrows from Clojure to LFE. It's been going well so far, despite having to rename it to pynchon, since Erlang wasn't down with the hyphenated name and a few people on the LFE Google group suggested it.

Without further ado, here's a contrived example:

(-<> "testing"
     (-!<>> (string:substr <> 1 4)
            (lists:duplicate 3)
            (compose #'list/1 #'lists:flatten/1)
            (lfe_io:format '"non-updating: ~p\n"))
     (string:substr 5)
     (++ "winn" <>))

Note: #'compose/2 comes from Duncan McGreggor's clj, Clojure functions and macros for LFE.

The wild-looking form above expands to something more like:

(-<>
 (progn
   (lfe_io:format
    '"non-updating: ~p\n"
    (list
     (lists:flatten
      (lists:duplicate 3 (string:substr "testing" 1 4)))))
   "testing")
 (string:substr 5)
 (++ "winn" <>))

After that, it becomes apparent the "return track" is rather simple:

(++ "winn" (string:substr 5 "testing"))
> (-<> "testing"
       (-!<>> (string:substr <> 1 4)
              (lists:duplicate 3)
              (compose #'list/1 #'lists:flatten/1)
              (lfe_io:format '"non-updating: ~p\n"))
       (string:substr 5)
       (++ "winn" <>))
non-updating: "testtesttest" ; printed
"winning"                    ; returned