'Unlist a singleton list to an atom in kdb

Many times I come across a singleton list like

,1b 
,0Nd

and in order to convert the singleton list to an atom I always use first operator. Example:

q)a: enlist 0Nd
q)a
,0Nd
q)b:null a
q)b
,1b
q)first a
0Nd
q)first b
1b

I'm sure there must be a better way to convert a singleton list to an atom but I'm not able to find that. Can someone please let me know how to unlist a singleton list to an atom?

kdb


Solution 1:[1]

Honestly, first is the best operator. You could index into the list, but that will likely have at best equal performance.

If you're concerned with reliably converting only singleton lists into atoms, you could use the following

q)conform:{$[0< type x;$[1=count x;first x;x];x]}
q)conform a
0Nd
q)conform 3i
3i
q)conform 1 2 3
1 2 3

What is the concern you have with first, it's about as simple and easy as you can get

Solution 2:[2]

To generalise, if given a singleton array, the following function extracts its sole atom:

q)atom:{if[1=count a:(raze/)x;:first a];'"atom"}
q)@[atom;;"cannot"] each (`a;enlist enlist `a;();enlist 1 2;enlist enlist 1 2; enlist ())
`a
`a
"cannot"
"cannot"
"cannot"
"cannot"


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Daniel Krizian