Function within a function

82 views
Skip to first unread message

Dean Williams

unread,
Mar 15, 2021, 3:32:43 PM3/15/21
to Kdb+ Personal Developers
I am struggling with how to map a list into a function.

Each item in the list, consists of a start date, end date, int and symbol.

q)requests:((2021.06.07;2021.06.09;53696;`Sent);(2021.06.12;2021.06.14;81840;`Sent))

I then have a function that creates all dates in between the start date and end date, then fills a table:

q)fillDates:{[a;b;c;d] f:a + til (b - a) + 1;([]Date:`date$(f);CrewID:`int$(c);Status:`symbol$(d))}

q)fillDates[2021.06.07;2021.06.09;53696;`Sent]

Date       CrewID Status

------------------------

2021.06.07 53696  Sent  

2021.06.08 53696  Sent  

2021.06.09 53696  Sent

Where I am having difficulty is trying to process all items in the requests list, something like:

fillDates[ ] each requests

Such that the  result would look like this:

Date       CrewID Status

------------------------

2021.06.07 53696  Sent  

2021.06.08 53696  Sent  

2021.06.09 53696  Sent

2021.06.12 81840  Sent  

2021.06.13 81840  Sent  

2021.06.14 81840  Sent

When I try:

q)fillDates[requests]

{[a;b;c;d] f:a + til (b - a) + 1;([]Date:`date$(f);CrewID:`int$(c);Status:`symbol$(d))}[((2021.06.07;2021.06.09;53696;`Sent);(2021.06.12;2021.06.14;81840;`Sent))]

q)type fillDates[requests]

104h

I get a projection of the function and I am not sure how to proceed. 

I would be grateful if somebody could point out a better way than my tortured approach.

Many thanks

Dean

David Demner

unread,
Mar 15, 2021, 4:19:58 PM3/15/21
to personal...@googlegroups.com

You want . (index/apply) https://code.kx.com/q/ref/apply/

and /: (each-right) https://code.kx.com/q/ref/maps/

and raze https://code.kx.com/q/ref/raze/

 

Combining these three to end up with:

q)requests:((2021.06.07;2021.06.09;53696;`Sent);(2021.06.12;2021.06.14;81840;`Sent))

q)fillDates:{[a;b;c;d] f:a + til (b - a) + 1;([]Date:`date$(f);CrewID:`int$(c);Status:`symbol$(d))}

q)raze fillDates ./: requests

Date       CrewID Status

------------------------

2021.06.07 53696  Sent

2021.06.08 53696  Sent

2021.06.09 53696  Sent

2021.06.12 81840  Sent

2021.06.13 81840  Sent

2021.06.14 81840  Sent

q)

 

From: personal...@googlegroups.com [mailto:personal...@googlegroups.com] On Behalf Of Dean Williams
Sent: Monday, March 15, 2021 12:33 PM
To: Kdb+ Personal Developers <personal...@googlegroups.com>
Subject: [personal kdb+] Function within a function

 

CAUTION: External email. Do not click links or open attachments unless you recognize the sender and know the content is safe.

--
You received this message because you are subscribed to the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to personal-kdbpl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/personal-kdbplus/f055dbdc-0b3b-426c-8c07-2a42c29fb26dn%40googlegroups.com.


This email, its contents and any files attached are a confidential communication and are intended only for the named addressees indicated in the message.

If you are not the named addressee or if you have received this email in error, you may not, without the consent of AquaQ Analytics, copy, use or rely on any information or attachments in any way. Please notify the sender by return email and delete it from your email system.

Unless separately agreed, AquaQ Analytics does not accept any responsibility for the accuracy or completeness of the contents of this email or its attachments. Please note that any views, opinion or advice contained in this communication are those of the sending individual and not those of AquaQ Analytics and AquaQ Analytics shall have no liability whatsoever in relation to this communication (or its content) unless separately agreed.

This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of AquaQ Analytics may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.

Dean Williams

unread,
Mar 15, 2021, 4:25:23 PM3/15/21
to Kdb+ Personal Developers
Many, many thanks David.

Dean Williams

unread,
Mar 15, 2021, 4:47:06 PM3/15/21
to personal...@googlegroups.com

Alvi Kabir

unread,
Mar 15, 2021, 7:13:58 PM3/15/21
to personal...@googlegroups.com
You can vectorize parts of the function and have it perform faster (half the time) as well:

requests:1000000#((2021.06.07;2021.06.09;53696;`Sent);(2021.06.12;2021.06.14;81840;`Sent))

\t a:raze fillDates ./: requests

2346

fillDates2:{[a;b;c;d]dt:a+til each 1+b-a;cnt:count each dt;data:raze each(dt;cnt#'c;cnt#'d);([]Date:data 0;CrewID:`int$data 1;Status:data 2)}

\t b:fillDates2 . flip requests

1215

a~b

1b




--
Sincerely,

Alvi Kabir

Dean Williams

unread,
Mar 15, 2021, 8:51:16 PM3/15/21
to personal...@googlegroups.com
Alvi
 
That was very a interesting solution. Thank you.
Reply all
Reply to author
Forward
0 new messages