Comments (7)
Concatenation by itself is only good for ordered collections with regular structure such as lists. Even file paths are more complex than just a list of path components, because they may be absolute or relative (even if we ignore drive letters on Windows).
This is obvious if we recall that the (</>)
operation from filepath
is not quite concatenation, it has to be more complex because it has to deal with cases like when its right (second) argument is absolute. What to do in that case? We need to adopt a convention: we just return the second argument unchanged in that case.
URIs are even more complex than file paths, they have many different components and merging two URIs (I don't even say "concatenate") should also be done by using a convention. Luckily the standard describing URIs, RFC 3986 describes how to resolve one URI against another absolute URI, which you can do with modern-uri-0.2.0.0
and later, see relativeTo
.
If you just want to operate on path pieces only, you first need to obtain the path pieces. Since not every Text
is a valid path piece, you must prove that your candidate is a valid path piece by feeding it into mkPathPiece
and obtaining a refined text value (which serves as a proof that that Text
value is indeed a valid path piece). This "proving" may conclude that your candidate is not a valid path piece, of course, that's why we have the MonadThrow
constraint. If you click on MonadThrow
you'll see that it has pure instances like Maybe
too, so you can work in that context.
When you have your path pieces, you may do whatever you want by using lens:
λ> :set -XOverloadedStrings
λ> uri <- URI.mkURI "https://markkarpov.com"
λ> pp <- URI.mkPathPiece "foo"
λ> URI.render (uri & uriPath %~ (pp:))
"https://markkarpov.com/foo"
from modern-uri.
Not really, uriPath
, the lens, still manipulates plain list of path pieces:
https://hackage.haskell.org/package/modern-uri-0.2.0.0/docs/Text-URI-Lens.html#v:uriPath
For your convenience!
from modern-uri.
@mrkkrp Thanks for your response! Sure, I don't want to concatenate two URIs. Using lenses is good, I just wasn't sure how to do it exactly. I would appreciate having some combinator like this:
infixl 7 </> -- not sure about precedence
(</>) :: URI -> RText PathPiece -> URI
uri </> pp = uri & uriPath %~ (++ [pp])
from modern-uri.
I think such a combinator is not a good idea, because from the design point of view we step into the area of "too specific" combinations of existing building blocks. My philosophy is to provide the basic building blocks, which are simple, few, and compose well and let the user combine them to solve his/her specific tasks. In addition to that, such a function would make it tempting to use it repeatedly to build a path, and given the implementation appending will have quadratic complexity.
from modern-uri.
and given the implementation appending will have quadratic complexity.
Yes, this is sad, I agree. Probably, switching from list to Seq
from Data.Sequence
can solve performance problem. But I'm not sure is it worth it. I agree with your vision on providing basic building blocks. Your answer already helped me and resolved my issue!
from modern-uri.
Btw, uri & uriPath %~ (pp:)
doesn't really work well because since version 0.2.0.0
uriPath :: Maybe (Bool, NonEmpty (RText PathPiece))
.
from modern-uri.
Oh, I see. Thanks a lot!
from modern-uri.
Related Issues (20)
- Use newtypes instead of `RText` HOT 2
- File URIs are not parsed HOT 2
- A URI with a Query such as '?&height=500&width=500' cannot be parsed HOT 2
- Expose parsers of certain classes of URIs HOT 5
- Support for "data:" URI scheme? HOT 12
- Port numbers validation HOT 5
- Add an `updateQueryParams` function HOT 3
- Doesn't build with GHC-9.0
- Compatibility with bytestring 0.11 HOT 2
- Trailing slashes for URLs with no path HOT 2
- Allow `[` and `]` in the query component HOT 6
- Hashable instance HOT 1
- relativeTo and quasi-quotation
- GHC 9.2/TemplateHaskell 2.18 HOT 1
- Bug parsing numeric subdomains HOT 1
- appending paths to urls HOT 3
- Colons in path are escaped HOT 2
- `mkURI` fails to correctly parse uri when hostname contains a "_" HOT 2
- Empty path with a trailing slash HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modern-uri.