[Request] A way to extract all instance of X from a range

2016-03-14 Thread deadalnix via Digitalmars-d
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 ?

Re: [Request] A way to extract all instance of X from a range

2016-03-14 Thread ZombineDev via Digitalmars-d
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 OfTyp

Re: [Request] A way to extract all instance of X from a range

2016-03-14 Thread thedeemon via Digitalmars-d
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 : (

Re: [Request] A way to extract all instance of X from a range

2016-03-14 Thread Stefan Koch via Digitalmars-d
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 = fi

Re: [Request] A way to extract all instance of X from a range

2016-03-14 Thread Stefan Koch via Digitalmars-d
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

Re: [Request] A way to extract all instance of X from a range

2016-03-14 Thread Stefan Koch via Digitalmars-d
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

Re: [Request] A way to extract all instance of X from a range

2016-03-14 Thread Meta via Digitalmars-d
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

Re: [Request] A way to extract all instance of X from a range

2016-03-15 Thread Stefan Koch via Digitalmars-d
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.

Re: [Request] A way to extract all instance of X from a range

2016-03-15 Thread Meta via Digitalmars-d
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

Re: [Request] A way to extract all instance of X from a range

2016-03-21 Thread Nick Treleaven via Digitalmars-d
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 O

Re: [Request] A way to extract all instance of X from a range

2016-03-21 Thread Timothee Cour via Digitalmars-d
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, whe

Re: [Request] A way to extract all instance of X from a range

2016-03-22 Thread Matthias Bentrup via Digitalmars-d
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

Re: [Request] A way to extract all instance of X from a range

2016-03-23 Thread Nick Treleaven via Digitalmars-d
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

2016-03-23 Thread Marc Schütz via Digitalmars-d
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).

Re: [Request] A way to extract all instance of X from a range

2016-04-01 Thread Jakob Ovrum via Digitalmars-d
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 a