ELM get query parameter as string - functional-programming

Based on this post and thanks to the #glennsl iam getting some where.
First if someone has a link that i could learn about the parses i will be very glad.
page : Url.Url -> String
page url =
case (Parser.parse (Parser.query (Query.string "name")) url) of
Nothing -> "My query string: " ++ (Maybe.withDefault "empty" url.query)
Just v -> case v of
Just v2 -> "Finnaly a name"
Nothing -> "????"
As far i can understand the expression Parser.parse (Parser.query (Query.string "name")) urlis returning a Maybe (Maybe String) I see this as the parser could return something, and if do it could be an string, is that right?
In my mind if i have the parameter name in my url then my first Just would be executed and then i can get the name.
But no mather what i put on my url it always go the the first Nothing
The result i got

The problem is that you're not parsing the path part of the URL, which is what Url.Parser is primarily for. You have to match the path exactly.
Here's a parser that will match your URL:
s "src" </> s "Main.elm" <?> (Query.string "name")
Note also that parsing the query string is optional, meaning this will also match your URL:
s "src" </> s "Main.elm"
But as long as you include a query param parser, that also has to match.
If all you care about is the query parameter, you'll have to parse the query string specifically, by either writing your own function to do so, or using a library like qs for example:
QS.parse
QS.config
"?a=1&b=x"
== Dict.fromList
[ ( "a", One <| Number 1 )
, ( "b", One <| Str "x" )
]

Related

How to convert string to XPATH in BaseX

How can i convert string into XPATH, below is the code
let $ti := "item/title"
let $tiValue := "Welcome to America"
return db:open('test')/*[ $tiValue = $ti]/base-uri()
Here is one way to solve it:
let $ti := "item/title"
let $tiValue := "Welcome to America"
let $input := db:open('test')
let $steps := tokenize($ti, '/')
let $process-step := function($input, $step) { $input/*[name() = $step] }
let $output := fold-left($input, $steps, $process-step)
let $test := $output[. = $tiValue]
return $test/base-uri()
The path string is split into single steps (item, title). With fold-left, all child nodes of the current input (initially db:open('test')) will be matched against the current step (initially, item). The result will be used as new input and matched against the next step (title), and so on. Finally, only those nodes with $tiValue as text value will be returned.
Your question is very unclear - the basic problem is that you've shown us some code that doesn't do what you want, and you're asking us to work out what you want by guessing what was going on in your head when you wrote the incorrect code.
I suspect -- I may be wrong -- that you were hoping this might somehow give you the result of
db:open('test')/*[item/title = $ti]/base-uri()
and presumably $ti might hold different path expressions on different occasions.
XQuery 3.0/3.1 doesn't have any standard way to evaluate an XPath expression supplied dynamically as a string (unless you count the rather devious approach of using fn:transform() to invoke an XSLT transformation that uses the xsl:evaluate instruction).
BaseX however has an query:eval() function that will do the job for you. See https://docs.basex.org/wiki/XQuery_Module

Understanding Elm's Type Signature return types

I am trying to understand elm's type signatures. What does this function return exactly? It appears to be a function that accepts no arguments and returns ...
route : Parser (Page -> a) a
As a learning exercise for myself I'm going to try to answer this. Others will chip in if I get something wrong.
I'm sure you are used to something like
type Person = Adult String | Child String Age
Child is a type that takes two parameters. Parser is the same. But it's definition is pretty formidable
type Parser a b =
Parser (State a -> List (State b))
type alias State value =
{ visited : List String
, unvisited : List String
, params : Dict String String
, value : value
}
That said, you see how Parser is ultimately a wrapper around a function from a State to a list of States. Ultimately it is going to be passed a List of 'unvisited' strings or params; it will progressively 'visit' each one and the result will be combined into the final 'value'.
Next, note that while Parser takes two type parameters - a, b - parseHash is defined
parseHash : Parser (a -> a) a -> Location -> Maybe a
So, your original
route : Parser (Page -> a) a
is going to have to be
route : Parser (Page -> Page) Page
to type check.
To return to your original question, therefore, route is a Parser (which is a very general object) that encapsulates instructions on how to go from one Page to another, and can be used - via parseHash - to tell you what Page to go to next, and that is of course what you would expect from a router.
Hope this gets you started

Need URI list in marklogic database using Xquery

After executing the below xquery, resulted in whole content from XML but my objective is to get the list of URI.
let $i := cts:search(//root,
cts:element-value-query(
xs:QName("no"),
"123"))
return ($i)
If all you want is the URI, use cts:uris(). The 3rd parameter lets you define a query that will filter the URIs list.
So with your example this would work:
cts:uris(
(),
(),
cts:element-value-query(
xs:QName("no"),
"123")
)

Pass query params to Erlang HTTP Request

I'm trying to send parameters with an URL, like http://localhost:3000/register?name=Chris&job=typist. I can send that all at once as a string with httpc:request, but I can't find a function to put the query parameters in the URL(given a dictionary).
Is there another HTTP library I should be using that has this capability?
I'd like to give it a root URL with a hash/dictonary/map (in json {"a" : "b", "c" : "d"}) that then appends it correctly to the end of the url. For example, given "www.facebook.com" and [{"a", "b"}, {"c", "d"}] would give "www.facebook.com?a=b&c=d".
Here is a similar question for Ruby: Ruby: How to turn a hash into HTTP parameters?
I'm not sure exactly what you mean by "hash", but if you'd like to construct a query string from tuples, that is a fairly straitforward task.
I'm not familiar with a method in httpc to provide the functionality you desire. You can write a wrapper around request/4 very easily, similar to this.
(This program is hastily constructed to give you the idea, forgive any errors).
request(Method, Domain, {Path, Query, Fragment}, HTTPOptions, Options) ->
QueryString = lists:flatten([Path,
case Query of "" -> ""; _ -> [$? | Query] end,
case Fragment of "" -> ""; _ -> [$# | Fragment] end]);
Request = concat(Domain, QueryString);
httpc:request(Method, {Request, []}, HTTPOptions, Options).
You can invoke it like
request(get, "http://www.example.com", {"/path", "", "bar?baz}, HTTPOptions, Options)
try this function
test(URL,QP)->URL++"?"++loop(QP,[]).
loop([{A,B}],QP)->QP++A++"="++B;
loop([{A,B}|T],QP)->loop(T,QP++A++"="++B++"&").
call test("www.facebook.com",[{"a", "b"}, {"c", "d"}]).
it returns "www.facebook.com?a=b&c=d".

Lift Web Framework query string generator

How do you create a link with query string parameters:
/path/to/view?param=358&name=Something+with+spaces
in Lift? I know you can simply write it, I am looking for a wise approach, which encode spaces and other special characters. For example:
Link("path/to/view").param("param", 358).param("name", "Something with spaces")
Thanks in advance,
Etam.
There is appendParams method in net.liftweb.util.HttpHelpers trait:
import net.liftweb._
import util.Helpers._
val url = appendParams("/path/to/view",
("param" -> "358") ::
("name" -> "Something with spaces") :: Nil)
Reply from Scala REPL:
url: String = /path/to/view?param=358&name=Something+with+spaces
As you can see, it gets URL as a String, Seq of param tuples and finally returns String.

Resources