# why is this query working, and what does it do exactly ?

**URL:** <https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [July 10, 2012, 10:35am UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809 "2012-07-10T10:35:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [July 10, 2012, 10:35am UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/1 "2012-07-10T10:35:00Z")

</div>

Hi all,can somebody explain why this query is working?a group a: til 8Regards,Kim

---

<div class="post-metadata">

**Author:** ![Kevin\_Smyth](https://avatars.discourse-cdn.com/v4/letter/k/958977/32.png) [@Kevin\_Smyth](https://forum.kx.com/u/Kevin_Smyth)\
**Post date:** [July 10, 2012, 10:39am UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/2 "2012-07-10T10:39:00Z")

</div>

a is assigned to be an int list from 0-7, then ‘a group a’ maps each  
element of a to its index in the list, try ‘a group reverse a:til 8’

---

<div class="post-metadata">

**Author:** ![Deepak1](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@Deepak1](https://forum.kx.com/u/Deepak1)\
**Post date:** [July 10, 2012, 10:44am UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/3 "2012-07-10T10:44:00Z")

</div>

Hi,

?

This query will create a dictionary where domain contains unique elements and range have elements list appears…

?

? a: til 8

? show a

? 0 1 2 3 4 5 6 7

?

q)a group b  
0| 0  
1| 1  
2| 2  
3| 3  
4| 4  
5| 5  
6| 6  
7| 7

?

q)c: 1 2 3 4 3 2 5 6  
q)c group c  
1| ,1  
2| 2 2  
3| 3 3  
4| ,4  
5| ,5  
6| ,6

Regrads

Hemant

?

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [July 10, 2012, 12:27pm UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/4 "2012-07-10T12:27:00Z")

</div>

Thx for your explanation,

But why is this code executable?  
Normally you cannot forward a map to a list

This code is not executable.  
`b `a`b ! 1 2

But this code is executable:  
`a `a`b ! 1 2

This is work as designed ?

Kim

---

<div class="post-metadata">

**Author:** ![abcfoots1](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@abcfoots1](https://forum.kx.com/u/abcfoots1)\
**Post date:** [July 10, 2012, 1:34pm UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/5 "2012-07-10T13:34:00Z")

</div>

\>This code is not executable.&nbsp;  
\>`b `a`b ! 1 2&nbsp;

\>But this code is executable:&nbsp;

\>`a `a`b ! 1 2&nbsp;

Consider the latter to behave like&nbsp;

q)a: 8?10 &nbsp; &nbsp; &nbsp; / use random vector so you can see the mapping better

q){a@x}`a`b!1,2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; / value is used to index list a. the return value of f[dict] is the original keys and the returned list

a| 1  
b| 9

This is different to the&nbsp;

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [July 10, 2012, 2:31pm UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/6 "2012-07-10T14:31:00Z")

</div>

Cool,thx for your explaination.KimOn 10 Jul., 14:34, abcfoots \<abcfo…\> wrote:\> \>This code is not executable.\> \>`b `a`b ! 1 2&gt; &gt;But this code is executable:&gt; &gt;`a `a`b ! 1 2\>\> Consider the latter to behave like\> q)a: 8?10 ? ? ? / use random vector so you can see the mapping better\> q){a@x}`a`b!1,2 ? ? ? ? ? / value is used to index list a. the return value\> of f[dict] is the original keys and the returned list\> a| 1\> b| 9\>\> This is different to the\>\>\>\> On Tuesday, July 10, 2012 7:27:03 AM UTC-4, kuentang wrote:\>\> \> Thx for your explanation,\>\> \> But why is this code executable?\> \> Normally you cannot forward a map to a list\>\> \> This code is not executable.\> \> `b `a`b ! 1 2&gt;&gt; &gt; But this code is executable:&gt; &gt; `a `a`b ! 1 2\>\> \> This is work as designed ?\>\> \> Kim\>\> \> On 10 Jul., 11:39, Kevin Smyth \<kevin…\> wrote:\> \> \> a is assigned to be an int list from 0-7, then ‘a group a’ maps each\> \> \> element of a to its index in the list, try ‘a group reverse a:til 8’\>\> \> \> On 10 July 2012 10:35, kuentang \<kuent…\> wrote:\>\> \> \> \> Hi all,\>\> \> \> \> can somebody explain why this query is working?\>\> \> \> \> a group a: til 8\>\> \> \> \> Regards,\> \> \> \> Kim\>\> \> \> \> –\> \> \> \> You received this message because you are subscribed to the Google\> \> Groups “Kdb+ Personal Developers” group.\> \> \> \> To post to this group, send email to [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com).\>\> \> \> \> To unsubscribe from this group, send email to\> \> [personal-kdbplus+unsubscribe@googlegroups.com](mailto:personal-kdbplus+unsubscribe@googlegroups.com).\> \> \> \> For more options, visit this group athttp://\> \> [groups.google.com/group/personal-kdbplus?hl=en.-](http://groups.google.com/group/personal-kdbplus?hl=en.-) Zitierten Text\> \> ausblenden -\>\> \> \> - Zitierten Text anzeigen – Zitierten Text ausblenden -\>\> - Zitierten Text anzeigen -\</kuent…\>\</kevin…\>\</abcfo…\>

