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.
this behavior could be surprising if the user is already familiar with lib.strings.removePrefix
There was a problem hiding this comment.
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.
ghci> :m + Data.List
ghci> stripPrefix "hi" "bar"
NothingWas hoping for resolution, but null would be the third option.
There was a problem hiding this comment.
Or avoid completely and use continuation passing.
handlePrefix :: { prefix :: [a], onSuffix :: [a] -> r, onMismatch :: r } -> [a] -> r
There was a problem hiding this comment.
Continuations would be a bit unusual for lib. I like the null option more, Rust also does the same
|
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