-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #412 from andrewMacmurray/stack-safe-helpers
Makes BackendTask.combine stack safe
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module List.Chunks exposing (chunk) | ||
|
||
import Array exposing (Array) | ||
|
||
|
||
{-| Adapted from <https://package.elm-lang.org/packages/krisajenkins/elm-exts/latest/Exts-List> | ||
Split a list into chunks of length `n`. | ||
Be aware that the last sub-list may be smaller than `n`-items long. | ||
For example `chunk 3 [1..10] => [[1,2,3], [4,5,6], [7,8,9], [10]]` | ||
-} | ||
chunk : Int -> List a -> List (List a) | ||
chunk n xs = | ||
if n < 1 then | ||
List.singleton xs | ||
|
||
else | ||
evaluate (chunkInternal n xs Array.empty) | ||
|
||
|
||
chunkInternal : Int -> List a -> Array (List a) -> Trampoline (List (List a)) | ||
chunkInternal n xs acc = | ||
-- elm-review: known-unoptimized-recursion | ||
if List.isEmpty xs then | ||
Done (Array.toList acc) | ||
|
||
else | ||
Jump | ||
(\_ -> | ||
chunkInternal n | ||
(List.drop n xs) | ||
(Array.push (List.take n xs) acc) | ||
) | ||
|
||
|
||
type Trampoline a | ||
= Done a | ||
| Jump (() -> Trampoline a) | ||
|
||
|
||
evaluate : Trampoline a -> a | ||
evaluate trampoline = | ||
case trampoline of | ||
Done value -> | ||
value | ||
|
||
Jump f -> | ||
evaluate (f ()) |