Skip to content

Commit 07a2a5f

Browse files
author
Alejandro Gómez
committed
Extend set type with every protocol
1 parent 0b6a26e commit 07a2a5f

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/cljx/cats/types.cljx

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns cats.types
22
"Monadic types definition."
3-
(:require [cats.protocols :as proto]))
3+
(:require [clojure.set :as s]
4+
[cats.protocols :as proto]))
45

56
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
67
;; Either
@@ -194,6 +195,8 @@
194195
;; Clojure(Script) types
195196
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196197

198+
; TODO: PersistenList
199+
; TODO: document
197200
(extend-type #+clj clojure.lang.LazySeq
198201
#+cljs cljs.core.LazySeq
199202
proto/Functor
@@ -210,6 +213,7 @@
210213
(bind [self f]
211214
(apply concat (map f self))))
212215

216+
; TODO: test & document
213217
(extend-type #+clj clojure.lang.PersistentVector
214218
#+cljs cljs.core.PersistentVector
215219
proto/Functor
@@ -233,6 +237,32 @@
233237
proto/MonadPlus
234238
(mplus [mv mv'] (into mv mv')))
235239

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+
236266
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
237267
;; Pair (State monad related)
238268
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

tests/tests_cats.cljx

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646
[[1 3] [1 4] [2 3] [2 4]])))
4747
(testing "It works with lazy seqs"
4848
(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]})))
5053
(testing "It works with Maybe values"
5154
(is (= (m/m-sequence [(t/just 2) (t/just 3)])
5255
(t/just [2 3])))

0 commit comments

Comments
 (0)