digitalmars.D - [Request] A way to extract all instance of X from a range
- deadalnix (4/4) Mar 14 2016 Right now, I'm repeating the following pattern many times :
-
ZombineDev
(20/24)
Mar 14 2016
BTW, .NET has an extension method called OfType
that does the - thedeemon (15/17) Mar 14 2016 This reminds me of a function in OCaml extended stdlib that I've
- Nick Treleaven (11/16) Mar 21 2016 There is a pull for Option:
- Timothee Cour via Digitalmars-d (8/28) Mar 21 2016 see my proposal [+implementation] for emit
- Matthias Bentrup (8/46) Mar 22 2016 Why don't you go for the well-established monadic bind function ?
- Nick Treleaven (2/5) Mar 23 2016 http://dlang.org/phobos/std_range.html#.chain
- Marc =?UTF-8?B?U2Now7x0eg==?= (3/9) Mar 23 2016 Or joiner(), if you have a range of ranges:
- Jakob Ovrum (5/15) Apr 01 2016 Using the interface proposed in the PR:
- Stefan Koch (3/7) Mar 14 2016 you could use an alias.
- Stefan Koch (4/8) Mar 14 2016 There you go ;)
- Stefan Koch (3/13) Mar 14 2016 Ooops ;) didn't work
- Meta (4/14) Mar 14 2016 I believe this should work with the latest DMD:
- Stefan Koch (12/15) Mar 15 2016 No it does not.
- Meta (3/20) Mar 15 2016 What version of DMD are you using? It compiles and a small test
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 ?
Mar 14 2016
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<T> that does the same thing: https://msdn.microsoft.com/library/bb360913(v=vs.100).aspx which is different from Cast<T>, 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)
Mar 14 2016
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.
Mar 14 2016
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]]));
Mar 21 2016
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:see my proposal [+implementation] for emit http://forum.dlang.org/post/mailman.538.1458560190.26339.digitalmars-d 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);});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]]));
Mar 21 2016
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: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).On 14/03/2016 11:32, thedeemon wrote:see my proposal [+implementation] for emit http://forum.dlang.org/post/mailman.538.1458560190.26339.digitalmars-d 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);});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]]));
Mar 22 2016
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).
Mar 23 2016
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:Or joiner(), if you have a range of ranges: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).
Mar 23 2016
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])));
Apr 01 2016
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);
Mar 14 2016
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);
Mar 14 2016
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:Ooops ;) didn't work then it'll has to be done with a wrapper struct ...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);
Mar 14 2016
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:I believe this should work with the latest DMD: alias castRange(T) = t => t.map!(x => cast(T) x).filter!(x => x !is null);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);
Mar 14 2016
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.
Mar 15 2016
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:What version of DMD are you using? It compiles and a small test works for me on 2.070.2.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.
Mar 15 2016