Thread overview | |||||
---|---|---|---|---|---|
|
March 10, 2019 Applying a function each respective 2 elements of an array | ||||
---|---|---|---|---|
| ||||
I have an array like(In my real problem my data structs is more complex ) : auto a = [2,3,4,5,6,7]; I want to apply a operation in a fashion like : [ 2 , 3 ] --> apply foo and get return result -1 [ 3 , 4 ] ---> -1 [ 4 , 5 ] ---> -1 and so on... operation might be : int foo ( int a, int b) { return a - b; } So my expected result is [-1,-1,-1,-1,-1] I tried fold but fold add up the result of the last operation. Range utilities like "chunks" does not help because "chunks" transforms the array into a range like [2,3],[4,5],[6,7]. Can I avoid for loops and solve my problem with std algorithms or ranges ? |
March 10, 2019 Re: Applying a function each respective 2 elements of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to kerdemdemir | On Sunday, 10 March 2019 at 08:59:59 UTC, kerdemdemir wrote: > Can I avoid for loops and solve my problem with std algorithms or ranges ? Try slide: https://dlang.org/phobos/std_range.html#slide And then use map. |
March 10, 2019 Re: Applying a function each respective 2 elements of an array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis | On Sunday, 10 March 2019 at 09:43:59 UTC, Dennis wrote:
> On Sunday, 10 March 2019 at 08:59:59 UTC, kerdemdemir wrote:
>> Can I avoid for loops and solve my problem with std algorithms or ranges ?
>
> Try slide: https://dlang.org/phobos/std_range.html#slide
> And then use map.
I think that will work that is exactly what I am looking for thanks
Erdem
|
Copyright © 1999-2021 by the D Language Foundation