| Thread overview | |||||
|---|---|---|---|---|---|
|
March 05, 2021 lockstep works with .each, but fails with .map | ||||
|---|---|---|---|---|
| ||||
Hi
What am I doing wrong here?
import std.stdio, std.range, std.algorithm, std.uni, std.utf, std.conv, std.typecons, std.array;
auto SE(A, B)(in A a, in B b){
return (a-b)^^2;
}
void main(){
auto a = [1, 2, 3], b = [1, 1, 1];
lockstep(a, b, StoppingPolicy.requireSameLength).each!((a, b){ writeln(SE(a, b)); });
lockstep(a, b, StoppingPolicy.requireSameLength).map !((a, b){ return SE(a, b) ; }).each!writeln; <- error here
}
The error:
map(Range)(Range r)
with Range = Lockstep!(int[], int[])
must satisfy the following constraint:
isInputRange!(Unqual!Range)
Why it works with each (or foreach), but not with map? o.O
I just wanted to make a Sum of squared errors function.
Thanks in advance!
| ||||
March 05, 2021 Re: lockstep works with .each, but fails with .map | ||||
|---|---|---|---|---|
| ||||
Posted in reply to realhet | On 2021-03-05 19:49, realhet wrote: > Why it works with each (or foreach), but not with map? o.O `lockstep` is specifically designed to work with `foreach`. I think `each` has a special case to work with `lockstep`. If you want to use other range functions, you should use `zip` instead of `lockstep`. -- /Jacob Carlborg | |||
March 05, 2021 Re: lockstep works with .each, but fails with .map | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Friday, 5 March 2021 at 19:26:38 UTC, Jacob Carlborg wrote: > On 2021-03-05 19:49, realhet wrote: > >> Why it works with each (or foreach), but not with map? o.O > > `lockstep` is specifically designed to work with `foreach`. I think `each` has a special case to work with `lockstep`. If you want to use other range functions, you should use `zip` instead of `lockstep`. It works now: zip(StoppingPolicy.requireSameLength, a, b).map!(a => SE(a[])).sum / float(a.length); I had a misconception (lazyness of learning) that zip is making a simple array, not a tuple array like I guessed lockstep does. Also in zip() the StoppingPolicy is the first parameter and in lockstep() it's the last. Thank you very much! | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply