-
Notifications
You must be signed in to change notification settings - Fork 0
/
chicken-arrows.scm
42 lines (34 loc) · 1009 Bytes
/
chicken-arrows.scm
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
42
(module chicken-arrows
(reduce
insert-first
insert-last
simple-inserter
->
->>)
(import scheme
procedural-macros)
(define (reduce proc lst)
(define (aux lst acc)
(if (null? lst)
acc
(aux (cdr lst) (proc acc (car lst)))))
(if (null? lst)
#f
(aux (cdr lst) (car lst))))
(define (insert-first arg surround)
(cons (car surround) (cons arg (cdr surround))))
(define (insert-last arg surround)
(append surround (list arg)))
(define (simple-inserter insert-proc)
(lambda (acc next)
(if (pair? next)
(insert-proc acc next)
(list next acc))))
;; Insert the first sexp of REST as the first argument of the second sexp of
;; REST, the result into the next etc, before evaluation.
(define-macro (-> . rest)
(eval `(reduce (simple-inserter insert-first) ',rest)))
;; Like -> but inserts as the last argument instead of the first.
(define-macro (->> . rest)
(eval `(reduce (simple-inserter insert-last) ',rest)))
) ; module