Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 06, 2016 Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
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`? |
October 07, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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.
|
October 06, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | 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.
|
October 06, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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:
>> 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.
Sounds like what output ranges are here for.
|
October 06, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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);
|
October 06, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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.
|
October 06, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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
|
October 06, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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: > > 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 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 Davis |
October 06, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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!Array
You mean semantics like this?
Container collect(Container, Range) (Range r)
if(isOutputRange!Container)
{
Container container;
r.copy(container);
return container;
}
|
October 06, 2016 Re: Rust-like collect in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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:
>> 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
>
> You mean semantics like this?
>
> Container collect(Container, Range) (Range r)
> if(isOutputRange!Container)
> {
> Container container;
> r.copy(container);
> return container;
> }
Yes, along with inference of element type of the container.
|
Copyright © 1999-2021 by the D Language Foundation