lfe_clj(3)

๐Ÿ“ฆ
version 2.2.0 ยท 2 versions
, in alpine 3.24, nixos 26.05

๐Ÿ’– lfe_clj(3) โ€” the clj LFE Clojure interface library ๐Ÿ’–

โœจ NAME โœจ

clj โ€” LFE Clojure interface library. Itโ€™s literally the Clojure-flavored bestie for your LFE code. ๐Ÿซถ

โœจ SYNOPSIS โœจ

This module provides Clojure-inspired functions and macros for use in LFE. Thatโ€™s the whole vibe: Clojure energy, LFE address. ๐Ÿ’…

โœจ EXPORTS โœจ

N.B. You do not have to make fully-qualified calls to the macros exported from clj โ€” you can (include-lib "lfe/include/clj.lfe") and then call them directly, like this:

(include-lib "lfe/include/clj.lfe")

(-> 2 (+ 2) (=:= 4)) ; 'true

๐Ÿ› ๏ธ Function Macros ๐Ÿ› ๏ธ

  • (defn name [arg ...] {{doc-string}} ...) and (defn {{doc-string}} ([argpat ...] ...)) โ€” define and automatically export a function. Yes, it exports it for you, no extra steps, slay. โœจ
  • (defn- name [arg ...] {{doc-string}} ...) and (defn- {{doc-string}} ([argpat ...] ...)) โ€” equivalent to defun.
  • (fn (arg ...) ...) โ€” equivalent to lambda.

๐Ÿงต Threading Macros ๐Ÿงต

Note: the original versions were copied from Tim Dysingerโ€™s lfesl repo here:

https://github.com/lfex/lfesl/blob/master/include/thread.lfe

They have since been modified to be safely exportable. Iconic glow-up behavior, honestly. ๐Ÿ’…

  • (-> ...) โ€” thread first.

Example usage, demonstrating ordering (and yes, the order is the whole point, bestie):

