Hi Dirk, Rob,
I realize now that the example I gave the wrong impression. I am looking for something general. That's why I added the link to the Clojure apply function which has a couple of examples which show what it should do.
Glossing over the hof functions in XQuery 3 and the hof module again I realize that this is a function that really seems to be lacking and which plays a very important role in functional languages, or at least in Lispy languages and to my knowledge.
Say you have this pseudocode:
$fn = foo(a,b,c) $args = (1,2,3)
$fn($args) obviously doesn't work as it's interpreted as $fn((1,2,3)). What apply does is to apply the function as in $fn(1,2,3). Fold doesn't get me there because it does an application for each argument.
Currently I can do this with one argument functions $fn($args[1]) and if I'm not mistaken soon we can do $args[1] => $fn with the arrow operator. I was thinking, hoping, that the semantics of the arrow operator would allow this but apparently it won't. But just by looking at the following code:
(1,2,3) => $fn
My mind goes $fn(1,2,3). But to XQuery it means $fn((1,2,3)) probably due to the flattening of nested sequences cannot do this as you would loose the ability to provide a sequence as a single argument.
Now some more context as the example I gave was misleading and conflated two different issues.
In my code I first compile a sequence of routes into handler functions. Then when a request comes in I pass it as a map ($request) to this handler function.
Until now my code looks somewhat like this.
declare function handler($request as map(*)) { ... };
$route-handler = def-route('GET', '/hello/{name}', handler#1)
This way I can apply a request to this handler:
$route-handler($request)
This is all working fine.
The downside is that all the handlers that a user writes must always have the same signature: $request as map(*). I want to de-couple this so that a user can write a handler function like:
function greet($name as xs:string, $greeting as xs:string) { fn:content('Hello ', $name, ' ', $greeting) }
Then in def-route:
$route-handler = def-route('GET', '/hello/{name}/{greeting}', ('name', 'greeting'), greet#2)
This keeps the handler function free of having to deal with the request map directly and also have the usual signature checking on request handling. def-route will ensure that the handler knows how to take the 'name' and 'greeting' params from the request map and to apply this two string sequence to the function.
There are probably other ways of getting around this if it turns out not to be possible but they would be less elegant.
Cheers, --Marc
On Thu, Aug 14, 2014 at 8:49 AM, Dirk Kirsten dk@basex.org wrote:
Hi Rob, Hi Marc,
As Rob showed, HOF functions are a possible (and I think actually the only way, although I am not sure) way to deal with functions like concat, which do expect a variable number of arguments. This is very valid and might be a valid solution. I would just like to add that you should keep the performance in mind. fold-left() of course comes to a price. A simple comparison using fold vs. using string-join
declare variable $args := let $a := ('a', 'b', 'c') for $i in 1 to 50000 return $a[random:integer(3) + 1];
declare function local:apply($fn, $args) { $fn($args) };
declare function local:apply2($fn, $args) { fold-left($args, '', $fn) };
( prof:time(local:apply2(concat#2, $args)), prof:time(local:apply(fn:string-join#1, $args)) )
shows a huge performance impact (around 500ms for fold, 0.01ms for string-join).
This is not me saying you should not use fold (in fact it is a very nice and elegant function), but that you might want to consider the performance impact.
Cheers, Dirk
On 14/08/14 08:15, Rob Stapper wrote:
Par example:
declare variable $args := ('a', 'b', 'c');
declare function local:apply
( $fn , $args ) { fold-left( $args , '' , $fn ) } ;
local:apply( concat#2, $args)
Van: basex-talk-bounces@mailman.uni-konstanz.de [mailto:basex-talk-bounces@mailman.uni-konstanz.de] Namens Marc van Grootel Verzonden: woensdag 13 augustus 2014 22:16 Aan: BaseX Onderwerp: [basex-talk] Apply variable argument list to anonymous function
Hi,
I am trying to call an anonymous function with a variable argument list.
Maybe I'm overlooking something within XQuery but I cannot figure out how to do this.
This is the code I would like to get working.
declare function local:apply($fn, $args) {
$fn($args)
};
declare variable $concat := fn:concat#3;
declare variable $args := ('a', 'b', 'c'); (: needs to handle any list of strings :)
local:apply($concat, $args)
(: => 'abc' :)
What I want local:apply to do is similar to apply in some function languages such as Clojure (http://clojuredocs.org/clojure_core/clojure.core/apply)
In the code above I have two problems. a) how to bind the fn:concat function to a variable. Arity? #1, #2 ... ? b) how to "apply" a variable argument list to the function passed in to local:apply.
I figure I could do something maybe with some other higher order functions but I cannot see the solution. I don't mind rtfm responses, I may have missed it.
I keep bombarding the list with questions so it is only fair to give a bit of context. I am porting parts of a few small Clojure libraries to XQuery, most important libraries are Ring and Compojure which are libraries for use in web frameworks. My implementation currently builds on top of RESTXQ but eventually can provide an alternative way of handling HTTP routing requests through function handlers (without function annotations). This is similar to WSGI in Python and Rack in Ruby. I would've liked to release this sooner but it is more work than anticipated and I want to provide something that does these ideas justice. So it's ready when it's ready ;-) but I'm pretty close. I'm currently finalizing the routing part and need to work some more on middleware handlers. Until then go look at https://github.com/ring-clojure to see what this is about.
--Marc
Dit e-mailbericht bevat geen virussen en malware omdat avast! Antivirus-bescherming actief is. http://www.avast.com
-- Dirk Kirsten, BaseX GmbH, http://basex.org |-- Firmensitz: Blarerstrasse 56, 78462 Konstanz |-- Registergericht Freiburg, HRB: 708285, Geschäftsführer: | Dr. Christian Grün, Dr. Alexander Holupirek, Michael Seiferle `-- Phone: 0049 7531 28 28 676, Fax: 0049 7531 20 05 22