www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - having problem with `std.algorithm.each`

reply ref2401 <refactor24 gmail.com> writes:
It seems like `std.algorithm.each` is not executed in the example 
below.
Could anyone tell why?
Thank you.


import std.algorithm;

void main(string[] args) {
	int[] arr = [1, 2, 3, 4, 5];

	arr.each!((ref e) => {
		writeln(e); // does not print
		++e;
	})();

	writeln(arr); // prints [1, 2, 3, 4, 5]
}
Nov 30 2015
parent reply ref2401 <refactor24 gmail.com> writes:
DMD 2.069.1
OS Win8.1 Enterprise
Nov 30 2015
parent reply visitor <visitor gmail.com> writes:
On Monday, 30 November 2015 at 09:56:08 UTC, ref2401 wrote:
 DMD 2.069.1
 OS Win8.1 Enterprise
in a multiline statement, i believe you must use : arr.each!((ref e) { writeln(e); ++e; }) "=>" is for oneliner though i don"t understand why it fails silently ??
Nov 30 2015
parent reply anonymous <anonymous example.com> writes:
On 30.11.2015 11:50, visitor wrote:
 though i don"t understand why it fails silently ??
ref2491's original code is valid, but doesn't have the intended meaning. `e => {foo(e);}` is the same as `(e) {return () {foo(e);};}`, i.e. a (unary) function that returns a (nullary) delegate. Calling it does not run foo. In contrast, calling this runs foo: `e => foo(e)`.
Nov 30 2015
next sibling parent visitor <visitor gmail.com> writes:
On Monday, 30 November 2015 at 12:03:08 UTC, anonymous wrote:
 On 30.11.2015 11:50, visitor wrote:
 though i don"t understand why it fails silently ??
ref2491's original code is valid, but doesn't have the intended meaning. `e => {foo(e);}` is the same as `(e) {return () {foo(e);};}`, i.e. a (unary) function that returns a (nullary) delegate. Calling it does not run foo. In contrast, calling this runs foo: `e => foo(e)`.
 `e => {foo(e);}` is the same as `(e) {return () {foo(e);};}`
Ok, Thanks ! :-)
Nov 30 2015
prev sibling parent ref2401 <refactor24 gmail.com> writes:
On Monday, 30 November 2015 at 12:03:08 UTC, anonymous wrote:
 On 30.11.2015 11:50, visitor wrote:
 though i don"t understand why it fails silently ??
ref2491's original code is valid, but doesn't have the intended meaning. `e => {foo(e);}` is the same as `(e) {return () {foo(e);};}`, i.e. a (unary) function that returns a (nullary) delegate. Calling it does not run foo. In contrast, calling this runs foo: `e => foo(e)`.
Got it. Thank you)
Nov 30 2015