cancel
Showing results for 
Search instead for 
Did you mean: 

composition confusion

newstudent2017
New Contributor II

Composition has many "weired" syntax . Are they overloads of adverbs ?

Is '[f ff] overload of ' adverb 

Another example is '[;]/[(g;f;ff)] 

1 REPLY 1

SJT
Valued Contributor
Valued Contributor

The q language overloads many symbols, and yes, quote is one of them. It most commonly denotes the Each iterator, but in your examples it denotes the Compose operator

This overloading makes precise terminology particularly important when discussing q syntax, which is why we revised the terminology and stopped using terms such as adverb

In your first example, '[f;ff] returns a derived function, equivalent to {f ff x}.

The extra execution overhead of using a lambda is tiny, which is probably why you are more likely to find the lambda in code. But because Compose is a binary operator, you can use '[;] to reduce a list of (mostly) unary functions or operators. (You need to use '[;] rather than ' in order to resolve the overloading.) 

So, for example, if f, g, and h are unaries, and ff is a binary, then ('[;])over(f;g;h;ff) is equivalent to {f g h ff x}. In most cases you would probably choose to write the lambda. But if the choice (or even the number) of functions is to be made at runtime, then the reduction could be just the way to put it all together.

It is worth remembering here that lists and dictionaries are also unaries. If you had a sequence of mappings to apply, with the choice of mappings determined at runtime (perhaps columns picked from a table) then Compose Over – ('[;])over – might be your best way to assemble it.