A collection of arrow macros.
Usage
Installation
http://clojars.org/swiss-arrows
Getting Started
(ns example.core
(:require [swiss.arrows :refer :all]))
Overview
–<> , –<>> The Diamond Wand, Diamond Spear
some-<> , some-<>> The Nil-shortcutting Diamond Wand
apply-> , apply->> Applicative arrows (WIP)
-!> , -!>> , -!<> Non-updating Arrows
<<- The Back Arrow
–< , –<:p The Furcula, Parallel Furcula
–<< , –<<:p The Trystero Furcula, Parallel Trystero Furcula
–<>< , –<><:p The Diamond Fishing Rod, Parallel Diamond Fishing Rod
A Generalization of the Arrow
The Diamond Wand – similar to -> or ->> except that the flow of execution
is passed through specified <> positions in each of the forms.
If no <> position marker is found in a form within the Diamond Wand –<>, the
default positioning behavior follows that of the -> macro. Likewise, if no
position is specified in a form within the Diamond Spear –<>>, the default is
has the positioning semantics of ->>.
Some examples:
Applicative Arrows (WIP)
“Since I noticed that the “nil-shortcutting diamond wand” acts like the Maybe monad, I started getting the feeling that the swiss arrows could be generalized over all monads. Since the archetype of monads is the sequence monad and the mother operator, bind, for sequence is (apply concat (map my-foo your-sequence-monad)) I started to see chaining of apply-concat as a start toward monadic swiss arrows :)
Also, Wolfram / Mathematica have a host of operators that thread and merge Apply around expressions (see http://reference.wolfram.com/mathematica/ref/Apply.html). Mathematica was designed before monads were formalized in programming languages, but their precursors are all over Mathematica, for instance in the frequent use of Apply. [edit: I should add, contextually, that I am a big admirer of Mathematica just as a programming language, never mind its huge knowledge base of math. I often refer to it for ideas to bring to Clojure and other languages.]
As for the first argument being special, that didn’t seem out-of-step with the other arrows, none of which, for instance, can take an expression with an angle-hole “<>” in the first position, and all of which take expressions-with-angle-holes in every slot except the first, modulo the defaults. The defaults are abundant and require the same kind of mental substitution that apply->> etc. would require, so the overall design has established the precedent of “implicits.””
Non-updating Arrows (for unobtrusive side-effecting)
It is often expedient, in particular for debugging and logging, to stick a
side-effecting form midway in the pipeline of an arrow. One solution is a
pair of utility macros “with” and “within”
A caveat of that approach is that having too many anaphoric macros can lead
to messy code, and they don’t nest (eg. #( ) reader macro), and so on.
Non-updating arrows offer an adequately elegant alternative solution for
inserting side-action in what would otherwise be a difficult situation.
As a bonus, the arrow-style macros (including the wand– the <> does not refer
to any sort of binding, and does not act recursively so it is not anaphoric
in the usual sense, if at all) do not rely on symbol capture, and therefore
are arbitrarily nestable.
The Back Arrow
This is simply ->> with its arguments reversed, convenie
>>>
>>>>>>>>>>>>>>>>>>>>>>
Read More