forked from okuoku/xitomatl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
curry.sls
41 lines (37 loc) · 1.1 KB
/
curry.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!r6rs
;; Copyright 2009 Derick Eddington. My MIT-style license is in the file named
;; LICENSE from the original collection this file is distributed with.
(library (xitomatl curry)
(export
define/curry
lambda/curry
curry)
(import
(rnrs)
(only (xitomatl define) define/?)
(only (xitomatl predicates) non-negative-integer?))
(define-syntax define/curry
(lambda (stx)
(syntax-case stx ()
((_ (name a ... . r) . body)
(identifier? #'name)
#'(define name
(lambda/curry (a ... . r) . body))))))
(define-syntax lambda/curry
(lambda (stx)
(syntax-case stx ()
((_ (a a* ... . r) . body)
#`(curry
(lambda (a a* ... . r) . body)
#,(length #'(a a* ...))))
((_ . r) ;; zero or "rest"-only arguments
#'(lambda . r)))))
(define/? (curry proc (n non-negative-integer?))
(lambda args
(let ((len (length args)))
(if (>= len n)
(apply proc args)
(curry
(lambda more (apply proc (append args more)))
(- n len))))))
)