Forum Replies Created

Page 2 of 2
  • cillianreilly

    Member
    October 27, 2022 at 12:00 am in reply to: Function composition using common argument

    You can do this using iteration over a list of the functions to apply.

    q){z .(y;x)}[a]/[3;(f2;f1)] 
    0n 0.5 0.5 0.3333333 0.25 0.2 0.1666667 0.1428571 0.125 0.1111111

    Adding another function:

    q)f3:+ 
    q){z .(y;x)}[a]/[3;(f2;f1;f3)] 
    0n 1.5 2.5 3.333333 4.25 5.2 6.166667 7.142857 8.125 9.111111

     

  • cillianreilly

    Member
    October 12, 2022 at 12:00 am in reply to: parse tree not working for simple select

    You can also include where in the parse tree passed as the last argument.

    q)?[tab;til 10;(where;(>;`c2;16))] 
    7 8 9
  • cillianreilly

    Member
    October 8, 2022 at 12:00 am in reply to: Underneath q is k

    On your last point, worth mentioning that using brackets to wrap k code is not encouraged: https://code.kx.com/q/basics/exposed-infrastructure/#unary-forms. (I think I heard somewhere that this usage is actually an issue with the parser – but don’t quote me on that.)

  • cillianreilly

    Member
    September 9, 2022 at 12:00 am in reply to: xbar millisecond

    If you don’t cast to minutes first, you can explicitly group by millisecond (assuming your time column is a timestamp):

    q)t:([]time:.z.p+500000*til 20;col:til 20) 
    q)select avg col by 5000000 xbar time from t // millisecond = 1000000 nanoseconds 
    time                         | col 
    -----------------------------| ---- 
    2022.09.09D03:50:38.425000000| 2.5 
    2022.09.09D03:50:38.430000000| 10.5 
    2022.09.09D03:50:38.435000000| 17.5
  • cillianreilly

    Member
    August 19, 2022 at 12:00 am in reply to: Swap key-value in dictionary

    Another method:

    q){a!x a:asc key x:group(!). flip raze key[x],''value x}d 
    1| ,3 
    3| 1 2 
    4| 1 3 
    5| ,1 
    6| ,2 
    7| ,2

     

     

  • cillianreilly

    Member
    August 19, 2022 at 12:00 am in reply to: Swap key-value in dictionary

    This achieves what you’re looking for.

    q)d:1 2 3!(4 5 3;6 7 3;4 1) 
    q)r:1 3 4 5 6 7!(enlist 3;1 2;1 3;enlist 1;enlist 2;enlist 2) 
    q){a!key[x]where each flip value(a:asc distinct raze x)in/:x} d 
    1| ,3 
    3| 1 2 
    4| 1 3 
    5| ,1 
    6| ,2 
    7| ,2 
    
    q)r~{a!key[x]where each flip value(a:asc distinct raze x)in/:x}d 
    1b
  • cillianreilly

    Member
    August 19, 2022 at 12:00 am in reply to: licence error: k4.lic

    What is your QHOME variable set to?

    C:Users>echo %QHOME%

    C:q

    You may need to set it to the correct folder location. It is also helpful to add q to your PATH while you are at it:

    C:Users>setx QHOME “D:kDBw64”

    C:Users>setx PATH “%PATH%;D:kDBw64w64”

    Full installation instructions here: https://code.kx.com/q/learn/install

    (Being pedantic, you can also remove the top level w64 folder in order to neaten up the environmental variables mentioned.)

  • cillianreilly

    Member
    August 18, 2022 at 12:00 am in reply to: set namespace on .q file

    Yes, this is possible using the system command “d”. See here for more info: https://code.kx.com/q/basics/syscmds/#d-directory

     

    q)d .ns q.ns)fn1:fn2:{}

    q)d .

    q).ns | :: fn2| {} fn1| {}

     

     

  • cillianreilly

    Member
    November 16, 2021 at 12:00 am in reply to: Referencing new column

    As David mentioned, you can’t reference iq2 before you create it. If your where clause isn’t too wieldy, you can just include that in the qsql query to create column iq2:

    q)select name,iq2:iq%100 from t where 1<iq%100 
    name    iq2 
    ------------ 
    Prefect 1.26

    This does the calculation twice, so be careful with particularly heavy where conditions.

  • cillianreilly

    Member
    November 11, 2021 at 12:00 am in reply to: Problem with log message when using trap

    The error thrown is passed as the first parameter to the function. https://code.kx.com/q/ref/apply/#trap. To allow parameters, you need to have a function of rank 2 or greater, you can then create a projection of a rank 1 function by eliding the first parameter:

     

    q)logError:{-1"Error: ",x;} 
    q)@[10+;`abc;{logError x,". Couldn't add 10 to ",string y}[;`abc]] 
    Error: type. Couldn't add 10 to abc

     

    You can extend this to allow additional parameters to your error function by creating greater rank functions

  • cillianreilly

    Member
    September 23, 2021 at 12:00 am in reply to: Applying nested conditions in q

    This is most easily done in two parts and then defaulting the rest to welcome:

     

    q)T:update newCol:`hi from T where a=`F,b<c 
    q)T:update newCol:`hi from T where a<>`F,c>d 
    q)T:update`welcome^newCol from T 
    q)T 
    a b c d newCol 
    ------------------ 
    F 10 9 10 welcome 
    R 20 21 22 welcome

     

    You may find the reference on update statements useful: https://code.kx.com/q/ref/update/

    If you want it done in one line (which doesn’t scale well for any additional clauses you might want):

     

    q)update newCol:`welcome`hi any(all(a=`F;b<c);all(a<>`F;c>d))from T 
    a b c d newCol 
    ------------------ 
    F 10 9 10 welcome 
    R 20 21 22 welcome

     

     

  • cillianreilly

    Member
    August 31, 2021 at 12:00 am in reply to: Basic (but still intriguing to me) q Interpreter question

    Hi JP, this is just the underlying k implementation of idesc: https://code.kx.com/q/ref/desc/#idesc

     

    q)a:0 1 1 0 -1 0 1 -1 0 0 
    q)idesc 
    k){$[0h>@x;'`rank;>x]} 
    q)idesc[a]~(>)a 
    1b 
    q) >a 
    1 2 6 0 3 5 8 9 4 7

     

    The brackets allow q to interpret this, but the use is discouraged (https://code.kx.com/q/basics/exposed-infrastructure/#unary-forms). Rather use the idesc keyword instead.

Page 2 of 2