Merge pull request #893 from gilch/xi-forms

xi parameter may appear in function position
This commit is contained in:
Gregor Best 2015-08-12 17:00:44 +02:00
commit ead65b646b
3 changed files with 8 additions and 4 deletions

View File

@ -231,7 +231,7 @@ Returns a function which applies several forms in series from left to right. The
xi xi
== ==
Usage ``(xi function body ...)`` Usage ``(xi body ...)``
Returns a function with parameters implicitly determined by the presence in the body of xi parameters. An xi symbol designates the ith parameter (1-based, e.g. x1, x2, x3, etc.), or all remaining parameters for xi itself. This is not a replacement for lambda. The xi forms cannot be nested. Returns a function with parameters implicitly determined by the presence in the body of xi parameters. An xi symbol designates the ith parameter (1-based, e.g. x1, x2, x3, etc.), or all remaining parameters for xi itself. This is not a replacement for lambda. The xi forms cannot be nested.

View File

@ -122,7 +122,7 @@
"Returns a function which is the composition of several forms." "Returns a function which is the composition of several forms."
`(fn [var] (ap-pipe var ~@forms))) `(fn [var] (ap-pipe var ~@forms)))
(defmacro xi [function &rest body] (defmacro xi [&rest body]
"Returns a function with parameters implicitly determined by the presence in "Returns a function with parameters implicitly determined by the presence in
the body of xi parameters. An xi symbol designates the ith parameter the body of xi parameters. An xi symbol designates the ith parameter
(1-based, e.g. x1, x2, x3, etc.), or all remaining parameters for xi itself. (1-based, e.g. x1, x2, x3, etc.), or all remaining parameters for xi itself.
@ -143,5 +143,5 @@
~@(if (in 'xi flatbody) ~@(if (in 'xi flatbody)
'(&rest xi) '(&rest xi)
'())] '())]
(~function ~@body))) (~@body)))

View File

@ -134,4 +134,8 @@
(assert-equal ((xi identity [x3 x1]) 1 2 3) [3 1]) (assert-equal ((xi identity [x3 x1]) 1 2 3) [3 1])
;; test nesting ;; test nesting
(assert-equal ((xi identity [x1 (, x2 [x3] "Hy" [xi])]) 1 2 3 4 5) (assert-equal ((xi identity [x1 (, x2 [x3] "Hy" [xi])]) 1 2 3 4 5)
[1 (, 2 [3] "Hy" [(, 4 5)])])) [1 (, 2 [3] "Hy" [(, 4 5)])])
;; test arg as function
(assert-equal ((xi x1 2 4) +) 6)
(assert-equal ((xi x1 2 4) -) -2)
(assert-equal ((xi x1 2 4) /) 0.5))