cancel
Showing results for
Did you mean:

## Functions with select statement New Contributor
Here's something I find perplexing:The following definition of f works:q)t:([]a:til 10;b:10?10f)q)f:{[x;y]select from x where a within y}q)f[t;3 5]a b----------3 6.2542564 3.6673165 9.844223But when [x;y] is omitted, why does it fail with 'rank:q)f:{select from x where a within y}q)f[t;3 5]'rankSwitch around the function arguments and it works even withoutexplicit declaration of [x;y]:q)f:{select from y where a within x}q)f[3 5;t]a b----------3 6.2542564 3.6673165 9.844223Would appreciate it if someone can enlighten me on this rather non-intuitive aspect of function definitions.SH
2 REPLIES 2 New Contributor
On Thu, Nov 20, 2008 at 11:24 PM, Swee Heng wrote:> Here's something I find perplexing:>> The following definition of f works:> q)t:([]a:til 10;b:10?10f)> q)f:{[x;y]select from x where a within y}> q)f[t;3 5]> a b> ----------> 3 6.254256> 4 3.667316> 5 9.844223>> But when [x;y] is omitted, why does it fail with 'rank:> q)f:{select from x where a within y}> q)f[t;3 5]> 'rank>> Switch around the function arguments and it works even without> explicit declaration of [x;y]:> q)f:{select from y where a within x}> q)f[3 5;t]> a b> ----------> 3 6.254256> 4 3.667316> 5 9.844223>> Would appreciate it if someone can enlighten me on this rather non-> intuitive aspect of function definitions.a classic gotchaq (mis)interprets a y in the where clause as a column name if thereare no other indicators that it's a parameterq)unshow get{[x;y]select from x where a within y}(0x0ba0a178a20a040005;`x`y;`symbol\$();,`;0b;,(within;`a;`y);?;"{[x;y]selectfrom x where a within y}")q)unshow get{select from x where a within y}(0x0ba0a178a20a040005;,`x;`symbol\$();,`;0b;,(within;`a;`y);?;"{selectfrom x where a within y}")note the differences in element one (args) of the function objectsanother workaround:q){y;select from x where a within y}[t;3 5]a b----------3 6.2542564 3.6673165 9.844223the same is true of y (and z) in the select clauseq)unshow{select a+y from x}[t;3]'rankq){[x;y]select a+y from x}[t;3]a--3456789101112and probably in the by clause too, tho i can't think of a test off thetop of my head New Contributor
Thanks Aaron! You are right about the by clause too.q)t:([]a:100?5)q){select count a by a+x from y}[0;t]a| a-| --0| 101| 322| 243| 234| 11q){select count a by a+y from x}[t;0]'rankq){[x;y]select count a by a+y from x}[t;0]a| a-| --0| 101| 322| 243| 234| 11On Nov 21, 12:01�am, "Aaron Davies" wrote:> On Thu, Nov 20, 2008 at 11:24 PM, Swee Heng wrote:> > Here's something I find perplexing:>> > The following definition of f works:> > q)t:([]a:til 10;b:10?10f)> > q)f:{[x;y]select from x where a within y}> > q)f[t;3 5]> > a b> > ----------> > 3 6.254256> > 4 3.667316> > 5 9.844223>> > But when [x;y] is omitted, why does it fail with 'rank:> > q)f:{select from x where a within y}> > q)f[t;3 5]> > 'rank>> > Switch around the function arguments and it works even without> > explicit declaration of [x;y]:> > q)f:{select from y where a within x}> > q)f[3 5;t]> > a b> > ----------> > 3 6.254256> > 4 3.667316> > 5 9.844223>> > Would appreciate it if someone can enlighten me on this rather non-> > intuitive aspect of function definitions.>> a classic gotcha>> q (mis)interprets a y in the where clause as a column name if there> are no other indicators that it's a parameter>> q)unshow get{[x;y]select from x where a within y}> (0x0ba0a178a20a040005;`x`y;`symbol\$();,`;0b;,(within;`a;`y);?;"{[x;y]select> from x where a within y}")> q)unshow get{select from x where a within y}> (0x0ba0a178a20a040005;,`x;`symbol\$();,`;0b;,(within;`a;`y);?;"{select> from x where a within y}")>> note the differences in element one (args) of the function objects>> another workaround:>> q){y;select from x where a within y}[t;3 5]> a b> ----------> 3 6.254256> 4 3.667316> 5 9.844223>> the same is true of y (and z) in the select clause>> q)unshow{select a+y from x}[t;3]> 'rank> q){[x;y]select a+y from x}[t;3]> a> --> 3> 4> 5> 6> 7> 8> 9> 10> 11> 12>> and probably in the by clause too, tho i can't think of a test off the> top of my head 