/el-arrows

A set of threading macro's for our favorite operating system

Primary LanguageEmacs Lisp

Arrows

A set of Threading macro’s blatantly rewritten from cl-arrows

Roadmap

Arrows to implement

  • [ ] nil short circuiting arrows, these will return nil if any of the forms return nil.

Reference

Macros

Macro nameDescriptionNickname
arr->argument first. Identical to thread-firsttip
arr->>argument last. Identical to thread-lastspear
arr-<>argument first with placeholdersdiamond wand
arr-<>>argument last with placeholdersdiamond spear

each of them has a fn variant which allows for “curried” like function syntax they take the name of arr-fn-NAME so for example arr-fn-<>>

Functions

Function nameDescriptionarguments
arr-inspecta way to quickly print out the value in a pipeline without disrupting itvalue &optional &key print-fn label

Examples

Simple threading

here we are just pushing a number through a contrived pipeline

(arr-> 8 (1+) (number-to-string) (string-pad 5) (string-pad 9 nil t))
"     9      "

Here we are using ->> to pass this list in as the last argument of these maps and filters.

(arr->> '(1 2 3 4 5)
        (seq-map #'1+)
        (seq-filter #'cl-evenp))
246

placeholder example

If we go back to that last example we see a limitation, we can only thread last, we could not use seq-reduce here as it requires the sequence to be the 2nd argument out of 3 (placing it in the middle). This is where diamond variants come into play.

(arr-<>> '(1 2 3 4 5)
         (seq-map #'1+)
         (seq-filter #'cl-evenp)
         (seq-reduce #'+ <> 0))
12

Here <> is a placeholder meaning our value is passed into that spot, this makes it very easy to compose functions that might not have uniform positioning of the passed argument (like with seq).

fn example

We also provide composition functions which are useful when you want to have a function that represents a set of transformations. this also allows your code to look point free. All arrow macro’s have a fn variant

(require 'arr)

(seq-map (arr-fn-> (1+) (number-to-string)) '(1 2 3))
234

Lambda short hands

as a side effect this allows for short functions without the need for the full lambda syntax. This is not as nice nor as flexible as something like dollar.el but still is worth mentioning IMHO

(seq-map (arr-fn-<> (* <> <>)) '(2 3 4))
4916