A set of Threading macro’s blatantly rewritten from cl-arrows
- [-] Formalise direction
- [ ] find more arrow idea’s (this is now a small obsession)
- [ ] Implement parts from srfi 197
- [ ] Implement arrows from the racket threading library
- [ ] Implement arrows from the swiss arrows library
- [ ] add an inspect function similar to elixirs for easier debugging
- [ ] nil short circuiting arrows, these will return nil if any of the forms return nil.
Macro name | Description | Nickname |
---|---|---|
arr-> | argument first. Identical to thread-first | tip |
arr->> | argument last. Identical to thread-last | spear |
arr-<> | argument first with placeholders | diamond wand |
arr-<>> | argument last with placeholders | diamond 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-<>>
Function name | Description | arguments |
---|---|---|
arr-inspect | a way to quickly print out the value in a pipeline without disrupting it | value &optional &key print-fn label |
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))
2 | 4 | 6 |
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).
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))
2 | 3 | 4 |
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))
4 | 9 | 16 |