1

How to properly apply a monadic functions and projections in k?

KDB+ 3.6 2018.05.17 Copyright (C) 1993-2018 Kx Systems
q) \
  (5*;10*)@\:2
10 20
  ({x};{x*x})@\:2
2 4
  (#;#)@\:2
(#[2];#[2])

Why 2 first examples work properly and the last one doesn't? I thought it would be:

  (#;#)@\:2
1 1

but it gives me a strange result.

egor7
  • 4,678
  • 7
  • 31
  • 55

3 Answers3

1

Got it!

q)\
  (#;#)@\:2
(#[2];#[2])
  (#:;#:)@\:2
1 1
egor7
  • 4,678
  • 7
  • 31
  • 55
1

For the purpose of completeness, this relates to unary forms which is documented here: https://code.kx.com/q/basics/exposed-infrastructure/#unary-forms

terrylynch
  • 11,844
  • 13
  • 21
1

# (take) is a diadic function, unlike count which is monadic. This is why you were getting a projection when applying only a single argument to it.

q)count
#:
q)type (count)
101h
q)type (#)
102h

You can use the . (dot-apply) operator on diadic functions with two operands to return a result that is not a projection.

(#;#) .\: (3;til 10)
0 1 2
0 1 2
Mark Kelly
  • 1,780
  • 7
  • 16