on master help sign in
Simplify Trampoline datatype.
b471661Max New
libraries/Native/Trampoline.js+5−5
@@ -10,11 +10,11 @@ Elm.Native.Trampoline.make = function(elm) {
10 trampoline = function(t) {
10 trampoline = function(t) {
11 var tramp = t;
11 var tramp = t;
12 while(true) {
12 while(true) {
13− switch(tramp._0.ctor) {
13+ switch(tramp.ctor) {
14− case "Left":
14+ case "Done":
15− return tramp._0._0;
15+ return tramp._0;
16− case "Right":
16+ case "Continue":
17− tramp = tramp._0._0({ctor: "_Tuple0"});
17+ tramp = tramp._0({ctor: "_Tuple0"});
18 continue;
18 continue;
19 }
19 }
20 _E.Case("Trampoline", "in Native.Trampoline.trampoline");
20 _E.Case("Trampoline", "in Native.Trampoline.trampoline");
libraries/Trampoline.elm+3−4
@@ -1,5 +1,4 @@
1−module Trampoline (Trampoline, trampoline)
1+module Trampoline where
2− where
3
2
4{-| Trampolining loops for unbounded recursion.
3{-| Trampolining loops for unbounded recursion.
5Since most javascript implementations lack tail-call elimination, deeply tail-recursive functions will result in a stack overflow.
4Since 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
33@docs Trampoline, trampoline
32@docs Trampoline, trampoline
34 -}
33 -}
35import Native.Trampoline
34import Native.Trampoline
36−import open Either
37
35
38{-| A computation that might loop. A trampoline is either the resulting value or a thunk that needs to be run more. -}
36{-| 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)
40
39
41{-| Run a trampolining loop in constant space. -}
40{-| Run a trampolining loop in constant space. -}
42trampoline : Trampoline a -> a
41trampoline : Trampoline a -> a