Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
September 09, 2015 using std.algorithm to find intersection of DateTime[][] arg | ||||
---|---|---|---|---|
| ||||
I have a DateTime[][] arg (actually my own date type, but that shouldnt matter). Ordered by time series ticker (eg a stock) and then the date. eg arg[0] might be the dates relating to IBM and arg[0][0] would be the date of the first point in IBM time series. I would like to find the intersection of the dates. so setIntersection(arg[0],arg[1],arg[2] .. arg[$-1]) except that I don't know how many series are in arg at compile time. what's the most efficient way to use Phobos to find these? (I could write a loop, but I am trying to learn how to use std.algorithm better). |
September 10, 2015 Re: using std.algorithm to find intersection of DateTime[][] arg | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | Couldn't you use setIntersection together with reduce? Doesn't seem like the most efficient solution, but its less typing and most likely will have no bugs. |
September 10, 2015 Re: using std.algorithm to find intersection of DateTime[][] arg | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | On Wednesday, 9 September 2015 at 20:28:35 UTC, Laeeth Isharc wrote: > > so setIntersection(arg[0],arg[1],arg[2] .. arg[$-1]) > except that I don't know how many series are in arg at compile time. > > what's the most efficient way to use Phobos to find these? (I could write a loop, but I am trying to learn how to use std.algorithm better). I'd use something like this, it works in O(Σ|arg[i]| * log|arg.length|) > arg.nWayUnion.group.filter!(g => g[1] == arg.length).map!(g => g[0]).array |
September 10, 2015 Re: using std.algorithm to find intersection of DateTime[][] arg | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | On Wednesday, 9 September 2015 at 20:28:35 UTC, Laeeth Isharc wrote: > I have a DateTime[][] arg ... > I would like to find the intersection of the dates. A suggestion: auto minLength = arg.map!(a => a.length).reduce!min; auto minIdx = arg.map!(a => a.length).countUntil(minLength); auto intersection = arg[minIdx].filter!(e => chain(arg[0..minIdx], arg[minIdx..$]).all!(a => a.assumeSorted.contains(e))); reduce with setIntersection seems the most straightforward, but needs array AFAIK, i.e.: auto intersection = //reduce!((r, x) => setIntersection(r, x))(arg); // doesn't work reduce!((r, x) => setIntersection(r, x).array)(arg);// does, but with array |
September 11, 2015 Re: using std.algorithm to find intersection of DateTime[][] arg | ||||
---|---|---|---|---|
| ||||
Posted in reply to deed | On Thursday, 10 September 2015 at 11:58:10 UTC, deed wrote:
> On Wednesday, 9 September 2015 at 20:28:35 UTC, Laeeth Isharc wrote:
>> I have a DateTime[][] arg ...
>> I would like to find the intersection of the dates.
>
> A suggestion:
>
> auto minLength = arg.map!(a => a.length).reduce!min;
> auto minIdx = arg.map!(a => a.length).countUntil(minLength);
> auto intersection = arg[minIdx].filter!(e => chain(arg[0..minIdx], arg[minIdx..$]).all!(a => a.assumeSorted.contains(e)));
>
> reduce with setIntersection seems the most straightforward, but needs array AFAIK, i.e.:
>
> auto intersection =
> //reduce!((r, x) => setIntersection(r, x))(arg); // doesn't work
> reduce!((r, x) => setIntersection(r, x).array)(arg);// does, but with array
Thank you for this - exactly what I was looking for - and for the other answer.
Laeeth.
|
Copyright © 1999-2021 by the D Language Foundation