---

<div class="post-metadata">

**Author:** ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)\
**Post date:** [July 13, 2012, 3:52am UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/7 "2012-07-13T03:52:00Z")

</div>

To: [personal-kdbplus@googlegroups.com](mailto:personal-kdbplus@googlegroups.com)  
X-Mailer: Apple Mail (2.1278)

\> This code is not executable.  
\> `b `a`b ! 1 2 \> \> But this code is executable: \> `a `a`b ! 1 2

some forms of (sym@) mean “apply with left arg passed by name”

q)(a group a)~`a group a  
1b  
q)

if the object named does not exist, that’s an error

q)b group a  
'b  
q)`b group a  
'b  
q)

define it and it starts working

q)b:a  
q)`b group a  
0| 0  
1| 1  
2| 2  
3| 3  
4| 4  
5| 5  
6| 6  
7| 7  
q)

the result, btw, is a form of functional composition, and imao one of =  
the more interesting bits of theory embedded in q:

considering a dict as a (partial) function from its keys to its values, =  
then two dicts f and g st f’s value and g’s key are of the same type can =  
be composed

q)f:`a`b`c!1 2 3 q)g:1 2 3!("foo";"bar";"quux") q)g f a| "foo" b| "bar" c| "quux" q)(g f)`b  
“bar”  
q)

considering a vector as a dictionary from (0..n-1) to its values, then =  
it can be composed with a dictionary of int value

q)v:42 137 23  
q)h:`a`b`c!0 1 2 q)v h a| 42 b| 137 c| 23 q)(v h)`b  
137  
q)

---

<div class="post-metadata">

**Author:** ![kuentang](https://avatars.discourse-cdn.com/v4/letter/k/bc8723/32.png) [@kuentang](https://forum.kx.com/u/kuentang)\
**Post date:** [July 13, 2012, 5:01am UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/8 "2012-07-13T05:01:00Z")

</div>

Cool,thx for the explanation.Btw is this feature somewhere documented ?KimAm 13.07.2012 04:52, schrieb Aaron Davies:\>\> This code is not executable.\>\> `b `a`b ! 1 2\>\>\>\> But this code is executable:\>\> `a `a`b ! 1 2\> some forms of (sym@) mean “apply with left arg passed by name”\>\> q)(a group a)~`a group a\> 1b\> q)\>\> if the object named does not exist, that's an error\>\> q)b group a\> 'b\> q)`b group a\> 'b\> q)\>\> define it and it starts working\>\> q)b:a\> q)`b group a\> 0| 0\> 1| 1\> 2| 2\> 3| 3\> 4| 4\> 5| 5\> 6| 6\> 7| 7\> q)\>\> the result, btw, is a form of functional composition, and imao one of the more interesting bits of theory embedded in q:\>\> considering a dict as a (partial) function from its keys to its values, then two dicts f and g st f's value and g's key are of the same type can be composed\>\> q)f:`a`b`c!1 2 3\> q)g:1 2 3!(“foo”;“bar”;“quux”)\> q)g f\> a| “foo”\> b| “bar”\> c| “quux”\> q)(g f)`b\> "bar"\> q)\>\> considering a vector as a dictionary from (0..n-1) to its values, then it can be composed with a dictionary of int value\>\> q)v:42 137 23\> q)h:`a`b`c!0 1 2\> q)v h\> a| 42\> b| 137\> c| 23\> q)(v h)`b\> 137\> q)\>

---

<div class="post-metadata">

**Author:** ![felix1](https://avatars.discourse-cdn.com/v4/letter/f/3e96dc/32.png) [@felix1](https://forum.kx.com/u/felix1)\
**Post date:** [July 13, 2012, 11:27am UTC](https://forum.kx.com/t/why-is-this-query-working-and-what-does-it-do-exactly/7809/9 "2012-07-13T11:27:00Z")

</div>

yes. here:&nbsp;http://kx.com/q/d/a/q1.htm#Functional Forms of Amend
