Permalink
Browse files

docstrings

  • Loading branch information...
1 parent d05876f commit 6d0aa335d4ba84b5ba151d07c4ead9245ed0c4e3 @odyssomay committed Jul 6, 2011
Showing with 62 additions and 13 deletions.
  1. +62 −13 src/Hafni/arrow.clj
View
@@ -7,9 +7,6 @@
(defn swap [[x y]]
[y x])
-(defn- iarr [a f]
- (assoc a :f f))
-
(defmacro flow [& flows]
`(-> ~(first flows) ~@(partition 2 (rest flows))))
@@ -29,9 +26,10 @@
(fst_int [this] (assoc this :f (fn [[x y]] [(this x) y]))))
(defn arr [f]
- (if (isa? (class f) (class (Arrow. identity)))
- f
- (Arrow. f)))
+ f)
+; (if (isa? (class f) (class (Arrow. identity)))
+; f
+; (Arrow. f)))
(extend-type clojure.lang.IFn
Arrow_p
@@ -41,28 +39,79 @@
(fn [[x y]]
[(this x) y])))
-(defn >>> [& arrs]
+(defn >>>
+ "Create an arrow where the first
+ arrow is applied to the input, the
+ second arrow is applied to the result,
+ the third arrow is applied to the
+ second result, and so on...
+
+Example:
+ ((>>> inc inc inc) 0)
+=> 3"
+ [& arrs]
(case (count arrs)
1 (first arrs)
2 (>>>_int (first arrs) (second arrs))
(>>>_int (first arrs) (apply >>> (rest arrs)))))
-(defn <<< [& arrs]
+(defn <<<
+ "The reverse of >>>"
+ [& arrs]
(apply >>> (reverse arrs)))
-(defn fst [arr]
+(defn fst
+ "Create an arrow that takes a pair
+ and applies arr on the first value.
+Example:
+ ((fst inc) [1 1])
+=> [2 1]"
+ [arr]
(fst_int arr))
-(defn snd [arr]
+(defn snd
+ "As fst, but applies arr on the
+ second value.
+Example:
+ ((fst inc) [1 1])
+=> [1 2]"
+ [arr]
(>>> swap (fst arr) swap))
-(defn *** [arr1 arr2]
+(defn ***
+ "Create an arrow that takes a pair,
+ applies arr1 on the first value
+ and applies arr2 on the second value.
+Example:
+ ((*** inc dec) [1 1])
+=> [2 0]"
+ [arr1 arr2]
(>>> (fst arr1) (snd arr2)))
-(defn &&& [arr1 arr2]
+(defn &&&
+ "As *** but takes a single value
+ and clones it.
+Example:
+ ((*** inc dec) 1)
+=> [2 0]"
+ [arr1 arr2]
(>>> clone (*** arr1 arr2)))
-(defn |||
+(defn |||
+ "Create an arrow that takes a pair.
+ arr1 will be applied to the first value,
+ if the result is truthy the pair with the
+ resulting value is sent to arr2, if falsey
+ it is sent to arr3.
+
+ The two arrow version has arr3 as an arrow
+ that always returns nil.
+
+Example:
+ ((||| nil? first second) [0 1])
+=> 0
+ ((||| nil? first second) [nil 1])
+=> 1"
([arr1 arr2]
(||| arr1 arr2 (constantly nil)))
([arr1 arr2 arr3]

0 comments on commit 6d0aa33

Please sign in to comment.