Parse a string URL to Enum? #28
Replies: 1 comment
-
Ah yes, the let customProtocol = Skip(PrefixThrough<Substring>("dead-beef://"))
Yes, super long chains can definitely bog down the compiler, which also happens with super long chains of lazy array operations. For example this takes a really long to compile: let tmp = Array(1...100)
.lazy
.map { $0 + 1 }
.filter { $0 % 2 == 0 }
.map { $0 + 1 }
.filter { $0 % 2 == 0 }
.map { $0 + 1 }
.filter { $0 % 2 == 0 }
.map { $0 + 1 }
.filter { $0 % 2 == 0 }
.map { $0 + 1 }
.filter { $0 % 2 == 1 } It can be helpful to break out large parsers into a few smaller ones.
What you are doing here can definitely work, but it's sometimes easier to work with an intermediate type to parse rather than going directly to string. For example, we have a router example in this repo and we chose to first convert URL strings into a Working on this more structured data types makes it so that you don't have to worry about nitty gritty details like slashes, equal signs, etc. |
Beta Was this translation helpful? Give feedback.
-
Long time listener, first time caller.... 😅
Below I have a
urlParser
that chains viaorElse
's 2 other parsers with increasing complexity to map a String to a Route. This works, however a few things feel off or altogether not optimal.The compiler complained at me for not explicitly ignoring the input to the
.map
operators inroot
andaccount
.... "Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored". I'm guessing this is because I started withPrefixThrough<Substring>
rather than skipping that prefix? I wasn't certain how to start the parser chain to get the effect I wanted.I ran into compiler complexity problems with the long chain of skipped parsers and having distinct parsers defined helped with that. Is that a good practice wrt to long-chains of parsers?
Am using the correct parsers and/or combining them in the right ways?
As a quick follow up I wrote some tests and found this isn't working as I'd suspect.
.skip
isn't doing what I thought it would.This malformed Url successfully parses out the code:
I'm definitely doing something wrong, here. What's the correct Parser/Operator to use to ensure a token is in the string, failing if it is not?
Beta Was this translation helpful? Give feedback.
All reactions