on master help sign in
Simplify Trampoline datatype.
commitb47166144d33a330cbfbf7e6a2b9330aff9d1de2 authorMax New <maxsnew@gmail.com> dateMon 20 Jan 2014, 14:38 (13 years ago) parent84aab58
2 files changed+8−9
libraries/Native/Trampoline.js+5−5 libraries/Trampoline.elm+3−4
libraries/Native/Trampoline.js+5−5
@@ -10,11 +10,11 @@ Elm.Native.Trampoline.make = function(elm) {
1010 trampoline = function(t) {
1111 var tramp = t;
1212 while(true) {
13− switch(tramp._0.ctor) {
14− case "Left":
15− return tramp._0._0;
16− case "Right":
17− tramp = tramp._0._0({ctor: "_Tuple0"});
13+ switch(tramp.ctor) {
14+ case "Done":
15+ return tramp._0;
16+ case "Continue":
17+ tramp = tramp._0({ctor: "_Tuple0"});
1818 continue;
1919 }
2020 _E.Case("Trampoline", "in Native.Trampoline.trampoline");
libraries/Trampoline.elm+3−4
@@ -1,5 +1,4 @@
1−module Trampoline (Trampoline, trampoline)
2− where
1+module Trampoline where
32
43{-| Trampolining loops for unbounded recursion.
54Since most javascript implementations lack tail-call elimination, deeply tail-recursive functions will result in a stack overflow.
@@ -33,10 +32,10 @@ main = asText <| fac 1000000000000
3332@docs Trampoline, trampoline
3433 -}
3534import Native.Trampoline
36−import open Either
3735
3836{-| A computation that might loop. A trampoline is either the resulting value or a thunk that needs to be run more. -}
39−data Trampoline a = Trampoline (Either a (() -> Trampoline a))
37+data Trampoline a = Done a
38+ | Continue (() -> Trampoline a)
4039
4140{-| Run a trampolining loop in constant space. -}
4241trampoline : Trampoline a -> a