From 2b551329f9f622bfa3e2c0cbf4d223bcfa8496f7 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 8 Apr 2017 19:51:06 -0400 Subject: - Improved the implementation of continuations. --- stdlib/test/test/lux/function/cont.lux | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'stdlib/test') diff --git a/stdlib/test/test/lux/function/cont.lux b/stdlib/test/test/lux/function/cont.lux index c2e36a06b..97058c22f 100644 --- a/stdlib/test/test/lux/function/cont.lux +++ b/stdlib/test/test/lux/function/cont.lux @@ -15,10 +15,10 @@ #let [(^open "&/") &;Monad]] ($_ seq (assert "Can run continuations to compute their values." - (n.= sample (&;run (&;@lazy sample)))) + (n.= sample (&;run (&/wrap sample)))) (assert "Can use functor." - (n.= (n.inc sample) (&;run (&/map n.inc (&;@lazy sample))))) + (n.= (n.inc sample) (&;run (&/map n.inc (&/wrap sample))))) (assert "Can use applicative." (n.= (n.inc sample) (&;run (&/apply (&/wrap n.inc) (&/wrap sample))))) @@ -29,10 +29,24 @@ arg (wrap sample)] (wrap (func arg)))))) - ## (assert "Can access current continuation." - ## (n.= (n.dec sample) (&;run (do &;Monad - ## [func (wrap n.inc) - ## _ (&;call/cc (lambda [k] (k (n.dec sample)))) - ## arg (wrap sample)] - ## (wrap (func arg)))))) + (assert "Can use the current-continuation as a escape hatch." + (n.= (n.* +2 sample) + (&;run (do &;Monad + [value (&;call/cc + (lambda [k] + (do @ + [temp (k sample)] + ## If this code where to run, + ## the output would be + ## (n.* +4 sample) + (k temp))))] + (wrap (n.* +2 value)))))) + + (assert "Can use the current-continuation to build a time machine." + (n.= (n.+ +100 sample) + (&;run (do &;Monad + [[restart [output idx]] (&;portal [sample +0])] + (if (n.< +10 idx) + (restart [(n.+ +10 output) (n.inc idx)]) + (wrap output)))))) )) -- cgit v1.2.3