Skip to content

Commit

Permalink
Added function: frequencies (#163)
Browse files Browse the repository at this point in the history
* Added function: frequencies

* Update Tests.elm

Forgot closing bracket

* Update Tests.elm

Co-authored-by: Cristian Iorga <cristian.iorga@devnest.ro>
Co-authored-by: Chadtech <chadtech0@gmail.com>
  • Loading branch information
3 people committed Oct 31, 2022
1 parent ce86bf7 commit b88b36f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/List/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module List.Extra exposing
, 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
, splitAt, splitWhen, takeWhileRight, dropWhileRight, span, break, stripPrefix, group, groupWhile, inits, tails, select, selectSplit, gatherEquals, gatherEqualsBy, gatherWith, subsequencesNonEmpty, frequencies
, isPrefixOf, isSuffixOf, isInfixOf, isSubsequenceOf, isPermutationOf
, notMember, find, elemIndex, elemIndices, findIndex, findIndices, findMap, count
, zip, zip3
Expand Down Expand Up @@ -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
@docs splitAt, splitWhen, takeWhileRight, dropWhileRight, span, break, stripPrefix, group, groupWhile, inits, tails, select, selectSplit, gatherEquals, gatherEqualsBy, gatherWith, subsequencesNonEmpty, frequencies
# Predicates
Expand Down Expand Up @@ -2140,6 +2140,22 @@ gatherWith testFn list =
helper list []


{-| Calculate the number of occurences for each element in a list. Elements
will be ordered ascendingly, then grouped in a tuple with the number of
occurences.
frequencies [2,1,3,2,3,3]
--> [(1,1),(2,2),(3,3)]
-}
frequencies : List comparable -> List ( comparable, Int )
frequencies list =
list
|> List.sort
|> group
|> List.map (\( x, y ) -> ( x, 1 + List.length y ))


{-| Performs an inner join, combining data items from both lists if they match by their respective key functions.
employees : List { name : String, departmentId : Int }
Expand Down
60 changes: 59 additions & 1 deletion tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,64 @@ all =
joinOn Tuple.pair identity identity [ 1, 3, 2 ] [ 2, 1, 3 ]
|> Expect.equal [ ( 3, 3 ), ( 2, 2 ), ( 1, 1 ) ]
]
, describe "frequencies"
[ describe "Property testing with fuzz"
[ fuzz (list int) "Final value similar list and List.sort list" <|
\list ->
frequencies list
|> List.sort
|> Expect.equal (frequencies list)
, fuzz (list int) "Final value similar, no matter the order of composition for frequencies and List.sort" <|
\list ->
let
freqFirst =
list
|> frequencies
|> List.sort

sortFirst =
list
|> List.sort
|> frequencies
in
freqFirst
|> Expect.equal sortFirst
, fuzz2 (list int) (list int) "Two (different/similar) lists should give 2 (different/similar) results" <|
\list1 list2 ->
let
areListsSimilar =
isPermutationOf list2 list1

freq1 =
frequencies list1

freq2 =
frequencies list2

shouldFreqsBeSimilar =
areListsSimilar

areFreqsSimilar =
isPermutationOf freq2 freq1
in
areFreqsSimilar
|> Expect.equal shouldFreqsBeSimilar
]
, describe "Unit testing on basic examples"
[ test "Frequencies on List Int" <|
\() ->
frequencies [ 4, 1, 3, 2, 2, 4, 3, 3, 4, 4 ]
|> Expect.equal [ ( 1, 1 ), ( 2, 2 ), ( 3, 3 ), ( 4, 4 ) ]
, test "Frequencies on List String" <|
\() ->
frequencies [ "a", "b", "aa", "c", "b", "aa", "c", "C", "C", "D" ]
|> Expect.equal [ ( "C", 2 ), ( "D", 1 ), ( "a", 1 ), ( "aa", 2 ), ( "b", 2 ), ( "c", 2 ) ]
, test "Frequencies on empty List a" <|
\() ->
frequencies []
|> Expect.equal []
]
]
, describe "stoppableFoldl"
[ fuzz (list int) "behaves like foldl if function always returns Continue" <|
\xs ->
Expand Down Expand Up @@ -1056,7 +1114,7 @@ all =
Stop n

else
explode ()
throwRangeErrorException ()
)
0
(List.range 1 1000)
Expand Down

0 comments on commit b88b36f

Please sign in to comment.