Fixing doto
to be API compatible with Clojure's doto
This commit is contained in:
parent
f4b67e8bd8
commit
834b0019a7
@ -563,23 +563,21 @@ del
|
||||
doto
|
||||
----
|
||||
|
||||
`doto` macro is used to make repetitive calls to an object easy. Following
|
||||
example demonstrates this.
|
||||
`doto` macro is used to make a sequence of method calls for an object easy.
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
=> (doto [] (.append 1) (.append 2) .reverse)
|
||||
[2 1]
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
=> (setv collection [])
|
||||
=> (doto collection (.append 1) (.append 2))
|
||||
=> (.append collection 1)
|
||||
=> (.append collection 2)
|
||||
=> (.reverse collection)
|
||||
=> collection
|
||||
[1 2]
|
||||
|
||||
.. code-block:: clj
|
||||
|
||||
=> (setv collection [])
|
||||
=> (.append 1 collection)
|
||||
=> (.append 2 collection)
|
||||
=> collection
|
||||
[1 2]
|
||||
[2 1]
|
||||
|
||||
eval
|
||||
----
|
||||
|
@ -128,14 +128,15 @@
|
||||
|
||||
|
||||
(defmacro doto [form &rest expressions]
|
||||
(setv expressions (iter expressions))
|
||||
(defn build-form [form expression]
|
||||
`(~(first expression) ~form ~@(rest expression)))
|
||||
(setv result `())
|
||||
(for* [expression expressions]
|
||||
(.append result (build-form form expression)))
|
||||
`(do ~@result))
|
||||
|
||||
"Performs a sequence of potentially mutating actions on an initial object, returning the resulting object after the sequence is performed"
|
||||
(setv f (gensym))
|
||||
(defn build-form [expression]
|
||||
(if (isinstance expression HyExpression)
|
||||
`(~(first expression) ~f ~@(rest expression))
|
||||
`(~expression ~f)))
|
||||
`(let [[~f ~form]]
|
||||
~@(map build-form expressions)
|
||||
~f))
|
||||
|
||||
(defmacro ->> [head &rest rest]
|
||||
;; TODO: fix the docstring by someone who understands this
|
||||
|
@ -483,5 +483,9 @@
|
||||
"NATIVE: testing doto macro"
|
||||
(setv collection [])
|
||||
(doto collection (.append 1) (.append 2) (.append 3))
|
||||
(assert-equal collection [1 2 3]))
|
||||
(assert-equal collection [1 2 3])
|
||||
(setv res (doto (set) (.add 2) (.add 1)))
|
||||
(assert-equal res (set [1 2]))
|
||||
(setv res (doto [] (.append 1) (.append 2) .reverse))
|
||||
(assert-equal res [2 1]))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user