« How portable is q code across operating systems? | Main | What are 0!, 1! and 2!? »

Given a value, how do I get the null of that value's type?

We take advantage of a property of q's indexing: If you request an item from a list (or a dictionary, for that matter) using an index that is out of range, q returns the null item for the list's type.

q)x: 1 2 3
q)x 3

Thus, we can take the item whose corresponding null we want, make a list from it, and then access that list with an out-of-range index:

q)NullOf: {[item] enlist[item] 1}
q)NullOf 1
q)(::) ~ NullOf {x*x}
q)null NullOf (.z.D; 1e; " "; type "foo")

Remember, there is no null list. If you pass a list to NullOf, you get the empty list of the corresponding type:

q)NullOf 1 2 3

See also: .Q.ty

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
All HTML will be escaped. Textile formatting is allowed.