Skip to content

Commit 963c31e

Browse files
committed
Add SearchResults.take
1 parent 84c77e8 commit 963c31e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/Lib/SearchResults.elm

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Lib.SearchResults exposing
2121
, next
2222
, prepend
2323
, prev
24+
, take
2425
, toList
2526
, toMaybe
2627
, uniqueMatchesBy
@@ -102,6 +103,26 @@ mapMatches f results =
102103
SearchResults (Matches (Zipper.map f matches))
103104

104105

106+
{-| Limit the number of results
107+
-}
108+
take : Int -> SearchResults a -> SearchResults a
109+
take n results =
110+
case results of
111+
Empty ->
112+
Empty
113+
114+
SearchResults (Matches matches) ->
115+
SearchResults
116+
(Matches
117+
(matches
118+
|> Zipper.toList
119+
|> List.take n
120+
|> Zipper.fromList
121+
|> Maybe.withDefault matches
122+
)
123+
)
124+
125+
105126
mapToList : (a -> Bool -> b) -> SearchResults a -> List b
106127
mapToList f results =
107128
case results of

0 commit comments

Comments
 (0)