Skip to content

Commit cbfe65e

Browse files
committed
Initial commit, migrating stuff from elm-lang/core
0 parents  commit cbfe65e

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

LICENSE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2016, Evan Czaplicki
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of the {organization} nor the names of its
15+
contributors may be used to endorse or promote products derived from
16+
this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Trampolines
2+
3+
Popular JavaScript implementations do not perform any tail-call elimination, so recursive functions can cause a stack overflow if they go too deep. That said, the Elm compiler optimizes [tail calls](https://en.wikipedia.org/wiki/Tail_call) into loops for functions that call themselves, so most situations will just get optimized behind the scenes for you.
4+
5+
The compiler does not do anything for *mutually* tail-recursive functions though. If you find yourself needing that, you can use a [trampoline](http://en.wikipedia.org/wiki/Tail-recursive_function#Through_trampolining). Trampolines make it possible to call functions recursively without growing the stack.
6+
7+
This strategy creates intermediate closures, which is very expensive in JavaScript, so use this library only when it is essential that you recurse deeply.

src/Trampoline.elm

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Trampoline
2+
( trampoline
3+
, Trampoline
4+
, done, continue
5+
)
6+
where
7+
8+
{-| A [trampoline](http://en.wikipedia.org/wiki/Tail-recursive_function#Through_trampolining)
9+
makes it possible to recursively call a function without growing the stack.
10+
11+
Popular JavaScript implementations do not perform any tail-call elimination, so
12+
recursive functions can cause a stack overflow if they go too deep. Trampolines
13+
permit unbounded recursion despite limitations in JavaScript.
14+
15+
This strategy may create many intermediate closures, which is very expensive in
16+
JavaScript, so use this library only when it is essential that you recurse deeply.
17+
18+
# Trampolines
19+
@docs Trampoline, done, jump, evaluate
20+
-}
21+
22+
23+
{-| A computation that has been broken up into a bunch of smaller chunks. The
24+
programmer explicitly adds "pause points" so each chunk of computation can be
25+
run without making the stack any deeper.
26+
-}
27+
type Trampoline a
28+
= Done a
29+
| Jump (() -> Trampoline a)
30+
31+
32+
done : a -> Trampoline a
33+
done =
34+
Done
35+
36+
37+
jump : (() -> Trampoline a) -> Trampoline a
38+
jump =
39+
Jump
40+
41+
42+
{-| Evaluate a trampolined value in constant space. -}
43+
evaluate : Trampoline a -> a
44+
evaluate trampoline =
45+
case trampoline of
46+
Done value ->
47+
value
48+
49+
Jump f ->
50+
evaluate (f ())

0 commit comments

Comments
 (0)