Merge pull request #479 from paultag/paultag/feature/curry
add curry contrib module
This commit is contained in:
commit
aefea557cb
12
eg/curry/ski.hy
Normal file
12
eg/curry/ski.hy
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
(require hy.contrib.curry)
|
||||||
|
|
||||||
|
|
||||||
|
(defnc s [x y z] ((x z) (y z))) ; λxyz.xz(yz)
|
||||||
|
(defnc k [x] (fn [y] x)) ; λx.λy.x
|
||||||
|
(defnc i [x] x) ;; λx.x
|
||||||
|
|
||||||
|
(defnc succ [n] (+ n 1))
|
||||||
|
|
||||||
|
|
||||||
|
(print (((((s ((((k s) k) i) i)) (i i)) ((i (i i))
|
||||||
|
((((k s) i) ((s (k s)) k)) i))) succ) 0))
|
20
hy/contrib/curry.hy
Normal file
20
hy/contrib/curry.hy
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
(import inspect functools sys)
|
||||||
|
|
||||||
|
|
||||||
|
(defn curry [func]
|
||||||
|
(let [[sig (.getargspec inspect func)]
|
||||||
|
[count (len sig.args)]]
|
||||||
|
|
||||||
|
(fn [&rest args]
|
||||||
|
(if (< (len args) count)
|
||||||
|
(apply functools.partial (+ [(curry func)] (list args)))
|
||||||
|
(apply func args)))))
|
||||||
|
|
||||||
|
|
||||||
|
(defmacro fnc [args &rest body]
|
||||||
|
`(do (import hy.contrib.curry)
|
||||||
|
(with-decorator hy.contrib.curry.curry (fn [~@args] ~@body))))
|
||||||
|
|
||||||
|
|
||||||
|
(defmacro defnc [name args &rest body]
|
||||||
|
`(def ~name (fnc [~@args] ~@body)))
|
@ -213,7 +213,7 @@ def reader_macroexpand(char, tree, module_name):
|
|||||||
"""Expand the reader macro "char" with argument `tree`."""
|
"""Expand the reader macro "char" with argument `tree`."""
|
||||||
load_macros(module_name)
|
load_macros(module_name)
|
||||||
|
|
||||||
if not char in _hy_reader[module_name]:
|
if char not in _hy_reader[module_name]:
|
||||||
raise HyTypeError(
|
raise HyTypeError(
|
||||||
char,
|
char,
|
||||||
"`{0}' is not a reader macro in module '{1}'".format(
|
"`{0}' is not a reader macro in module '{1}'".format(
|
||||||
|
@ -19,3 +19,4 @@ from .native_tests.contrib.loop import * # noqa
|
|||||||
from .native_tests.contrib.meth import * # noqa
|
from .native_tests.contrib.meth import * # noqa
|
||||||
from .native_tests.contrib.walk import * # noqa
|
from .native_tests.contrib.walk import * # noqa
|
||||||
from .native_tests.contrib.multi import * # noqa
|
from .native_tests.contrib.multi import * # noqa
|
||||||
|
from .native_tests.contrib.curry import * # noqa
|
||||||
|
13
tests/native_tests/contrib/curry.hy
Normal file
13
tests/native_tests/contrib/curry.hy
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
(require hy.contrib.curry)
|
||||||
|
|
||||||
|
|
||||||
|
(defnc s [x y z] ((x z) (y z))) ; λxyz.xz(yz)
|
||||||
|
(defnc k [x] (fn [y] x)) ; λx.λy.x
|
||||||
|
(defnc i [x] x) ;; λx.x
|
||||||
|
|
||||||
|
(defnc succ [n] (+ n 1))
|
||||||
|
|
||||||
|
|
||||||
|
(defn test-curry []
|
||||||
|
(assert (= 16 (((((s ((((k s) k) i) i)) (i i)) ((i (i i))
|
||||||
|
((((k s) i) ((s (k s)) k)) i))) succ) 0))))
|
Loading…
Reference in New Issue
Block a user