Re: [Request] A way to extract all instance of X from a range
On Monday, 21 March 2016 at 11:34:15 UTC, Nick Treleaven wrote: There is a pull for Option: https://github.com/D-Programming-Language/phobos/pull/3915 We could have: // fun takes r.front and produces an Option of that type auto mapFilter(alias fun, R)(R r); // turn a possibly null value into an Option Option!T nullFilter(T)(T v) if (isNullable!T); auto src = [new Object(), new T(), null]; auto res = mapFilter!(e => nullFilter(cast(T)e)); assert(res.equal([src[1]])); Using the interface proposed in the PR: auto src = [new Object(), new T(), null]; assert(src.map!(e => option(cast(T)e)).joiner.equal(only(src[1])));
Re: [Request] A way to extract all instance of X from a range
On Wednesday, 23 March 2016 at 11:36:39 UTC, Nick Treleaven wrote: On Tuesday, 22 March 2016 at 20:09:51 UTC, Matthias Bentrup wrote: It is logically just a combination of map and concat (which turns a range of ranges into a combined range, but I think that one is missing in the std lib too). http://dlang.org/phobos/std_range.html#.chain Or joiner(), if you have a range of ranges: http://dlang.org/phobos/std_algorithm_iteration.html#.joiner
Re: [Request] A way to extract all instance of X from a range
On Tuesday, 22 March 2016 at 20:09:51 UTC, Matthias Bentrup wrote: It is logically just a combination of map and concat (which turns a range of ranges into a combined range, but I think that one is missing in the std lib too). http://dlang.org/phobos/std_range.html#.chain
Re: [Request] A way to extract all instance of X from a range
On Monday, 21 March 2016 at 11:50:06 UTC, Timothee Cour wrote: On Mon, Mar 21, 2016 at 4:34 AM, Nick Treleaven via Digitalmars-d < digitalmars-d@puremagic.com> wrote: On 14/03/2016 11:32, thedeemon wrote: filter_map : ('a -> 'b option) -> 'a t -> 'b t "filter_map f e returns an enumeration over all elements x such as f y returns Some x, where y is an element of e." It is really convenient and comes handy in many situations. However it requires some common Option/Maybe type that different libraries could use. There is a pull for Option: https://github.com/D-Programming-Language/phobos/pull/3915 We could have: // fun takes r.front and produces an Option of that type auto mapFilter(alias fun, R)(R r); // turn a possibly null value into an Option Option!T nullFilter(T)(T v) if (isNullable!T); auto src = [new Object(), new T(), null]; auto res = mapFilter!(e => nullFilter(cast(T)e)); assert(res.equal([src[1]])); see my proposal [+implementation] for emit http://forum.dlang.org/post/mailman.538.1458560190.26339.digitalmar...@puremagic.com emit is more powerfull, and generalizes map,filter,joiner auto res = src.mapFilter!(e=>nullFilter(cast(T)e)); with emit: auto res = src.emit!((put,e){if(cast(T)e) put(e);}); Why don't you go for the well-established monadic bind function ? A rangified bind would take a range of inputs, a lambda returning a range of results for one input and return a range of all results. It is logically just a combination of map and concat (which turns a range of ranges into a combined range, but I think that one is missing in the std lib too).
Re: [Request] A way to extract all instance of X from a range
On Mon, Mar 21, 2016 at 4:34 AM, Nick Treleaven via Digitalmars-d < digitalmars-d@puremagic.com> wrote: > On 14/03/2016 11:32, thedeemon wrote: > >> >> filter_map : ('a -> 'b option) -> 'a t -> 'b t >> >> "filter_map f e returns an enumeration over all elements x such as f y >> returns Some x, where y is an element of e." >> >> It is really convenient and comes handy in many situations. However it >> requires some common Option/Maybe type that different libraries could use. >> > > There is a pull for Option: > https://github.com/D-Programming-Language/phobos/pull/3915 > > We could have: > > // fun takes r.front and produces an Option of that type > auto mapFilter(alias fun, R)(R r); > > // turn a possibly null value into an Option > Option!T nullFilter(T)(T v) if (isNullable!T); > > auto src = [new Object(), new T(), null]; > auto res = mapFilter!(e => nullFilter(cast(T)e)); > assert(res.equal([src[1]])); > > > see my proposal [+implementation] for emit http://forum.dlang.org/post/mailman.538.1458560190.26339.digitalmar...@puremagic.com emit is more powerfull, and generalizes map,filter,joiner auto res = src.mapFilter!(e=>nullFilter(cast(T)e)); with emit: auto res = src.emit!((put,e){if(cast(T)e) put(e);});
Re: [Request] A way to extract all instance of X from a range
On 14/03/2016 11:32, thedeemon wrote: filter_map : ('a -> 'b option) -> 'a t -> 'b t "filter_map f e returns an enumeration over all elements x such as f y returns Some x, where y is an element of e." It is really convenient and comes handy in many situations. However it requires some common Option/Maybe type that different libraries could use. There is a pull for Option: https://github.com/D-Programming-Language/phobos/pull/3915 We could have: // fun takes r.front and produces an Option of that type auto mapFilter(alias fun, R)(R r); // turn a possibly null value into an Option Option!T nullFilter(T)(T v) if (isNullable!T); auto src = [new Object(), new T(), null]; auto res = mapFilter!(e => nullFilter(cast(T)e)); assert(res.equal([src[1]]));
Re: [Request] A way to extract all instance of X from a range
On Tuesday, 15 March 2016 at 07:42:58 UTC, Stefan Koch wrote: On Tuesday, 15 March 2016 at 04:05:11 UTC, Meta wrote: I believe this should work with the latest DMD: alias castRange(T) = t => t.map!(x => cast(T) x).filter!(x => x !is null); No it does not. However this works : auto typeFilter(T, Range)(Range range) { import std.algorithm : filter, map; import std.traits : ForeachType; static assert(is(T == class) && is(T : ForeachType!Range), "typeFilter only works with classes that are derived form each other"); return range.map!(x => cast(T) x).filter!(x => x !is null); } You're Welcome. What version of DMD are you using? It compiles and a small test works for me on 2.070.2.
Re: [Request] A way to extract all instance of X from a range
On Tuesday, 15 March 2016 at 04:05:11 UTC, Meta wrote: I believe this should work with the latest DMD: alias castRange(T) = t => t.map!(x => cast(T) x).filter!(x => x !is null); No it does not. However this works : auto typeFilter(T, Range)(Range range) { import std.algorithm : filter, map; import std.traits : ForeachType; static assert(is(T == class) && is(T : ForeachType!Range), "typeFilter only works with classes that are derived form each other"); return range.map!(x => cast(T) x).filter!(x => x !is null); } You're Welcome.
Re: [Request] A way to extract all instance of X from a range
On Monday, 14 March 2016 at 23:34:37 UTC, Stefan Koch wrote: On Monday, 14 March 2016 at 08:04:18 UTC, deadalnix wrote: Right now, I'm repeating the following pattern many times : range.map!(x => cast(Foo) x).filter!(x => x !is null) Which is kind of annoying. Could we get something in phobos to do this ? There you go ;) alias castRange(T) = map!(x => cast(T) x).filter!(x => x !is null); I believe this should work with the latest DMD: alias castRange(T) = t => t.map!(x => cast(T) x).filter!(x => x !is null);
Re: [Request] A way to extract all instance of X from a range
On Monday, 14 March 2016 at 23:34:37 UTC, Stefan Koch wrote: On Monday, 14 March 2016 at 08:04:18 UTC, deadalnix wrote: Right now, I'm repeating the following pattern many times : range.map!(x => cast(Foo) x).filter!(x => x !is null) Which is kind of annoying. Could we get something in phobos to do this ? There you go ;) alias castRange(T) = map!(x => cast(T) x).filter!(x => x !is null); Ooops ;) didn't work then it'll has to be done with a wrapper struct ...
Re: [Request] A way to extract all instance of X from a range
On Monday, 14 March 2016 at 08:04:18 UTC, deadalnix wrote: Right now, I'm repeating the following pattern many times : range.map!(x => cast(Foo) x).filter!(x => x !is null) Which is kind of annoying. Could we get something in phobos to do this ? There you go ;) alias castRange(T) = map!(x => cast(T) x).filter!(x => x !is null);
Re: [Request] A way to extract all instance of X from a range
On Monday, 14 March 2016 at 08:04:18 UTC, deadalnix wrote: Right now, I'm repeating the following pattern many times : range.map!(x => cast(Foo) x).filter!(x => x !is null) Which is kind of annoying. Could we get something in phobos to do this ? you could use an alias. alias NullFliter = filter!(x => x !is null);
Re: [Request] A way to extract all instance of X from a range
On Monday, 14 March 2016 at 08:04:18 UTC, deadalnix wrote: Right now, I'm repeating the following pattern many times : range.map!(x => cast(Foo) x).filter!(x => x !is null) This reminds me of a function in OCaml extended stdlib that I've used quite often and really miss in D: filter_map : ('a -> 'b option) -> 'a list -> 'b list "filter_map f l call (f a0) (f a1) (f an) where a0..an are the elements of l. It returns the list of elements bi such as f ai = Some bi (when f returns None, the corresponding element of l is discarded)." Or its variant for OCaml's analog of ranges: filter_map : ('a -> 'b option) -> 'a t -> 'b t "filter_map f e returns an enumeration over all elements x such as f y returns Some x, where y is an element of e." It is really convenient and comes handy in many situations. However it requires some common Option/Maybe type that different libraries could use.
Re: [Request] A way to extract all instance of X from a range
On Monday, 14 March 2016 at 08:04:18 UTC, deadalnix wrote: Right now, I'm repeating the following pattern many times : range.map!(x => cast(Foo) x).filter!(x => x !is null) Which is kind of annoying. Could we get something in phobos to do this ? BTW, .NET has an extension method called OfType that does the same thing: https://msdn.microsoft.com/library/bb360913(v=vs.100).aspx which is different from Cast, which throws an exception if an element can't be converted: https://msdn.microsoft.com/library/bb341406(v=vs.100).aspx How do you want to handle this? With two separate methods as in .NET, or with a template parameter? Maybe something like this: /// Returns a range that lazily iterates over /// the elements in `source` and converts each /// of them to type `T`. /// /// If `throwOnConvError` is false, elements /// that can't be converted will be skipped. auto ofType (T, bool throwOnConvError = true, Range) (Range source) if (isInputRange!Range)
[Request] A way to extract all instance of X from a range
Right now, I'm repeating the following pattern many times : range.map!(x => cast(Foo) x).filter!(x => x !is null) Which is kind of annoying. Could we get something in phobos to do this ?