> (set o '(#(a 1) #(b 2) #(c 3)))
(#(a 1) #(b 2) #(c 3))
> (clj:-> o
>         (++ '(#(d 4)))
>         (++ '(#(e 5)))
>         (++ '(#(f 6))))
(#(a 1) #(b 2) #(c 3) #(d 4) #(e 5) #(f 6))

Note that the use of -> in this example results in each successive value being appended to the input list.

Another example showing how this works:

> (lists:sublist
>   (lists:reverse
>     (lists:sort
>       (lists:merge
>         (string:tokens
>           (string:to_upper "a b c d e")
>           " ")
>         '("X" "F" "L"))))
>   2 3)
("L" "F" "E")

Can be rewritten as this:

> (clj:-> "a b c d e"
>         (string:to_upper)
>         (string:tokens " ")
>         (lists:merge '("X" "F" "L"))
>         (lists:sort)
>         (lists:reverse)
>         (lists:sublist 2 3))
("L" "F" "E")
  • (->> ...) โ€” thread last.

Example usage, demonstrating ordering:

> (set o '(#(a 1) #(b 2) #(c 3)))
(#(a 1) #(b 2) #(c 3))
> (clj:->> o
>          (++ '(#(d 4)))
>          (++ '(#(e 5)))
>          (++ '(#(f 6))))
(#(f 6) #(e 5) #(d 4) #(a 1) #(b 2) #(c 3))

Note that the use of ->> in this example results in each successive value being prepended to the input list. Different direction, different drama. ๐Ÿ’€

Another example showing how this:

> (lists:foldl #'+/2 0
>   (clj:take 10
>     (lists:filter
>       (clj:comp #'clj:even?/1 #'clj:round/1)
>       (lists:map
>         (lambda (x)
>           (math:pow x 2))
>         (clj:seq 42)))))
1540.0

Can be rewritten as this:

> (clj:->> (clj:seq 42)
>          (lists:map (lambda (x) (math:pow x 2)))
>          (lists:filter (clj:comp #'clj:even?/1 #'clj:round/1))
>          (clj:take 10)
>          (lists:foldl #'+/2 0))
1540.0
  • (as-> expr name . sexps) โ€” bind name to expr, evaluate the first sexp in the lexical context of that binding, then bind name to that result, repeating for each successive sexp in sexps, and return the result of the last sexp. Itโ€™s giving โ€œpass the aux, but make it lexical.โ€ ๐ŸŽ€
  • (cond-> expr . clauses) โ€” given an expression and a set of test/sexp pairs, thread x (via ->) through each sexp for which the corresponding test expression is truthy, i.e. neither 'false nor 'undefined. Note that, unlike cond branching, cond-> threading does not short circuit after the first truthy test expression. It keeps going, main character energy. ๐Ÿ‘‘
  • (cond->> expr . clauses) โ€” given an expression and a set of test/sexp pairs, thread x (via ->>) through each sexp for which the corresponding test expression is truthy, i.e. neither 'false nor 'undefined. Note that, unlike cond branching, cond->> threading does not short circuit after the first truthy test expression. Same no-short-circuit vibe, different thread. ๐Ÿ’…
  • (some-> x . sexps) โ€” when x is not 'undefined, thread it into the first sexp (via ->), and when that result is not 'undefined, through the next, etc. Lowkey the โ€œonly if it existsโ€ pipeline. ๐Ÿฆ‹
  • (some->> x . sexps) โ€” when x is not 'undefined, thread it into the first sexp (via ->>), and when that result is not 'undefined, through the next, etc. Same some-> energy, but last-threaded. โœจ

๐ŸŽ€ Conditional Macros ๐ŸŽ€

  • (if-let ((patt test)) then {{else}}) โ€” if test evaluates to anything other than 'false or 'undefined, evaluate then with patt bound to the value of test; otherwise evaluate else, if supplied, else 'undefined.
  • (iff-let ((patt test)) . body) โ€” when test evaluates to anything other than 'false or 'undefined, evaluate body with patt bound to the value of test; otherwise return 'undefined.
  • (condp pred expr . clauses) โ€” given a binary predicate, an expression, and a set of clauses of the form:
test-expr result-expr

test-expr >> result-fn

where result-fn is a unary function, if (pred test-expr expr) returns anything other than 'undefined or 'false, the clause is a match.

If a binary clause matches, return result-expr. If a ternary clause matches, call result-fn with the result of the predicate and return the result.

If no clause matches and a single default expression is given after the clauses, return it. If no default expression is given and no clause matches, throw a no-matching-clause error. No cap, it will absolutely throw. ๐Ÿ˜ญ

  • (if-not test then) and (if-not test then else) โ€” if test evaluates to 'false or 'undefined, evaluate and return then; otherwise else, if supplied, else 'undefined.
  • (iff test . body) โ€” like Clojureโ€™s when. If test evaluates to anything other than 'false or 'undefined, evaluate body in an implicit progn.
  • (when-not test . body) โ€” if test evaluates to 'false or 'undefined, evaluate body in an implicit progn. Otherwise return 'undefined.
  • (not= x), (not= x y), and (not= x y . more) โ€” same as (not (== ...)).

๐ŸŒธ Predicate Macros ๐ŸŒธ

Allowed in guards, unless otherwise stated. Guard-friendly little icons, we love to see it. ๐Ÿซถ

  • (tuple? x) โ€” return 'true if x is a tuple.
  • (atom? x) โ€” return 'true if x is an atom.
  • (binary? x) โ€” return 'true if x is a binary.
  • (bitstring? x) โ€” return 'true if x is a bitstring.
  • (boolean? x) and (bool? x) โ€” return 'true if x is a boolean.
  • (float? x) โ€” return 'true if x is a float.
  • (function? f) and (func? f) โ€” return 'true if x is a function.
  • (function? f n) and (func? f n) โ€” return 'true if f is an n-ary function.
  • (integer? x) and (int? x) โ€” return 'true if x is an integer.
  • (number? x) โ€” return 'true if x is a number.
  • (record? x record-tag) and (record? x record-tag size) โ€” return 'true if x is a tuple and its first element is record-tag. If size is given, check that x is a record-tag record of size size.
N.B. record?/2 may yield unexpected results, due to difference between the Erlang and LFE compilers. As such, whenever possible, prefer record?/3.
  • (reference? x) โ€” return 'true if x is a reference.
  • (map? x) โ€” return 'true if x is a map. Return 'false on versions of Erlang without maps. Respect the old Erlang era, bestie. ๐Ÿ•ฐ๏ธ
  • (undefined? x) and (undef? x) โ€” return 'true if x is the atom 'undefined.
  • (nil? x) โ€” return 'true if x is the atom 'nil or the empty list.
  • (true? x) โ€” return 'true if x is the atom 'true.
  • (false? x) โ€” return 'true if x is the atom 'false.
  • (falsy? x) โ€” return 'true if x is one of the atoms 'false and 'undefined.
  • (odd? x) โ€” return 'true if x is odd.
  • (even? x) โ€” return 'true if x is even.
  • (zero? x) โ€” return 'true if x is zero.
  • (pos? x) โ€” return 'true if x is greater than zero.
  • (neg? x) โ€” return 'true if x is less than zero.
  • (identical? x) โ€” return 'true if x is exactly equal to y.

๐Ÿฆ‹ Other Macros ๐Ÿฆ‹

  • (str x1, x2 ... xn) โ€” given an arbitrary number of arguments, return a string consisting of each of their string representations.
N.B. Because Erlang characters are represented as integers, this will not work for chars, e.g. #\a, which will be presented in the return value as its integer value, i.e. "97".
> (clj:str #\a "bc")
"97bc"
> (clj:str "a" "bc")
"abc"
  • (lazy-seq) and (lazy-seq seq) โ€” return a (possibly infinite) lazy sequence from a given lazy sequence seq, or a finite lazy sequence from given list seq. A lazy sequence is treated as finite if at any iteration it produces the empty list, instead of a cons cell with data as the head and a nullary function for the next iteration as the tail. Itโ€™s giving โ€œinfinite until it says itโ€™s done.โ€ โœจ
  • (conj coll . xs) โ€” conj[oin] a value onto an existing collection. Prepend to a list, append to a tuple, and merge maps. Collection glow-up, no cap. ๐Ÿ’…

๐Ÿ‘‘ Clojure-inspired if Macro ๐Ÿ‘‘

  • (if test then) and (if test then else) โ€” if test evaluates to anything other than 'false or 'undefined, return then; otherwise else, if given, else 'undefined.

๐Ÿงš Function Composition ๐Ÿงš

  • (comp f g) โ€” right to left function composition.
  • (comp fs x) โ€” compose a list of functions fs, right to left, and apply the resulting function to x.
  • (comp f g x) โ€” equivalent to (funcall (comp f g) x).
  • (comp fs) โ€” compose a list of functions fs from right to left.
  • (comp) โ€” equivalent to #'identity/1.

๐ŸŒˆ Usage ๐ŸŒˆ

The following examples assume #'1+/1 is defined:

> (defun 1+ (x) (+ x 1))
1+

> (funcall (clj:comp #'math:sin/1 #'math:asin/1) 0.5)
0.49999999999999994
> (funcall (clj:comp (list #'1+/1 #'math:sin/1 #'math:asin/1) 0.5))
1.5

Or used in another function call:

> (lists:filter (clj:comp #'not/1 #'zero?/1)
    '(0 1 0 2 0 3 0 4))
(1 2 3 4)

The usage above is best when comp will be called by higher-order functions like lists:foldl/3 or lists:filter/2, etc. However, one may also call comp in the following manner, best suited for direct usage:

> (clj:comp #'math:sin/1 #'math:asin/1 0.5)
0.49999999999999994
> (clj:comp (list #'1+/1 #'math:sin/1 #'math:asin/1) 0.5)
1.5

๐Ÿ“ Partial Application ๐Ÿ“

  • (partial f args) and (partial f arg-1) โ€” partially apply f to a given argument arg-1 or list of args. Pre-fill the vibe, call it later. ๐Ÿ’œ

โœจ Usage โœจ

Okay bestie, letโ€™s see this cutie in action. ๐Ÿ’… These examples are literally the whole vibe:

> (set f (clj:partial #'+/2 1))
#Fun<clj.3.121115395>
> (funcall f 2)
3
> (set f (clj:partial #'+/3 1))
#Fun<clj.3.121115395>
> (funcall f '(2 3))
6
> (set f (clj:partial #'+/3 '(2 3)))
#Fun<clj.3.121115395>
> (funcall f 4)
9
> (set f (clj:partial #'+/4 '(2 3)))
#Fun<clj.3.121115395>
> (funcall f '(4 5))
14

Lowkey important: to partially apply a function that expects a list, you have to wrap that list into a (singleton) list. No cap. ๐Ÿซถ

> (set double (clj:partial #'*/2 2))
#Fun<clj.5.16146786>
> (set f (clj:partial #'lists:map/2 double))
#Fun<clj.5.16146786>
> (funcall f '((1 2 3)))
(2 4 6)

๐Ÿฆ‹ Predicate Functions ๐Ÿฆ‹

N.B. These functions may not be used in guards. ๐Ÿšจ
  • (string? data) โ€” Return 'true if data is a flat list of printable characters. ๐Ÿ’–
  • (unicode? data) โ€” Return 'true if data is a flat list of printable Unicode characters. ๐ŸŒˆ
  • (list? data) โ€” Return 'true if data is a list and not a string. ๐ŸŽ€
  • (set? data) โ€” Return 'true if data appears to be a (possibly ordered) set. โœจ
  • (dict? data) โ€” Return 'true if data is a dictionary. ๐Ÿ“š
  • (proplist? lst) โ€” Return 'true if lst is a list where proplist-kv?/1 returns 'true for all elements in lst. ๐Ÿงš
  • (proplist-kv? data) โ€” Return 'true if data is a key/value tuple or an atom. ๐Ÿ”‘
  • (queue? x) โ€” Return 'true if x is a queue. ๐Ÿšถ
  • (empty? x) โ€” Return 'true if x is the empty list, tuple, map, dictionary, queue, or general balanced tree. ๐Ÿซง
  • (every? pred lst) โ€” Return 'true if (pred x) returns 'true for every x in lst. ๐Ÿ’ฏ
  • (all? pred lst) โ€” Return 'true if (pred x) returns 'true for every x in lst. ๐ŸŒŸ
  • (any? pred lst) โ€” Return 'true if (pred x) returns 'true for any x in lst. ๐Ÿ”ฅ
  • (not-any? pred lst) โ€” Return 'false if (pred x) returns 'true for any x in lst. ๐Ÿ’€
  • (element? elem data) โ€” Return 'true if elem is an element of data, where data is a list, set or ordset. ๐ŸŽฏ

๐ŸŒธ Sequence Functions ๐ŸŒธ

  • (seq end) โ€” Equivalent to (seq 1 end). โœจ
  • (seq start end) โ€” Equivalent to (seq start end 1). ๐Ÿ’ซ
  • (seq start end step) โ€” Return a sequence of integers, starting with start, containing the successive results of adding step to the previous element, until end has been reached or password. In the latter case, end is not an element of the sequence. ๐Ÿซถ
  • (next func) โ€” Equivalent to (next func 1 1). ๐ŸŽ€
  • (next func start) โ€” Equivalent to (next func start 1). ๐Ÿฆ‹
  • (next func start step) โ€” Return a nullary function that returns a cons cell with start as the head and a nullary function, (next func (funcall func start step) step), as the tail. The result can be treated as a (possibly infinite) lazy list, which only computes subsequent values as needed. ๐Ÿ”
  • (lazy-seq seq) โ€” Return a lazy sequence (possibly infinite) from given lazy sequence seq or finite lazy sequence from given list seq. Lazy sequence is treated as finite if at any iteration it produces empty list instead of data as its head and nullary function for next iteration as its tail. ๐Ÿงš
  • (cycle lst) โ€” Return a lazy infinite sequence with all elements from a given list lst or another lazy sequence cycled. See next/3 for details on the structure. ๐Ÿ”„
  • (range) โ€” Equivalent to (range 1 1). ๐ŸŒˆ
  • (range start) โ€” Equivalent to (range start 1). โญ
  • (range start step) โ€” Return a lazy list of integers, starting with start and increasing by step. Equivalent to (next #'+/2 start step). See also: next/3. ๐Ÿ“ˆ
  • (drop n lst) โ€” Return a list of all but the first n elements in lst. If n is the atom all, return the empty list. ๐Ÿซณ
  • (drop โ€™all lst) โ€” Return a list of all but the first n elements in lst. If n is the atom all, return the empty list. ๐Ÿ’จ
  • (take n lst) โ€” Given a (possibly lazy) list lst, return a list of the first n elements of lst, or all elements if there are fewer than n. If n is the atom all and lst is a โ€œnormalโ€ list, return lst. ๐Ÿซด
  • (take โ€™all lst) โ€” Given a (possibly lazy) list lst, return a list of the first n elements of lst, or all elements if there are fewer than n. If n is the atom all and lst is a โ€œnormalโ€ list, return lst. ๐ŸŽ
  • (split-at n lst) โ€” Return a tuple of #(,(take n lst) ,(drop n lst)). โœ‚๏ธ
  • (partition n lst) โ€” Equivalent to (partition n n lst). ๐Ÿงฉ
  • (partition n step lst) โ€” Equivalent to (partition n step () lst). ๐Ÿช„
  • (partition n step pad lst) โ€” Return a list of lists of n items each, at offsets step apart. Use the elements of pad as necessary to complete the last partition up to n elements. In case there are not enough padding elements, return a partition with less than n items. ๐ŸŽจ
  • (partition-all n lst) โ€” Equivalent to (partition-all n n lst). ๐Ÿงต
  • (partition-all n step lst) โ€” Return a list of lists like partition/3, possibly including partitions with fewer than n elements at the end. ๐ŸŒท
  • (interleave list-1 list-2) โ€” Return a list of the first element of each list, then the second, etc. ๐Ÿค
  • (get-in data keys) โ€” Equivalent to (get-in data keys 'undefined). ๐Ÿ”
  • (get-in data keys not-found) โ€” Return the value in a nested associative structure, where keys is a list of keys or list indices. Return the atom not-found if the key is not present or index is out of bounds, or the not-found value. ๐Ÿ—๏ธ
  • (reduce func (cons head tail)) โ€” Equivalent to (reduce func head tail). ๐ŸŒ€
  • (reduce func acc lst) โ€” Equivalent to (lists:foldl func acc lst). ๐Ÿงฎ
  • (repeat x) โ€” Return a lazy infinite sequence of xs. See next/3 for details on the structure. ๐Ÿ”‚
  • (repeat n f) โ€” Given a nullary function f, return a list of n applications of f. ๐ŸŽฌ
  • (repeat n x) โ€” Given a term x, return a list of n copies of x. ๐Ÿ‘ฏ

๐Ÿ’œ Other Functions ๐Ÿ’œ

  • (identity x) โ€” Identity function. ๐Ÿชž
  • (constantly x) โ€” Return a unary function that returns x. N.B. This is like Haskellโ€™s const rather than Clojureโ€™s constantly. ๐Ÿงธ
  • (inc x) โ€” Increment x by 1. โž•
  • (dec x) โ€” Decrement x by 1. โž–

๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

Huge shoutout to Tim Dysinger, Duncan McGreggor, and Eric Bailey โ€” icons only. ๐Ÿ’…โœจ


Final cute sign-off: the source/version and date footer says 2015-2016, lfe_clj(3) โ€” and thatโ€™s the whole page, bestie. ๐ŸŽ€๐ŸŒˆ๐Ÿ’–

๐Ÿ“ฆ where this page comes from