Code Monkey home page Code Monkey logo

Comments (7)

mrkkrp avatar mrkkrp commented on June 3, 2024 1

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.

mrkkrp avatar mrkkrp commented on June 3, 2024 1

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.

chshersh avatar chshersh commented on June 3, 2024

@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.

mrkkrp avatar mrkkrp commented on June 3, 2024

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.

chshersh avatar chshersh commented on June 3, 2024

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.

chshersh avatar chshersh commented on June 3, 2024

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.

chshersh avatar chshersh commented on June 3, 2024

Oh, I see. Thanks a lot!

from modern-uri.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.