This module implements lazy construction of strict List. It is not Lazy List implementation and it has different characteristics. It's mostly intended for internal purposes of this library and is exposed just in case some additional user extensions will need it.
Init empty LList.
toList empty
--> []Init singleton LList.
singleton "foo"
|> toList
--> [ "foo" ]Init LList using constructor.
LList is init as a function from a -> List b.
Evaluation of this function is lazy and happens in time when
actuall value is needed, not when constructor is called.
For instance you can use some List constructor:
llist (List.range 0) 5
|> toList
--> [ 0, 1, 2, 3, 4, 5 ]Or use any other function you needList:
llist (List.filter <| \a -> modBy 2 a == 0) (List.range 0 10)
|> toList
--> [ 0, 2, 4, 6, 8, 10 ]Construct LList from List.
fromList [ "foo", "bar", "baz" ]
|> toList
--> [ "foo", "bar", "baz" ]Add element to LList.
empty
|> cons "bar"
|> cons "foo"
|> toList
--> [ "foo", "bar" ]This function is performed lazily.
Append LList to LList.
append (singleton "foo") (fromList [ "bar", "baz" ])
|> toList
--> [ "foo", "bar", "baz" ]This function is performed lazily.
Check if LList is empty.
isEmpty empty
--> True
isEmpty (singleton "foo")
--> False
llist (List.filter <| \a -> a > 10) [ 1, 2, 3 ]
|> isEmpty
--> TrueThis function forces evaluation.
Build List from LList.
toList empty
--> []
toList <| llist (List.range 0) 2
--> [ 0, 1, 2 ]This function forces evaluation.
Get first element from LList.
head empty
--> Nothing
llist (List.range 0) 2
|> head
--> Just 0This function forces evaluation.
Get first element from LList.
tail empty
--> Nothing
tail (singleton "foo")
|> Maybe.map toList
--> Just []
llist (List.range 0) 2
|> tail
|> Maybe.map toList
--> Just [ 1, 2 ]This function forces evaluation.
Map function over LList.
llist (List.range 0) 5
|> map ((*) 2)
|> toList
--> [ 0, 2, 4, 6, 8, 10 ]This function is performed lazily.
Map two functions over LList.
llist (List.range 0) 5
|> map2 (+) (llist (List.range 0) 5)
|> toList
--> [ 0, 2, 4, 6, 8, 10 ]This function is performed lazily.
Filter LList.
(cons 1 <| cons 2 <| cons 3 empty)
|> filter ((<) 1)
|> toList
--> [ 2, 3 ]This function is performed lazily.
Similar to List.filterMap but for LList.
(cons 1 <| cons 2 <| cons 3 empty)
|> filterMap (\a -> if 1 < a then Just (2 * a) else Nothing)
|> toList
--> [ 4, 6 ]This function is performed lazily.
Reverse LList.
fromList [ 1, 2, 3 ]
|> reverse
|> toList
--> [ 3, 2, 1 ]This function is performed lazily.
Sort by for LList.
fromList [ 3, 1, 2 ]
|> sort
|> toList
--> [ 1, 2, 3 ]This function is performed lazily.
Sort by for LList.
fromList [ 3, 1, 2 ]
|> sortBy identity
|> toList
--> [ 1, 2, 3 ]
fromList [ { val = "c"} , { val = "b"}, { val = "a"} ]
|> sortBy .val
|> toList
--> [ { val = "a"}, { val = "b"}, { val = "c"} ]This function is performed lazily.
Sort with for LList
flippedComparison : comparable -> comparable -> Order
flippedComparison a b =
case Basics.compare a b of
LT -> GT
EQ -> EQ
GT -> LT
llist (List.range 1) 5
|> sortWith flippedComparison
|> toList
--> [ 5, 4, 3, 2, 1 ]This function is performed lazily.
Same as List.foldr but for LLists.
llist (List.range 0) 5
|> foldr (+) 0
--> 15
llist (List.range 0) 3
|> foldr (::) []
--> [ 0, 1, 2, 3 ]This function forces evaluation.
Same as List.foldl but for LLists.
llist (List.range 0) 5
|> foldl (+) 0
--> 15
llist (List.range 0) 3
|> foldl (::) []
--> [ 3, 2, 1, 0 ]This function forces evaluation.
Flatten LList.
fromList [ singleton "foo", cons "bar" <| singleton "baz" ]
|> concat
|> toList
--> [ "foo", "bar", "baz" ]This function is performed lazily.
Map LList construction over LList.
cons "foo" (cons "bar" <| singleton "baz")
|> andThen (\a -> cons a <| singleton (a ++ " fighter" ))
|> toList
--> [ "foo", "foo fighter", "bar", "bar fighter", "baz", "baz fighter" ]This function is performed lazily.
LListuses lazy implementation and therefore can't be compared using==