add (picrin control option)

This commit is contained in:
Yuichi Nishiwaki 2015-07-19 15:12:49 +09:00
parent 1c13076f01
commit 43aac4dd29
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,7 @@
CONTRIB_LIBS += $(wildcard contrib/50.option/*.scm)
CONTRIB_TESTS += test-option
test-option: bin/picrin
for test in `ls contrib/50.option/t/*.scm`; do \
$(TEST_RUNNER) "$$test"; \
done

View File

@ -0,0 +1,22 @@
(define-library (picrin control option)
(import (scheme base)
(picrin control)
(picrin procedure))
(define unit identity)
(define (bind m f)
(and m (f m)))
(define-syntax reify
(syntax-rules ()
((_ expr)
(reset (unit expr)))))
(define (reflect m)
(shift k (bind m k)))
(export unit
bind
reify
reflect))

View File

@ -0,0 +1,27 @@
(import (picrin base)
(picrin test)
(picrin control option))
(define phonebook
'(("Bob" . "01788 665242")
("Fred" . "01624 556442")
("Alice" . "01889 985333")
("Jane" . "01732 187565")))
(define nums
'((one . 1) (two . 2) (three . 3) (four . 19)))
(define num-dict
(alist->dictionary nums))
(test '("01889 985333" . 3)
(reify
(let* ((a (reflect (assoc "Alice" phonebook)))
(b (reflect (dictionary-ref num-dict 'three))))
(cons (cdr a) (cdr b)))))
(test '#f
(reify
(let* ((a (reflect (assoc "Alice" phonebook)))
(b (reflect (dictionary-ref num-dict 'five))))
(cons (cdr a) (cdr b)))))