-
Notifications
You must be signed in to change notification settings - Fork 16
Add List.Extra.insertAt #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+227
−5
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| module List.Extra.InsertAt exposing | ||
| ( insertAtRecursion | ||
| , insertAtRecursion2 | ||
| , insertAtRecursion3 | ||
| , insertAtSplitAt | ||
| , insertAtTakeDrop | ||
| ) | ||
|
|
||
| import List.Extra | ||
|
|
||
|
|
||
| insertAtRecursion : Int -> a -> List a -> List a | ||
| insertAtRecursion index value list = | ||
| if index <= -1 then | ||
| list | ||
|
|
||
| else | ||
| let | ||
| go : Int -> List a -> List a -> List a | ||
| go i rest acc = | ||
| if i == index then | ||
| List.reverse acc ++ (value :: rest) | ||
|
|
||
| else | ||
| case rest of | ||
| [] -> | ||
| -- index > length list | ||
| list | ||
|
|
||
| head :: newRest -> | ||
| go (i + 1) newRest (head :: acc) | ||
| in | ||
| go 0 list [] | ||
|
|
||
|
|
||
| insertAtRecursion2Help : Int -> a -> List a -> Int -> List a -> List a -> List a | ||
| insertAtRecursion2Help index value list i rest acc = | ||
| if i == index then | ||
| List.foldl (::) (value :: rest) acc | ||
|
|
||
| else | ||
| case rest of | ||
| [] -> | ||
| -- index > length list | ||
| list | ||
|
|
||
| head :: newRest -> | ||
| insertAtRecursion2Help index value list (i + 1) newRest (head :: acc) | ||
|
|
||
|
|
||
| insertAtRecursion2 : Int -> a -> List a -> List a | ||
| insertAtRecursion2 index value list = | ||
| if index <= -1 then | ||
| list | ||
|
|
||
| else | ||
| insertAtRecursion2Help index value list 0 list [] | ||
|
|
||
|
|
||
| insertAtTakeDrop : Int -> a -> List a -> List a | ||
| insertAtTakeDrop index value list = | ||
| if index <= -1 then | ||
| list | ||
|
|
||
| else | ||
| let | ||
| length = | ||
| List.length list | ||
| in | ||
| if length < index then | ||
| list | ||
|
|
||
| else | ||
| List.take index list ++ (value :: List.drop index list) | ||
|
|
||
|
|
||
| insertAtSplitAt : Int -> a -> List a -> List a | ||
| insertAtSplitAt index value list = | ||
| if index <= -1 then | ||
| list | ||
|
|
||
| else | ||
| let | ||
| ( before, after ) = | ||
| List.Extra.splitAt index list | ||
| in | ||
| if List.isEmpty after && List.length before < index then | ||
| list | ||
|
|
||
| else | ||
| before ++ (value :: after) | ||
|
|
||
|
|
||
| insertAtRecursion3Help : a -> List a -> Int -> List a -> List a -> List a | ||
| insertAtRecursion3Help value list i rest acc = | ||
| if i == 0 then | ||
| List.foldl (::) (value :: rest) acc | ||
|
|
||
| else | ||
| case rest of | ||
| [] -> | ||
| -- index > length list | ||
| list | ||
|
|
||
| head :: newRest -> | ||
| insertAtRecursion3Help value list (i - 1) newRest (head :: acc) | ||
|
|
||
|
|
||
| insertAtRecursion3 : Int -> a -> List a -> List a | ||
| insertAtRecursion3 index value list = | ||
| if index <= -1 then | ||
| list | ||
|
|
||
| else | ||
| insertAtRecursion3Help value list index list [] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I forgot to mention that in my benchmarks this instead counts down from index and checks when it goes to 0. This avoids the extra
iargument but should not be that much faster (not sure if comparisons with a literal are only optimized in eol2 or elm make)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s an Elm compiler optimization – comparisons with literals are faster.
https://github.com/elm/compiler/blob/cce7a8bbd8fe690fc83fa795f8d7e02505d1f25f/compiler/src/Generate/JavaScript/Expression.hs#L550-L555