Skip to content

Commit

Permalink
Adds List.Extra.{cons,conditional}
Browse files Browse the repository at this point in the history
  • Loading branch information
gampleman committed Jul 25, 2024
1 parent 293ff9a commit 7356557
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

66 changes: 62 additions & 4 deletions src/List/Extra.elm
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module List.Extra exposing
( last, init, getAt, uncons, unconsLast, maximumBy, maximumWith, minimumBy, minimumWith, andMap, andThen, reverseMap, takeWhile, dropWhile, unique, uniqueBy, allDifferent, allDifferentBy, setIf, setAt, remove, updateIf, updateAt, updateIfIndex, removeAt, removeIfIndex, removeWhen, swapAt, stableSortWith
( last, init, getAt, cons, uncons, unconsLast, maximumBy, maximumWith, minimumBy, minimumWith, andMap, andThen, reverseMap, takeWhile, dropWhile, unique, uniqueBy, allDifferent, allDifferentBy, setIf, setAt, remove, updateIf, updateAt, updateIfIndex, removeAt, removeIfIndex, removeWhen, swapAt, stableSortWith
, intercalate, transpose, subsequences, permutations, interweave, cartesianProduct, uniquePairs
, foldl1, foldr1, indexedFoldl, indexedFoldr, Step(..), stoppableFoldl
, scanl, scanl1, scanr, scanr1, mapAccuml, mapAccumr, unfoldr, iterate, initialize, cycle, reverseRange
, splitAt, splitWhen, takeWhileRight, dropWhileRight, span, break, stripPrefix, group, groupWhile, inits, tails, select, selectSplit, gatherEquals, gatherEqualsBy, gatherWith, subsequencesNonEmpty, frequencies
, splitAt, splitWhen, takeWhileRight, dropWhileRight, span, break, stripPrefix, group, groupWhile, inits, tails, conditional, select, selectSplit, gatherEquals, gatherEqualsBy, gatherWith, subsequencesNonEmpty, frequencies
, isPrefixOf, isSuffixOf, isInfixOf, isSubsequenceOf, isPermutationOf
, notMember, find, elemIndex, elemIndices, findIndex, findIndices, findMap, count
, zip, zip3
Expand All @@ -17,7 +17,7 @@ module List.Extra exposing
# Basics
@docs last, init, getAt, uncons, unconsLast, maximumBy, maximumWith, minimumBy, minimumWith, andMap, andThen, reverseMap, takeWhile, dropWhile, unique, uniqueBy, allDifferent, allDifferentBy, setIf, setAt, remove, updateIf, updateAt, updateIfIndex, removeAt, removeIfIndex, removeWhen, swapAt, stableSortWith
@docs last, init, getAt, cons, uncons, unconsLast, maximumBy, maximumWith, minimumBy, minimumWith, andMap, andThen, reverseMap, takeWhile, dropWhile, unique, uniqueBy, allDifferent, allDifferentBy, setIf, setAt, remove, updateIf, updateAt, updateIfIndex, removeAt, removeIfIndex, removeWhen, swapAt, stableSortWith
# List transformations
Expand All @@ -37,7 +37,7 @@ module List.Extra exposing
# Sublists
@docs splitAt, splitWhen, takeWhileRight, dropWhileRight, span, break, stripPrefix, group, groupWhile, inits, tails, select, selectSplit, gatherEquals, gatherEqualsBy, gatherWith, subsequencesNonEmpty, frequencies
@docs splitAt, splitWhen, takeWhileRight, dropWhileRight, span, break, stripPrefix, group, groupWhile, inits, tails, conditional, select, selectSplit, gatherEquals, gatherEqualsBy, gatherWith, subsequencesNonEmpty, frequencies
# Predicates
Expand Down Expand Up @@ -251,6 +251,19 @@ reverseRange =
helper []


{-| Adds the first element of the tuple to the head of the list.
cons (1, [2, 3])
--> [1, 2, 3]
Useful for dealing with non-empty lists such as produced by `group` and `groupWhile`.
-}
cons : ( a, List a ) -> List a
cons ( x, xs ) =
x :: xs


{-| Decompose a list into its head and tail. If the list is empty, return `Nothing`. Otherwise, return `Just (x, xs)`, where `x` is head and `xs` is tail.
uncons [1,2,3]
Expand Down Expand Up @@ -1736,6 +1749,51 @@ tailsHelp e list =
[]


{-| Primary useful with literals in view style code.
Sometimes you want to include attributes conditionally:
Html.div
((if isStylish then
[ Html.Attributes.class "foo" ]
else
[ Html.Attributes.id "bar" ]
)
++ (if isFancy then
[ Html.Attribute.attribute "aria-description" "A very fancy div" ]
else
[]
)
)
[ Html.text "Hello" ]
But this is quite awkward and verbose. But with this helper you can write:
Html.div
(List.Extra.conditional
[ ( Html.Attributes.class "foo", isStylish )
, ( Html.Attributes.id "bar", not isStylish )
, ( Html.Attribute.attribute "aria-description" "A very fancy div", isFancy )
]
)
[ Html.text "Hello" ]
-}
conditional : List ( a, Bool ) -> List a
conditional list =
List.filterMap
(\( x, b ) ->
if b then
Just x

else
Nothing
)
list


{-| Return all combinations in the form of (element, rest of the list). Read [Haskell Libraries proposal](https://mail.haskell.org/pipermail/libraries/2008-February/009270.html) for further ideas on how to use this function.
select [ 1, 2, 3, 4 ]
Expand Down
5 changes: 5 additions & 0 deletions tests/ListTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,11 @@ all =
\() ->
Expect.equal (List.Extra.tails [ 1, 2, 3 ]) [ [ 1, 2, 3 ], [ 2, 3 ], [ 3 ], [] ]
]
, describe "conditional"
[ test "returns all elements where the second value is true" <|
\() ->
Expect.equal (List.Extra.conditional [ ( 1, True ), ( 2, False ), ( 3, True ), ( 4, False ) ]) [ 1, 3 ]
]
, describe "select" <|
[ test "returns all variations with a single item removed" <|
\() ->
Expand Down

0 comments on commit 7356557

Please sign in to comment.