How to make a user defined function atomic?

  If a define function f:{$[0=(x mod 2);1;0]}, I can only apply the function to atoms, e.g. f 3 will work but f til 9 will return `type. However, f each til 0 WILL work. In contrast, function neg WILL work on lists as well as atoms. 

  I understand this this due to the fact my user-defined function f is not atomic. Is there any way to make my function atomic? Also, how can I easily tell whether an existing function is atomic or not? 

  Thanks in advance, 

Hi,

You can work with the initial list of booleans to get what you want

q)f:{$[0=(x mod 2);1;0]}

q)

q)g:{“j”$0=x mod 2}

q)

q)\ts do[100;f each til 10000]

575 393744

q)\ts do[100;g til 10000]

11 524768

q)

q)(g til 10000)~(f each til 10000)

1b

Jason

   Ok, reading Jeff Borror’s book answered the question - because the $ operator is not atomic, it needs the each adverb to be applied to lists. So, to make my function atomic, I would do: 

          fatomic:{{$[0=(x mod 2);1;0]} each x}

   Thanks. 

You can also modify f to be:

f:{$[0=x mod 2;1;0]}@/:

Which will then work on both atoms and lists:

q)type f 12

-7h

q)type f til 9

7h

You could also force atom inputs to be lists with (), but this will cause all outputs to be lists:

q)fx:{?[0=((),x)mod 2;1;0]}

q)type fx 12

7h

q)type fx til 9

7h


From: personal-kdbplus@googlegroups.com <personal-kdbplus@googlegroups.com> on behalf of Jason Fealy <jfealy@kx.com>
Sent: 10 March 2018 19:27:23
To: personal-kdbplus@googlegroups.com
Subject: Re: [personal kdb+] How to make a user defined function atomic?
 

Hi,

You can work with the initial list of booleans to get what you want

q)f:{$[0=(x mod 2);1;0]}

q)

q)g:{“j”$0=x mod 2}

q)

q)\ts do[100;f each til 10000]

575 393744

q)\ts do[100;g til 10000]

11 524768

q)

q)(g til 10000)~(f each til 10000)

1b

Jason

That works for both atoms and lists, but it doesn’t work on a matrix, so it’s not actually atomic. Only g - (in Jason’s reply) fits the bill.

q)fatomic (1 2;3 4)

'type

q)g:{“j”$0=x mod 2}

q)g (1 2;3 4)

0 1

0 1