June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | I have a little bit of a follow up. After making the recommended changes, the function seems to work with both static and dynamic arrays. I then noticed that all of the examples for functions that pass arrays in http://dlang.org/function.html use the dynamic array notation like my function above. Does this matter? |
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 Attachments: | On Mon, 15 Jun 2015 17:07:55 +0000, jmh530 wrote:
> I have a little bit of a follow up.
>
> After making the recommended changes, the function seems to work with both static and dynamic arrays. I then noticed that all of the examples for functions that pass arrays in http://dlang.org/function.html use the dynamic array notation like my function above. Does this matter?
it doesn't, but i'd use `[]` anyway for code readability. static arrays can be converted to slices by the compiler when it needs to, but by using explicit `[]` it's easy to see if function expects slice or "real static array" right in the call site, without looking at function signature.
|
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Monday, 15 June 2015 at 15:10:24 UTC, jmh530 wrote: > I wrote a simple function to apply map to a float dynamic array > > auto exp(float[] x) { > auto y = x.map!(a => exp(a)); > return y; > } > > However, the type of the result is MapResult!(__lambda2, float[]). It seems like some of the things that I might do to a float[], I can't do to this type, like adding them together. So I tried to adjust this by adding in a cast to float[], as in > > float[] exp(float[] x) { > auto y = x.map!(a => exp(a)); > cast(float[]) y; > return y; > } > > But I get an error that I can't convert MapResult!(__lambda2, float[]) to float[]. > > So I suppose I have two questions: 1) am I screwing up the cast, or is there no way to convert the MapResult to float[], 2) should I just not bother with map (I wrote an alternate, longer, version that doesn't use map but returns float[] properly). In addition to the other answers you can use std.algorithm.iteration.each(): --- float[] _exp(float[] x) { auto result = x.dup; result.each!(a => exp(a)); return result; } --- |
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to Baz | On Monday, 15 June 2015 at 19:04:32 UTC, Baz wrote:
> In addition to the other answers you can use std.algorithm.iteration.each():
>
> ---
> float[] _exp(float[] x) {
> auto result = x.dup;
> result.each!(a => exp(a));
> return result;
> }
> ---
Am I right that the difference is that map is lazy and each is greedy? Does that have any significant performance effects?
|
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Monday, 15 June 2015 at 19:22:31 UTC, jmh530 wrote:
> On Monday, 15 June 2015 at 19:04:32 UTC, Baz wrote:
>> In addition to the other answers you can use std.algorithm.iteration.each():
>>
>> ---
>> float[] _exp(float[] x) {
>> auto result = x.dup;
>> result.each!(a => exp(a));
>> return result;
>> }
>> ---
>
> Am I right that the difference is that map is lazy and each is greedy? Does that have any significant performance effects?
i think that the OP wants greedy. That's why he had to fight with map results.
|
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to Baz | On Monday, 15 June 2015 at 19:30:08 UTC, Baz wrote:
> On Monday, 15 June 2015 at 19:22:31 UTC, jmh530 wrote:
>> On Monday, 15 June 2015 at 19:04:32 UTC, Baz wrote:
>>> In addition to the other answers you can use std.algorithm.iteration.each():
>>>
>>> ---
>>> float[] _exp(float[] x) {
>>> auto result = x.dup;
>>> result.each!(a => exp(a));
>>> return result;
>>> }
>>> ---
>>
>> Am I right that the difference is that map is lazy and each is greedy? Does that have any significant performance effects?
>
> i think that the OP wants greedy. That's why he had to fight with map results.
Ah sorry it's you the OP. just get it. So you wanted greedy, didn't you ?
|
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to Baz | On Monday, 15 June 2015 at 19:32:12 UTC, Baz wrote:
> On Monday, 15 June 2015 at 19:30:08 UTC, Baz wrote:
>> On Monday, 15 June 2015 at 19:22:31 UTC, jmh530 wrote:
>>> On Monday, 15 June 2015 at 19:04:32 UTC, Baz wrote:
>>>> In addition to the other answers you can use std.algorithm.iteration.each():
>>>>
>>>> ---
>>>> float[] _exp(float[] x) {
>>>> auto result = x.dup;
>>>> result.each!(a => exp(a));
>>>> return result;
>>>> }
>>>> ---
>>>
>>> Am I right that the difference is that map is lazy and each is greedy? Does that have any significant performance effects?
>>
>> i think that the OP wants greedy. That's why he had to fight with map results.
>
> Ah sorry it's you the OP. just get it. So you wanted greedy, didn't you ?
I suppose I would want whichever has the best performance. Without testing, I'm not sure which one would be better. Thoughts?
I had been fighting with the map results because I didn't realize there was an easy way to get just the array.
|
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | > I suppose I would want whichever has the best performance. Without testing, I'm not sure which one would be better. Thoughts?
>
> I had been fighting with the map results because I didn't realize there was an easy way to get just the array.
I'm actually not having much luck with your original function (and I tried some variations on it). It just kept outputting the original array without applying the function. I tried it in main also (without being in a function) without much luck either.
|
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Monday, 15 June 2015 at 20:10:30 UTC, jmh530 wrote:
>> I suppose I would want whichever has the best performance. Without testing, I'm not sure which one would be better. Thoughts?
>>
>> I had been fighting with the map results because I didn't realize there was an easy way to get just the array.
>
> I'm actually not having much luck with your original function (and I tried some variations on it). It just kept outputting the original array without applying the function. I tried it in main also (without being in a function) without much luck either.
Right, my bad. This one whould work:
---
float[] test(float[] x) {
auto result = x.dup;
result.each!((ref a) => (a = exp(a)));
return result;
}
---
|
June 15, 2015 Re: Casting MapResult | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On 06/15/2015 12:44 PM, jmh530 wrote: > On Monday, 15 June 2015 at 19:32:12 UTC, Baz wrote: >> Ah sorry it's you the OP. just get it. So you wanted greedy, didn't you ? > > I suppose I would want whichever has the best performance. Without > testing, I'm not sure which one would be better. Thoughts? There are different levels of lazyness regarding performance. :) 1) map and most algorithms are fully lazy. They don't do anything until elements are actually used. 2) Some algorithms that keep their state in struct objects do some work in their constructors to prepare the first element for use. 3) 'each' is fully eager. It walks through all elements of the range and does something with each of those. 4) 'array' is fully eager but it also creates an array to store all the elements in. There are also semi-eager algorithms like 'asyncBuf' and 'map' in std.parallelism that consume the input range in waves. Ali |
Copyright © 1999-2021 by the D Language Foundation