unexpected behaviour

Wednesday, 13 May 2015 · 4 messages
RS
Rob Stapper13 May 2015 09:55 #
Hi Christian, Can you have a look at the attached program. Both items of the returned sequence of the program should do the same thing. Both do give the same, correct result: an empty sequence, but the first item unnecessarely triggers function: "local:f1#1" , while the second item doesn't. This a pity since "local:f1#1" could be a time-consuming function. Cheers, Rob --- Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware. http://www.avast.com
CG
Christian Grün13 May 2015 11:13 #
Hi Rob, It usually takes me some time to understand new code, so could you possibly simplify the example as much as possible? For example, the import cannot be resolved, an empty squence is bound to $a, which means that one branch will never be called, and maybe you can simplify the partial function application as well.. Thanks in advance, Christian
Quoted text · Rob Stapper · 31 lines
On Wed, May 13, 2015 at 9:55 AM, Rob Stapper <r.stapper at lijbrandt.nl> wrote: > Hi Christian, > > > > Can you have a look at the attached program. > > Both items of the returned sequence of the program should do the same > thing. > > Both do give the same, correct result: an empty sequence, but the first > item unnecessarely triggers function: “local:f1#1” , while the second item > doesn’t. > > This a pity since “local:f1#1” could be a time-consuming function. > > > > Cheers, > > Rob > > > ------------------------------ > [image: Avast logo] <http://www.avast.com/> > > Dit e-mailbericht is gecontroleerd op virussen met Avast > antivirussoftware. > www.avast.com > >
CG
Christian Grün13 May 2015 12:36 #
If I get it right, you would also expect '!' not to be output in the following query, right?
  declare function local:f($a, $b) {
    if($a) then $b else ()
  };
  local:f((), trace('!'))
In this case, you'll have to wrap your trace call in a function:
  declare function local:f($a, $b) {
    if($a) then $b() else ()
  };
  local:f((), function() { trace('!') })
Best, Christian
Quoted text · Rob Stapper · 110 lines
On Wed, May 13, 2015 at 11:44 AM, Rob Stapper <r.stapper at lijbrandt.nl> wrote: > Hi Christian, > > > > This is the best I can do without losing the essence of the issue. Sorry > about the import, forgot to remove that one. > > The clue is in the “Query Info”-window. It shows: > > > > Evaluating: > > f1: 1 > > Compiling: > > ……. > > > > So function “local:f1#1” is unnecessarely evaluated with value 1. > > > > Success, > > Rob > > > > *Van:* Christian Grün [mailto:christian.gruen at gmail.com] > *Verzonden:* woensdag 13 mei 2015 11:13 > *Aan:* Rob Stapper > *CC:* BaseX > *Onderwerp:* Re: [basex-talk] unexpected behaviour > > > > Hi Rob, > > > > It usually takes me some time to understand new code, so could you > possibly simplify the example as much as possible? For example, the import > cannot be resolved, an empty squence is bound to $a, which means that one > branch will never be called, and maybe you can simplify the partial > function application as well.. > > > > Thanks in advance, > > Christian > > > > > > > > On Wed, May 13, 2015 at 9:55 AM, Rob Stapper <r.stapper at lijbrandt.nl> > wrote: > > Hi Christian, > > > > Can you have a look at the attached program. > > Both items of the returned sequence of the program should do the same > thing. > > Both do give the same, correct result: an empty sequence, but the first > item unnecessarely triggers function: “local:f1#1” , while the second item > doesn’t. > > This a pity since “local:f1#1” could be a time-consuming function. > > > > Cheers, > > Rob > > > ------------------------------ > > [image: Afbeelding verwijderd door afzender. Avast logo] > <http://www.avast.com/> > > Dit e-mailbericht is gecontroleerd op virussen met Avast > antivirussoftware. > www.avast.com > > > > > > > ------------------------------ > [image: Avast logo] <http://www.avast.com/> > > Dit e-mailbericht is gecontroleerd op virussen met Avast > antivirussoftware. > www.avast.com > >
LW
Leonard Wörtelerin reply to Rob Stapper, 13 May 2015, 09:5513 May 2015 13:58 #
Hi Rob,
Quoted text · Rob Stapper · 4 lines
Am 13.05.2015 um 09:55 schrieb Rob Stapper: > Both do give the same, correct result: an empty sequence, but the first > item unnecessarely triggers function: “local:f1#1” , while the second > item doesn’t.
This is a subtle but important difference between the two ways of partially applying a function f to an argument expression E: * f(E, ?) evaluates E *once* and creates a function item that supplies f with the result. This is preferable if evaluating E is expensive and the resulting function is called often. * function($x) { f(E, $x) } evaluates E every time the created function item is called. This is better if there is at most one call. the following query outputs "f1 f2 f2" from the trace(...) calls:
     let $f1 := concat(trace('f1'), ?),
         $f2 := function($x) { concat(trace('f2'), $x) }
     for $f in ($f1, $f2)
     return ($f('a'), $f('b'))
Hope that helps, Leo

⚡Generated with XQuery