LV's
LV's

Reputation: 41

Empty list creation in kdb with no datatype

I have been learning KDB for a while now & now need your help at one point. We can create empty list using below syntax: mylistname:0#

Example:

q)myEmptySymbolList:0#`
q)myEmptySymbolList
`symbol$()
q)type myEmptySymbolList
11h

q)myEmptyFloatList:0#0n
q)myEmptyFloatList
`float$()
q)type myEmptyFloatList
9h

q)myEmptyDateList:0#0Nd
q)myEmptyDateList
`date$()
q)type myEmptyDateList
14h

But when we do not specify the dataType , it create list of projection function type as below.

q)myEmptyTypeList:0#
q)myEmptyTypeList
#[0]
q)type myEmptyTypeList
104h

I am bit perplexed , why it creates list of projections function type ? If I wanted to create list of other function types how we can do that (like https://code.kx.com/wiki/Reference/Datatypes#Function_Types ) & where in development we can use list of functions ?

Thanks, Vajindar.

Upvotes: 2

Views: 4051

Answers (1)

Ryan McCarron
Ryan McCarron

Reputation: 909

In kdb, taking a zero-count of a scalar or atom value (like 0#1) is effectively a short-cut to creating an empty list.

It can be useful to use this shortcut, but for general purposes you can start with the generic list: () You can then cast this list using $ and either a symbol, character or short:

q)`int$()
`int$()
q)"i"$()
`int$()
q)6h$()
`int$()

These all result in an empty integer list.

In your final part above, # is a dyadic function - you're not actually creating a list of projections here when you do 0#, instead you're creating a single projection. There are no typed lists of functions in kdb:

q)type ({x+1};{x+2})
0h

They always appear as a mixed list (type 0), so you're just seeing the (positive) type of the projection you've created in this case (104).

There is an updated list of types here:

https://code.kx.com/q/ref/datatypes/

And information on casting here:

https://code.kx.com/q/ref/casting/#cast

Upvotes: 4

Related Questions