clj-arrow is a small library for combining functions.
In leiningen add [clj-arrow "1.0.3-SNAPSHOT"]
to dependencies. Otherwise, put the
jar
on the classpath. Only clojure is required as a dependency.
First use
, or require
clj-arrow.arrow. In this namespace these functions exist (see docstrings for usage):
>>>
- create a flow of functions to the right.<<<
- create a flow of functions to the left.fst
- apply a function to the first value of a pair.snd
- apply a function to the second value of a pair.***
- apply two functions to respective values of a pair.&&&
- same as***
, but takes a single value which is cloned to a pair.|||
- anif
conditional statement for functions.
***-multithread
- same as***
but multithreads.&&&-multithread
- same as&&&
but multithreads.
Using ***-multithread
or &&&-multithread
work can be done in parallel.
(time ((&&& #(Thread/sleep %) #(Thread/sleep %)) 1000))
; "Elapsed time: 2006.765373 msecs"
;=> [nil nil]
(time ((&&&-multithread #(Thread/sleep %) #(Thread/sleep %)) 1000))
; "Elapsed time: 1004.629826 msecs"
;=> [nil nil]
While I only mention functions as arguments to the functions above, they could be used just as well with any type of arrow (functions can be seen as a subset of arrows).
This is a delicate subject, which I must admit that I don't quite understand myself.
However, for clj-arrow it's simple: any /thing/ (class, record ...) implementing
the clj-arrow.arrow/Arrow
protocol (or the underlying interface) is an arrow. The protocol defines the following functions:
run
- function to execute the arrow.>>>_int
- the arrow's implementation of>>>
, takes only two arguments.fst_int
- the arrow's implementation offst
.
An arrow implementing these functions must also not make any assumptions of what type of arrow is sent as the second argument to the >>>_int
function.
If these conditions are met, the arrow will work with all the functions described in Usage.
Copyright (c) 2011 Jonathan Fischer Friberg
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
-
The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
-
Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
-
This notice may not be removed or altered from any source distribution.