cancel
Showing results for 
Search instead for 
Did you mean: 

Function within a function

deanwilliams11
New Contributor
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

5 REPLIES 5

david_demner
New Contributor

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-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] On Behalf Of Dean Williams
Sent: Monday, March 15, 2021 12:33 PM
To: Kdb+ Personal Developers <personal-kdbplus@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.

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

--
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-kdbplus+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/personal-kdbplus/f055dbdc-0b3b-426c-8c07-2a42c29fb26dn%40googlegr....


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.

Many, many thanks David.

deanwilliams11
New Contributor
Thank you Brian


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


Alvi
 
That was very a interesting solution. Thank you.