Skip to content

Commit 1a3ca35

Browse files
committed
Make unfoldr TCO
1 parent e3a8269 commit 1a3ca35

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

review/suppressed/NoUnoptimizedRecursion.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"automatically created by": "elm-review suppress",
44
"learn more": "elm-review suppress --help",
55
"suppressions": [
6-
{ "count": 7, "filePath": "src/List/Extra.elm" }
6+
{ "count": 6, "filePath": "src/List/Extra.elm" }
77
]
88
}

src/List/Extra.elm

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,12 +1535,17 @@ mapAccumr f acc0 list =
15351535
-}
15361536
unfoldr : (b -> Maybe ( a, b )) -> b -> List a
15371537
unfoldr f seed =
1538-
case f seed of
1539-
Nothing ->
1540-
[]
1538+
let
1539+
go : b -> List a -> List a
1540+
go x acc =
1541+
case f x of
1542+
Nothing ->
1543+
List.reverse acc
15411544

1542-
Just ( a, b ) ->
1543-
a :: unfoldr f b
1545+
Just ( a, b ) ->
1546+
go b (a :: acc)
1547+
in
1548+
go seed []
15441549

15451550

15461551
{-| Take a number and a list, return a tuple of lists, where first part is prefix of the list of length equal the number, and second part is the remainder of the list. `splitAt n xs` is equivalent to `(take n xs, drop n xs)`.

0 commit comments

Comments
 (0)