Add faster list functions

This commit is contained in:
Jeroen Engels 2022-09-17 16:49:21 +02:00
parent 5b79818260
commit 902fd381b6

View File

@ -1,6 +1,7 @@
module Vendor.ListExtra exposing
( find, last, uniquePairs
, fastConcatMap
, orderIndependentMap, orderIndependentAppend, orderIndependentConcatMap, orderIndependentMapAppend, orderIndependentConcat, orderIndependentConcatMapAppend
)
{-| Functions taken from elm-community/list-extra.
@ -8,11 +9,16 @@ module Vendor.ListExtra exposing
These were used so that we wouldn't have dependency conflicts when
elm-community/list-extra would release a new major version.
This also includes a few custom functions
# List functions
@docs find, last, uniquePairs
@docs fastConcatMap
@docs orderIndependentMap, orderIndependentAppend, orderIndependentConcatMap, orderIndependentMapAppend, orderIndependentConcat, orderIndependentConcatMapAppend
-}
@ -64,3 +70,46 @@ uniquePairs xs =
fastConcatMap : (a -> List b) -> List a -> List b
fastConcatMap fn list =
List.foldr (\item acc -> fn item ++ acc) [] list
{-| Version of List.map that doesn't care about order.
-}
orderIndependentMap : (a -> b) -> List a -> List b
orderIndependentMap fn list =
List.foldl (\element acc -> fn element :: acc) [] list
{-| Version of List.append that doesn't care about order.
-}
orderIndependentAppend : List a -> List a -> List a
orderIndependentAppend left right =
List.foldl (::) right left
{-| Version of "List.append (List.map fn left) right" that doesn't care about order.
-}
orderIndependentMapAppend : (a -> b) -> List a -> List b -> List b
orderIndependentMapAppend fn left right =
List.foldl (\element acc -> fn element :: acc) right left
{-| Version of "(List.concatMap fn left) ++ right" that doesn't care about order.
-}
orderIndependentConcatMapAppend : (a -> List b) -> List a -> List b -> List b
orderIndependentConcatMapAppend fn left right =
List.foldl (\item acc -> orderIndependentAppend (fn item) acc) right left
orderIndependentConcat : List (List a) -> List a
orderIndependentConcat list =
case list of
[] ->
[]
firstElement :: rest ->
List.foldl (\subList acc -> orderIndependentAppend subList acc) firstElement rest
orderIndependentConcatMap : (a -> List b) -> List a -> List b
orderIndependentConcatMap fn list =
List.foldl (\item acc -> orderIndependentAppend (fn item) acc) [] list