digitalmars.D.learn - Rust-like collect in D
- =?UTF-8?B?Tm9yZGzDtnc=?= (10/10) Oct 06 2016 Is there a concept in D similar to Rust's `collect`:
- rikki cattermole (3/12) Oct 06 2016 So an input range to array?
- =?UTF-8?B?Tm9yZGzDtnc=?= (4/6) Oct 06 2016 No, we want a generic alternative that fills any kind of
- cym13 (2/8) Oct 06 2016 Sounds like what output ranges are here for.
- Dicebot (4/14) Oct 06 2016 If an entity (i.e. container) implements OutputRange API, you can
- =?UTF-8?B?Tm9yZGzDtnc=?= (2/5) Oct 06 2016 Thanks, that's what I was looking for.
- =?UTF-8?B?Tm9yZGzDtnc=?= (8/11) Oct 06 2016 Ahh, not quite what I wanted... I want to mimic the functional
- Jonathan M Davis via Digitalmars-d-learn (6/18) Oct 06 2016 That makes it sound like you just need the container to have a construct...
- Dicebot (9/13) Oct 06 2016 You mean semantics like this?
- =?UTF-8?B?Tm9yZGzDtnc=?= (2/17) Oct 06 2016 Yes, along with inference of element type of the container.
- ag0aep6g (11/20) Oct 06 2016 https://dlang.org/phobos/std_container_util.html#.make
- ag0aep6g (3/4) Oct 06 2016 More specifically, the second overload is where it's at:
- Andrei Alexandrescu (3/5) Oct 12 2016 That's "make" in std.container. Is that what you're looking for? As an
- Meta (4/11) Oct 12 2016 I've always thought of it more as "collect the elements of the
- Russel Winder via Digitalmars-d-learn (19/33) Oct 12 2016 Java and Rust have gone the same route. collect is a terminal operation
- =?UTF-8?B?Tm9yZGzDtnc=?= (7/10) Oct 13 2016 Is D somehow better here?
- =?UTF-8?B?Tm9yZGzDtnc=?= (2/7) Oct 13 2016 https://github.com/dlang/phobos/pull/4860
- Russel Winder via Digitalmars-d-learn (16/25) Oct 12 2016 I's say collect has an interesting history. Smalltalk used collect for
Is there a concept in D similar to Rust's `collect`: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect If not, I'm eager to implement it to support D-style containers. What would the desired interface look like? Perhaps: 0.iota(n).collect!Array Or can/should we just overload `std.conv.to` as 0.iota(n).to!Array eventhough the element type is not explicit in the expression `to.!Array`?
Oct 06 2016
On 07/10/2016 3:32 AM, Nordlöw wrote:Is there a concept in D similar to Rust's `collect`: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect If not, I'm eager to implement it to support D-style containers. What would the desired interface look like? Perhaps: 0.iota(n).collect!Array Or can/should we just overload `std.conv.to` as 0.iota(n).to!Array eventhough the element type is not explicit in the expression `to.!Array`?So an input range to array? Sure std.array : array.
Oct 06 2016
On Thursday, 6 October 2016 at 14:33:59 UTC, rikki cattermole wrote:So an input range to array? Sure std.array : array.No, we want a generic alternative that fills any kind of container, typically non-GC allocated.
Oct 06 2016
On Thursday, 6 October 2016 at 14:58:34 UTC, Nordlöw wrote:On Thursday, 6 October 2016 at 14:33:59 UTC, rikki cattermole wrote:Sounds like what output ranges are here for.So an input range to array? Sure std.array : array.No, we want a generic alternative that fills any kind of container, typically non-GC allocated.
Oct 06 2016
On Thursday, 6 October 2016 at 14:32:44 UTC, Nordlöw wrote:Is there a concept in D similar to Rust's `collect`: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect If not, I'm eager to implement it to support D-style containers. What would the desired interface look like? Perhaps: 0.iota(n).collect!Array Or can/should we just overload `std.conv.to` as 0.iota(n).to!Array eventhough the element type is not explicit in the expression `to.!Array`?If an entity (i.e. container) implements OutputRange API, you can already do it: 0.iota(n).copy(container);
Oct 06 2016
On Thursday, 6 October 2016 at 16:14:33 UTC, Dicebot wrote:If an entity (i.e. container) implements OutputRange API, you can already do it: 0.iota(n).copy(container);Thanks, that's what I was looking for.
Oct 06 2016
On Thursday, 6 October 2016 at 16:14:33 UTC, Dicebot wrote:If an entity (i.e. container) implements OutputRange API, you can already do it: 0.iota(n).copy(container);Ahh, not quite what I wanted... I want to mimic the functional style Rust provides, where the `container` is constructed inline and does not have to be declared separately. Is there a way to do this, or do we need something similar to `collect` in Phobos? Something like import std.container.array : Array; 0.iota(n).collect!Array
Oct 06 2016
On Thursday, October 06, 2016 16:56:26 Nordlöw via Digitalmars-d-learn wrote:On Thursday, 6 October 2016 at 16:14:33 UTC, Dicebot wrote:That makes it sound like you just need the container to have a constructor that takes a range - either that or a helper function which constructs the container from a range (e.g. std.container.rbTree.redBlackTree is a helper function for constructing a RedBlackTree from a range). - Jonathan M DavisIf an entity (i.e. container) implements OutputRange API, you can already do it: 0.iota(n).copy(container);Ahh, not quite what I wanted... I want to mimic the functional style Rust provides, where the `container` is constructed inline and does not have to be declared separately. Is there a way to do this, or do we need something similar to `collect` in Phobos? Something like import std.container.array : Array; 0.iota(n).collect!Array
Oct 06 2016
On Thursday, 6 October 2016 at 16:56:26 UTC, Nordlöw wrote:Is there a way to do this, or do we need something similar to `collect` in Phobos? Something like import std.container.array : Array; 0.iota(n).collect!ArrayYou mean semantics like this? Container collect(Container, Range) (Range r) if(isOutputRange!Container) { Container container; r.copy(container); return container; }
Oct 06 2016
On Thursday, 6 October 2016 at 17:22:10 UTC, Dicebot wrote:On Thursday, 6 October 2016 at 16:56:26 UTC, Nordlöw wrote:Yes, along with inference of element type of the container.Is there a way to do this, or do we need something similar to `collect` in Phobos? Something like import std.container.array : Array; 0.iota(n).collect!ArrayYou mean semantics like this? Container collect(Container, Range) (Range r) if(isOutputRange!Container) { Container container; r.copy(container); return container; }
Oct 06 2016
On 10/06/2016 04:32 PM, Nordlöw wrote:Is there a concept in D similar to Rust's `collect`: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect If not, I'm eager to implement it to support D-style containers. What would the desired interface look like? Perhaps: 0.iota(n).collect!Array Or can/should we just overload `std.conv.to` as 0.iota(n).to!Array eventhough the element type is not explicit in the expression `to.!Array`?https://dlang.org/phobos/std_container_util.html#.make ---- import std.container.array: Array; import std.container.util: make; import std.range: iota; void main() { auto arr = 0.iota(99).make!Array; } ----
Oct 06 2016
On 10/06/2016 07:44 PM, ag0aep6g wrote:https://dlang.org/phobos/std_container_util.html#.makeMore specifically, the second overload is where it's at: https://dlang.org/phobos/std_container_util.html#.make.2
Oct 06 2016
On 10/06/2016 10:32 AM, Nordlöw wrote:Is there a concept in D similar to Rust's `collect`: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collectThat's "make" in std.container. Is that what you're looking for? As an aside, "collect" is an awful abbreviation of "collection". -- Andrei
Oct 12 2016
On Thursday, 13 October 2016 at 01:15:22 UTC, Andrei Alexandrescu wrote:On 10/06/2016 10:32 AM, Nordlöw wrote:I've always thought of it more as "collect the elements of the iterator into a container".Is there a concept in D similar to Rust's `collect`: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collectThat's "make" in std.container. Is that what you're looking for? As an aside, "collect" is an awful abbreviation of "collection". -- Andrei
Oct 12 2016
On Thu, 2016-10-13 at 02:33 +0000, Meta via Digitalmars-d-learn wrote:On Thursday, 13 October 2016 at 01:15:22 UTC, Andrei Alexandrescu=C2=A0 wrote:Java and Rust have gone the same route. collect is a terminal operation creating a data structure given a potentially infinite, lazily evaluated, stream. Rust uses iter for creating a stream from a data structure, Java uses stream or parallelstream. Java's parallelstream seems to be missing from Rust, but there is Rayon to provide a version. D's std.parallelism does something of this, but it needs more work to provide the tree-structured as well as scatter=E2=80=93gather internal parallelism models. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winderOn 10/06/2016 10:32 AM, Nordl=C3=B6w wrote:=20 I've always thought of it more as "collect the elements of the=C2=A0 iterator into a container".Is there a concept in D similar to Rust's `collect`: =20 https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.col lect=20 That's "make" in std.container. Is that what you're looking=C2=A0 for? As an aside, "collect" is an awful abbreviation of=C2=A0 "collection". -- Andrei
Oct 12 2016
On Thursday, 13 October 2016 at 05:07:26 UTC, Russel Winder wrote:Java and Rust have gone the same route. collect is a terminal operation creating a data structure given a potentially infinite, lazily evaluated, stream.Is D somehow better here? For instance, we can use `if (!isInfinite!Range)` to forbid collecting values from an infinite `Range`. Is this checking done? That is `0.iota.make!Array` should not be allowed.
Oct 13 2016
On Thursday, 13 October 2016 at 10:00:56 UTC, Nordlöw wrote:For instance, we can use `if (!isInfinite!Range)` to forbid collecting values from an infinite `Range`. Is this checking done? That is `0.iota.make!Array` should not be allowed.https://github.com/dlang/phobos/pull/4860
Oct 13 2016
On Wed, 2016-10-12 at 21:15 -0400, Andrei Alexandrescu via Digitalmars- d-learn wrote:On 10/06/2016 10:32 AM, Nordl=C3=B6w wrote:I's say collect has an interesting history. Smalltalk used collect for what other called map. Various languages, including Groovy, continued this tradition. Now Rust and Java are using collect as a verb-like form, just as D uses make. collection would be wrong here. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winderIs there a concept in D similar to Rust's `collect`: =20 https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.colle ct=20 That's "make" in std.container. Is that what you're looking for? As an=C2=A0 aside, "collect" is an awful abbreviation of "collection". -- Andrei
Oct 12 2016