Filter functions: bind-to-function without having to use lambda literal

Hi everyone,

Currently, if I want to use a variable as my filter function inside a lambda, the variable can only be a global variable, otherwise, the function is unable to access the filter function at runtime. My question is: is there a way to do this without having to paste the literal definition of the lambda or having to use a global variable?

q)f:{ch:{$[type[x]<0;x;'type]};{[x:ch]x}}[]
q)f
{[x:ch]x}
q)f 3
'ch
  [1]  f:{[x:ch]x}

// workarounds:
// 1. use global instead -- litters the global namespace
q)f:{ch::{$[type[x]<0;x;'type]};{[x:ch]x}}[]
q)f 3
3

// 2. just use lambda -- can't reuse it
q)f:{[x:{$[type[x]<0;x;'type]}]x}
q)f 3
3

Since the check is generic enough, you could assign to a global namespace the checks you wish to reuse, e.g. .ch.atom:{$[type[x]<0;x;'`type]} and then reference from anywhere.

If they must be defined within the lambda, you can pass into the function that requires filtering the check function so it’s in scope for the check

q)f:{ch:{$[type[x]<0;x;'`type]}; {[x:y;y]x}[x;ch]}
q)f 3
3
q)f 1 2
'type
  [3]  f@:{$[type[x]<0;x;'`type]}
                         ^
  [2]  f@:{[x:y;y]x}

One other note on the code: 'type is not a valid error code, that attempts to throw the function type as the error code rather than an error with the message "type", you're looking for '`type

q)'`type
'type
  [0]  '`type
        ^
q)'type
'stype
  [0]  'type
        ^