Skip to content
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

lib.lists.{hasPrefix,removePrefix}: init #243511

Merged
merged 2 commits into from
Aug 14, 2023

Conversation

infinisil
Copy link
Member

@infinisil infinisil commented Jul 14, 2023

Description of changes

Adds two new functions:

  • lib.lists.hasPrefix :: [ a ] -> [ a ] -> bool: Whether the first list is a prefix of the second list:
    lib.lists.hasPrefix [ 1 2 ] [ 1 2 3 4 ]
    -> true
  • lib.lists.removePrefix :: [ a ] -> [ a ] -> [ a ]: Remove the first list as a prefix from the second list, erroring if it's not a prefix:
    lib.lists.removePrefix [ 1 2 ] [ 1 2 3 4 ]
    -> [ 3 4 ]
    lib.lists.removePrefix [ 1 2 ] [ 3 4 ]
    -> <error>

This is distantly part of the path library effort, in order to be able to work with lists of path components.

This work is sponsored by Antithesis

Things done
  • Docs
  • Tests

@infinisil infinisil requested a review from edolstra as a code owner July 14, 2023 16:22
@github-actions github-actions bot added the 6.topic: lib The Nixpkgs function library label Jul 14, 2023
@infinisil infinisil mentioned this pull request Jul 14, 2023
13 tasks
@infinisil infinisil changed the title lib.lists.hasPrefix: init lib.lists.hasPrefix: init Jul 14, 2023
@infinisil infinisil force-pushed the lib.lists.hasPrefix branch from 4c78b6a to 545ad71 Compare July 14, 2023 16:32
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Jul 14, 2023
@infinisil infinisil force-pushed the lib.lists.hasPrefix branch from 545ad71 to 9fdc0bb Compare July 14, 2023 17:36
@infinisil infinisil changed the title lib.lists.hasPrefix: init lib.lists.{hasPrefix,removePrefix}: init Jul 14, 2023
if hasPrefix list1 list2 then
drop (length list1) list2
else
throw "lib.lists.removePrefix: First argument is not a list prefix of the second argument";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this behavior could be surprising if the user is already familiar with lib.strings.removePrefix

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a good point, though I'd argue that this here is the more correct behavior, at least given the name of this function. A version that doesn't fail should maybe be called tryRemovePrefix instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ghci> :m + Data.List
ghci> stripPrefix "hi" "bar"
Nothing

Was hoping for resolution, but null would be the third option.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or avoid completely and use continuation passing.

handlePrefix :: { prefix :: [a], onSuffix :: [a] -> r, onMismatch :: r } -> [a] -> r

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuations would be a bit unusual for lib. I like the null option more, Rust also does the same

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then arguably hasPrefix is barely necessary anymore, because you can just do removePrefix list != null, which would then also be more efficient.

However, the path version of these functions already exist now with the throw semantics: #237610 and #238013

@infinisil
Copy link
Member Author

Yeah I think I'm happy with this design, I think this can be merged.

@infinisil infinisil merged commit 87c5a6a into NixOS:master Aug 14, 2023
@infinisil infinisil deleted the lib.lists.hasPrefix branch August 14, 2023 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: lib The Nixpkgs function library 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants