# FreeBSD Manual Pages

lfe_clj(3) lfe_clj(3)NAMEclj - LFE Clojure interface library.SYNOPSISThis module provides Clojure-inpired functions and macros for use in LFE.EXPORTSN.B. Instead of making fully-qualified calls to the macros exported fromclj, you may (include-lib "lfe/include/clj.lfe") and then call them directly, e.g. (include-lib "lfe/include/clj.lfe") (-> 2 (+ 2) (=:= 4)) ; 'trueFunctionMacros(defnname[arg...]{{doc-string}}...)(defn{{doc-string}}([argpat...]...))Define and automatically export a function.(defn-name[arg...]{{doc-string}}...)(defn-{{doc-string}}([argpat...]...))Equivalent todefun.(fn(arg...)...)Equivalent to lambda.ThreadingMacrosNote: 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.(->...)Thread first. 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)))) (#(a 1) #(b 2) #(c 3) #(d 4) #(e 5) #(f 6)) Note that the use of-> in this example results in each successive val- ue beingappendedto 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 beingprependedto the input list. 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->exprname.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 succes- sive sexp in sexps, returning the result of the last sexp.(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, unlikecondbranching,cond-> threading does not short circuit after the first truthy test expression.(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, unlikecondbranching,cond->> threading does not short circuit after the first truthy test expression.(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.(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.ConditionalMacros(if-let((patttest))then{{else}})If test evaluates to anything other than 'false or 'undefined, evaluate then with patt bound to the value of test, otherwise else, if supplied, else 'undefined.(iff-let((patttest)).body)When test evaluates to anything other than 'false or 'undefined, evalu- ate body with patt bound to the value of test, otherwise return 'unde- fined.(condppredexpr.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.(if-nottestthen)(if-nottestthenelse)If test evaluates to 'false or 'undefined, evaluate and return then, otherwise else, if supplied, else 'undefined.(ifftest.body)Like Clojure's when. If test evaluates to anything other than 'false or 'undefined, evaluate body in an implicit progn.(when-nottest.body)If test evaluates to 'false or 'undefined, evaluate body in an implicit progn. Otherwise return 'undefined.(not=x)(not=xy)(not=xy.more)Same as (not (== ...)).PredicateMacrosAllowed in guards, unless otherwise stated.(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)(bool?x)Return 'true if x is a boolean.(float?x)Return 'true if x is a float.(function?f)(func?f)Return 'true if x is a function.(function?fn)(func?fn)Return 'true if f is an n-ary function.(integer?x)(int?x)Return 'true if x is an integer.(number?x)Return 'true if x is a number.(record?xrecord-tag)(record?xrecord-tagsize)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?/2may yield unexpected results, due to difference between the Erlang and LFE compilers. As such, whenever possible, preferrecord?/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 with- out maps.(undefined?x)(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.OtherMacros(strx1,x2...xn)Given 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)(lazy-seqseq)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, in- stead of a cons cell with data as the head and a nullary function for the next iteration as the tail.(conjcoll.xs)conj[oin] a value onto an existing collection. Prepend to a list, ap- pend to a tuple, and merge maps.Clojure-inspiredifMacro(iftestthen)(iftestthenelse)If test evaluates to anything other than 'false or 'undefined, return then, otherwise else, if given, else 'undefined.FunctionComposition(compfg)Right to left function composition.(compfsx)Compose a list of functions fs, right to left, and apply the resulting function to x.(compfgx)Equivalent to (funcall (comp f g) x).(compfs)Compose a list of functions fs from right to left.(comp)Equivalent to #'identity/1.UsageThe 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 whencompwill be called by higher-order func- tions likelists:foldl/3orlists:filter/2, etc. However, one may also callcompin 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.5PartialApplication(partialfargs)(partialfarg-1)Partially apply f to a given argument arg-1 or list of args.Usage> (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 Note that to partially apply a function that expects a list, you must wrap said list into a (singleton) list. > (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)PredicateFunctionsN.B. These functions maynotbe 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 is 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 whereproplist-kv?/1returns 'true for all elements in lst.(proplist-kv?data)Return 'true if a 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?predlst)(all?predlst)Return 'true if (pred x) returns 'true for every x in lst.(any?predlst)Return 'true if (pred x) returns 'true for any x in lst.(not-any?predlst)Return 'false if (pred x) returns 'true for any x in lst.(element?elemdata)Return 'true if elem is an element of data, where data is a list, set or ordset.SequenceFunctions(seqend)Equivalent to (seq 1 end).(seqstartend)Equivalent to (seq start end 1).(seqstartendstep)Return a sequence of integers, starting with start, containing the suc- cessive 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.(nextfunc)Equivalent to (next func 1 1).(nextfuncstart)Equivalent to (next func start 1).(nextfuncstartstep)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 subseqeuent values as needed.(lazy-seqseq)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.(cyclelst)Return a lazy infinite sequence with all elements from a given list lst or another lazy sequence cycled. Seenext/3for details on the structure.(range)Equivalent to (range 1 1).(rangestart)Equivalent to (range start 1).(rangestartstep)Return a lazy list of integers, starting with start and increasing by step. Equivalent to (next #'+/2 start step). See also:next/3.(dropnlst)(drop'alllst)Return a list of all but the first n elements in lst. If n is the atom all, return the empty list.(takenlst)(take'alllst)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-atnlst)Return a tuple of `#(,(take n lst) ,(drop n lst)).(partitionnlst)Equivalent to (partition n n lst).(partitionnsteplst)Equivalent to (partition n step () lst).(partitionnsteppadlst)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 el- ements. In case there are not enough padding elements, return a pari- tion with less than n items.(partition-allnlst)Equivalent to (partition-all n n lst).(partition-allnsteplst)Return a list of lists likepartition/3, possibly including partitions with fewer than n elements at the end.(interleavelist-1list-2)Return a list of the first element of each list, then the second, etc.(get-indatakeys)Equivalent to (get-in data keys 'undefined).(get-indatakeysnot-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.(reducefunc(consheadtail))Equivalent to (reduce func head tail).(reducefuncacclst)Equivalent to (lists:foldl func acc lst).(repeatx)Return a lazy infinite sequence of xs. Seenext/3for details on the structure.(repeatnf)Given a nullary function f, return a list of n applications of f.(repeatnx)Given a term x, return a list of n copies of x.OtherFunctions(identityx)Identity function.(constantlyx)Return a unary function that returns x. N.B. This is like Haskell's const rather than Clojure's constantly.(incx)Increment x by 1.(decx)Decrement x by 1.AUTHORSTim Dysinger, Duncan McGreggor, Eric Bailey. 2015-2016 lfe_clj(3)

NAME | SYNOPSIS | EXPORTS | AUTHORS

Want to link to this manual page? Use this URL:

<https://www.freebsd.org/cgi/man.cgi?query=lfe_clj&sektion=3&manpath=FreeBSD+12.1-RELEASE+and+Ports>