File tree 3 files changed +28
-21
lines changed
3 files changed +28
-21
lines changed Original file line number Diff line number Diff line change 1
- (defproject funcool /cats " 2.4.3-beta.1 "
1
+ (defproject funcool /cats " 2.4.3-beta.2 "
2
2
:description " Category Theory abstractions for Clojure"
3
3
:url " https://github.com/funcool/cats"
4
4
:license {:name " BSD (2 Clause)"
5
5
:url " http://opensource.org/licenses/BSD-2-Clause" }
6
- :dependencies [[org.clojure/clojure " 1.9 .0" :scope " provided" ]
6
+ :dependencies [[org.clojure/clojure " 1.10 .0" :scope " provided" ]
7
7
[org.clojure/clojurescript " 1.10.439" :scope " provided" ]
8
8
[org.clojure/core.async " 0.4.474" :scope " provided" ]
9
9
[org.clojure/test.check " 0.9.0" :scope " provided" ]
Original file line number Diff line number Diff line change 34
34
:clj
35
35
(:require [cats.protocols :as p]
36
36
[clojure.set]
37
+ [clojure.walk :as walk]
37
38
[cats.context :as ctx]))
38
39
(:refer-clojure :exclude [filter sequence unless when for ]))
39
40
229
230
; ; Monadic Let Macro
230
231
; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231
232
232
- (defn- flatten-bindings
233
- [bindings]
234
- (filterv symbol? (tree-seq coll? seq bindings)))
235
-
236
- (defn- parse-step
237
- [[l r] fn-bindings fn-sym]
238
- (case l
239
- :let
240
- (let [normalized-bindings (flatten-bindings (conj fn-bindings (take-nth 2 r)))]
241
- [`(let ~r (~fn-sym ~@normalized-bindings)) normalized-bindings])
242
- :when
243
- (let [normalized-bindings (flatten-bindings fn-bindings)]
244
- [`(bind (guard ~r) (partial ~fn-sym ~@normalized-bindings)) (conj normalized-bindings '_)])
245
- (let [normalized-bindings (flatten-bindings fn-bindings)]
246
- [`(bind ~r (partial ~fn-sym ~@normalized-bindings)) (conj normalized-bindings l)])))
233
+ #?(:clj
234
+ (defn- extract-bound-symbols
235
+ [bindings]
236
+ (->> bindings
237
+ (walk/prewalk #(if (map? %) (dissoc (update % :keys (partial map (comp symbol name))) :or ) %))
238
+ (tree-seq coll? seq)
239
+ (filterv #(and (symbol? %) (not= '& %))))))
240
+
241
+ #?(:clj
242
+ (defn- parse-step
243
+ [[l r] fn-bindings fn-sym]
244
+ (case l
245
+ :let
246
+ (let [normalized-bindings (extract-bound-symbols (conj fn-bindings (take-nth 2 r)))]
247
+ [`(let ~r (~fn-sym ~@normalized-bindings)) normalized-bindings])
248
+ :when
249
+ (let [normalized-bindings (extract-bound-symbols fn-bindings)]
250
+ [`(bind (guard ~r) (partial ~fn-sym ~@normalized-bindings)) (conj normalized-bindings '_)])
251
+ (let [normalized-bindings (extract-bound-symbols fn-bindings)]
252
+ [`(bind ~r (partial ~fn-sym ~@normalized-bindings)) (conj normalized-bindings l)]))))
247
253
248
254
#?(:clj
249
255
(defmacro mlet
Original file line number Diff line number Diff line change 30
30
31
31
(t/deftest mlet-tests
32
32
(t/testing " Support regular let bindings inside mlet"
33
- (t/is (= (maybe/just 2 )
34
- (m/mlet [{i :i j :j } (maybe/just {:i 1 :j 0 })
35
- :let [i (inc i)]]
36
- (m/return (+ i j))))))
33
+ (t/is (= (maybe/just 20 )
34
+ (m/mlet [[{:n/keys [a b c] y2 :i j :j :or {c 'yes}}] (maybe/just [{:i 1 :j 1 :n/a 2 :n/b 3 }])
35
+ :let [[[x1 y1] [x2 & [y2]] & remaining :as all-vec] (sorted-map j a b 4 )]
36
+ {age :age :keys [m/w ::m/z :zero ] :or {z 5 w 5 } :as all-map} (m/return {:age (+ x1 x2 y1 y2) :zero 0 })]
37
+ (m/return (when (= c 'yes) (+ zero w z age)))))))
37
38
38
39
(t/testing " Support :when guards inside its bindings"
39
40
(t/is (= (maybe/nothing )
You can’t perform that action at this time.
0 commit comments