-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
Conversation
4c78b6a
to
545ad71
Compare
545ad71
to
9fdc0bb
Compare
lib.lists.hasPrefix
: initlib.lists.{hasPrefix,removePrefix}
: init
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"; |
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.
this behavior could be surprising if the user is already familiar with lib.strings.removePrefix
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 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.
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.
ghci> :m + Data.List
ghci> stripPrefix "hi" "bar"
Nothing
Was hoping for resolution, but null
would be the third option.
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.
Or avoid completely and use continuation passing.
handlePrefix :: { prefix :: [a], onSuffix :: [a] -> r, onMismatch :: r } -> [a] -> r
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.
Continuations would be a bit unusual for lib
. I like the null
option more, Rust also does the same
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.
Yeah I think I'm happy with this design, I think this can be merged. |
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.removePrefix :: [ a ] -> [ a ] -> [ a ]
: Remove the first list as a prefix from the second list, erroring if it's not a prefix: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