Skip to content

Commit 6c31cc4

Browse files
authored
Merge pull request #248 from carloshernandez2/test-new-cats-mlet-macro-2
Support missing destructuring scenarios in mlet for version 2.4.3
2 parents 97be2c4 + c565dfc commit 6c31cc4

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

project.clj

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
(defproject funcool/cats "2.4.3-beta.1"
1+
(defproject funcool/cats "2.4.3-beta.2"
22
:description "Category Theory abstractions for Clojure"
33
:url "https://github.com/funcool/cats"
44
:license {:name "BSD (2 Clause)"
55
: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"]
77
[org.clojure/clojurescript "1.10.439" :scope "provided"]
88
[org.clojure/core.async "0.4.474" :scope "provided"]
99
[org.clojure/test.check "0.9.0" :scope "provided"]

src/cats/core.cljc

+21-15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
:clj
3535
(:require [cats.protocols :as p]
3636
[clojure.set]
37+
[clojure.walk :as walk]
3738
[cats.context :as ctx]))
3839
(:refer-clojure :exclude [filter sequence unless when for]))
3940

@@ -229,21 +230,26 @@
229230
;; Monadic Let Macro
230231
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
231232

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)]))))
247253

248254
#?(:clj
249255
(defmacro mlet

test/cats/core_spec.cljc

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030

3131
(t/deftest mlet-tests
3232
(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)))))))
3738

3839
(t/testing "Support :when guards inside its bindings"
3940
(t/is (= (maybe/nothing)

0 commit comments

Comments
 (0)