-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
categories firs implementation and Fn instance
- Loading branch information
1 parent
acfb55f
commit d119f0b
Showing
4 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(ns clj-control.category) | ||
|
||
(defprotocol Category | ||
"class Category cat where | ||
id ::cat a a | ||
(.) :: cat b c -> cat a b -> cat a c" | ||
(c-id [this]) | ||
(c-comp [this c])) | ||
|
||
|
||
(defn >>> | ||
"(>>>) :: Category cat => cat a b -> cat b c -> cat a c | ||
Left-to-right composition " | ||
([cab cbc] | ||
(c-comp cab cbc)) | ||
([cab cbc & args] | ||
(if (empty? args) | ||
(c-comp cab cbc) | ||
(apply >>> (cons (c-comp cab cbc) args))))) | ||
|
||
|
||
(defn <<< | ||
"(<<<) :: Category cat => cat b c -> cat a b -> cat a c | ||
Right-to-left composition" | ||
([cbc cab] | ||
(c-comp cab cbc)) | ||
([cbc cab & args] | ||
(apply >>> (reverse (flatten (list [cbc cab] args)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(ns clj-control.test.category | ||
(:use [clj-control.category] :reload) | ||
(:use [clj-control.core] :reload) | ||
(:use [clj-control.utils] :reload) | ||
(:use [clojure.test])) | ||
|
||
|
||
(deftest functions-should-work-with-the->>>-combinator | ||
(is (= ((>>> inc inc (partial * 2) inc inc inc (partial * 3)) 3) | ||
39))) | ||
|
||
(deftest functions-should-work-with-the-<<<-combinator | ||
(is (= ((<<< inc inc (partial * 2) inc inc inc (partial * 3)) 3) | ||
26))) |