File tree 2 files changed +35
-2
lines changed
2 files changed +35
-2
lines changed Original file line number Diff line number Diff line change 1
1
(ns cats.types
2
2
" Monadic types definition."
3
- (:require [cats.protocols :as proto]))
3
+ (:require [clojure.set :as s]
4
+ [cats.protocols :as proto]))
4
5
5
6
; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6
7
; ; Either
194
195
; ; Clojure(Script) types
195
196
; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196
197
198
+ ; TODO: PersistenList
199
+ ; TODO: document
197
200
(extend-type #+clj clojure.lang.LazySeq
198
201
#+cljs cljs.core.LazySeq
199
202
proto/Functor
210
213
(bind [self f]
211
214
(apply concat (map f self))))
212
215
216
+ ; TODO: test & document
213
217
(extend-type #+clj clojure.lang.PersistentVector
214
218
#+cljs cljs.core.PersistentVector
215
219
proto/Functor
233
237
proto/MonadPlus
234
238
(mplus [mv mv'] (into mv mv')))
235
239
240
+ ; TODO: test & document
241
+ (extend-type #+clj clojure.lang.PersistentHashSet
242
+ #+cljs cljs.core.PersistentHashSet
243
+ proto/Functor
244
+ (fmap [self f]
245
+ (set (map f self)))
246
+
247
+ proto/Applicative
248
+ (pure [_ v] #{v})
249
+
250
+ (fapply [self av]
251
+ (set (for [f self
252
+ v av]
253
+ (f v))))
254
+
255
+ proto/Monad
256
+ (bind [self f]
257
+ (apply s/union (map f self)))
258
+
259
+ proto/MonadZero
260
+ (mzero [_] #{})
261
+
262
+ proto/MonadPlus
263
+ (mplus [mv mv']
264
+ (s/union mv mv')))
265
+
236
266
; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237
267
; ; Pair (State monad related)
238
268
; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Original file line number Diff line number Diff line change 46
46
[[1 3 ] [1 4 ] [2 3 ] [2 4 ]])))
47
47
(testing " It works with lazy seqs"
48
48
(is (= (m/m-sequence [(lazy-seq [1 2 ]) (lazy-seq [3 4 ])])
49
- [[1 3 ] [1 4 ] [2 3 ] [2 4 ]])))
49
+ '([1 3 ] [1 4 ] [2 3 ] [2 4 ]))))
50
+ (testing " It works with sets"
51
+ (is (= (m/m-sequence [#{1 2 } #{3 4 }])
52
+ #{[1 3 ] [1 4 ] [2 3 ] [2 4 ]})))
50
53
(testing " It works with Maybe values"
51
54
(is (= (m/m-sequence [(t/just 2 ) (t/just 3 )])
52
55
(t/just [2 3 ])))
You can’t perform that action at this time.
0 commit comments