Wikia

Perl6 Wiki

Gather

Talk0
77pages on
this wiki

Gather is syntactic sugar for creating an Array, that is by gathering up a bunch of things to take into the Array.

Description Edit

Gather is a statement that encloses take statements. The take control structure can never be used outside of the gather structure in Perl6. Gather returns an Array such that whenever take is ran it pushes to the array that gather instantiated in the beggining.

General Structure Edit

gather { take $value; } 

Alternative Notation Edit

(sub {
	my @arr;

	@arr.push($value);

	return @arr;
})();

Examples Edit

Get lower case words Edit

sub getLowerCaseWords(Str $phrase) {

	gather {
		take $_ if $_ eq .lc for $phrase.words;
	}

}

getLowerCaseWords("This phrase is a test").join(", ").say;
Advertisement | Your ad here

Around Wikia's network

Random Wiki