Skip to content

Commit

Permalink
Add consecutivePairs and pairwise
Browse files Browse the repository at this point in the history
  • Loading branch information
Janiczek committed Jun 30, 2023
1 parent 7542f94 commit ef8d57b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/List/Extra.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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, filterNot, swapAt, stableSortWith
, intercalate, transpose, subsequences, permutations, interweave, cartesianProduct, uniquePairs
, intercalate, transpose, subsequences, permutations, interweave, cartesianProduct, uniquePairs, consecutivePairs, pairwise
, 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
Expand All @@ -22,7 +22,7 @@ module List.Extra exposing
# List transformations
@docs intercalate, transpose, subsequences, permutations, interweave, cartesianProduct, uniquePairs
@docs intercalate, transpose, subsequences, permutations, interweave, cartesianProduct, uniquePairs, consecutivePairs, pairwise
# Folds
Expand Down Expand Up @@ -1191,6 +1191,44 @@ uniquePairs xs =
List.map (\y -> ( x, y )) xs_ ++ uniquePairs xs_


{-| Returns consecutive pairs found in the input list.
Given a list with n items, the result has n-1 items.
consecutivePairs [ 1, 2, 3, 4 ]
--> [ ( 1, 2 ), ( 2, 3 ), ( 3, 4 ) ]
consecutivePairs []
--> []
-}
consecutivePairs : List a -> List ( a, a )
consecutivePairs xs =
pairwise Tuple.pair xs


{-| Runs a function on each pair of consecutive items.
Given a list with n items, the result has n-1 items.
pairwise (+) [ 1, 2, 3, 4 ]
--> [ 3, 5, 7 ]
pairwise max [ 1, 2, 3, 4 ]
--> [ 2, 3, 4 ]
pairwise (-) [100, 10, 1]
--> [ 90, 9 ]
pairwise Tuple.pair [ 1, 2, 3, 4 ]
--> [ ( 1, 2 ), ( 2, 3 ), ( 3, 4 ) ]
-}
pairwise : (a -> a -> b) -> List a -> List b
pairwise fn xs =
List.map2 fn xs (List.drop 1 xs)


reverseAppend : List a -> List a -> List a
reverseAppend list1 list2 =
List.foldl (::) list2 list1
Expand Down
44 changes: 44 additions & 0 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1134,4 +1134,48 @@ all =
(List.range 1 1000)
|> Expect.equal 50
]
, describe "consecutivePairs"
[ fuzz (Fuzz.list Fuzz.int) "Length is one less" <|
\xs ->
let
output =
consecutivePairs xs
in
case List.length xs of
0 ->
List.length output
|> Expect.equal 0

n ->
List.length output
|> Expect.equal (n - 1)
, test "Example" <|
\() ->
consecutivePairs [ 1, 10, 100 ]
|> Expect.equalLists [ ( 1, 10 ), ( 10, 100 ) ]
, test "Nothing weird going on with duplicates" <|
\() ->
consecutivePairs [ 10, 10, 10, 10 ]
|> Expect.equalLists [ ( 10, 10 ), ( 10, 10 ), ( 10, 10 ) ]
]
, describe "pairwise"
[ fuzz (Fuzz.list Fuzz.int) "Length is one less" <|
\xs ->
let
output =
consecutivePairs xs
in
case List.length xs of
0 ->
List.length output
|> Expect.equal 0

n ->
List.length output
|> Expect.equal (n - 1)
, test "Example showing off the order of arguments" <|
\() ->
pairwise (-) [ 100, 10, 1 ]
|> Expect.equalLists [ 90, 9 ]
]
]

0 comments on commit ef8d57b

Please sign in to comment.