Skip to content

Commit

Permalink
isPermutationOf performance increase (#155)
Browse files Browse the repository at this point in the history
* Removed currying form isPermutationOf implementation and rearranged cases to make more common cases in if and case statements calculated first

* Remove typo

Remove typo
  • Loading branch information
Chadtech committed Jan 10, 2022
1 parent 4769635 commit ebab60d
Showing 1 changed file with 22 additions and 27 deletions.
49 changes: 22 additions & 27 deletions src/List/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1833,34 +1833,11 @@ In other words: Do the 2 `List`s contain the same elements but in a different or
-}
isPermutationOf : List a -> List a -> Bool
isPermutationOf permut xs =
let
removeOneMember : a -> List a -> { foundAny : Bool, without : List a }
removeOneMember culprit =
removeOneMemberHelp culprit []

removeOneMemberHelp culprit before list =
case list of
[] ->
{ foundAny = False, without = [] }

head :: after ->
if head == culprit then
{ foundAny = True, without = before ++ after }

else
removeOneMemberHelp culprit (head :: before) after
in
case ( permut, xs ) of
( [], [] ) ->
True

( [], _ :: _ ) ->
False

( _ :: _, [] ) ->
False
case xs of
[] ->
List.isEmpty permut

( _ :: _, x :: after ) ->
x :: after ->
let
{ foundAny, without } =
removeOneMember x permut
Expand All @@ -1872,6 +1849,24 @@ isPermutationOf permut xs =
False


removeOneMember : a -> List a -> { foundAny : Bool, without : List a }
removeOneMember culprit l =
removeOneMemberHelp culprit [] l


removeOneMemberHelp culprit before list =
case list of
[] ->
{ foundAny = False, without = [] }

head :: after ->
if head == culprit then
{ foundAny = True, without = before ++ after }

else
removeOneMemberHelp culprit (head :: before) after


{-| Take two lists and returns a list of corresponding pairs
-}
zip : List a -> List b -> List ( a, b )
Expand Down

0 comments on commit ebab60d

Please sign in to comment.