Ejemplo n.º 1
0
# map() is much like array_map in php or map in ruby, perl or haskell. it requires a function as an argument
# and is available to any Enumerable object and returns an Array
# each_with_index() accepts a 2-place function with the index and value of each element within the Enumerable object

$a->map(function($x) { return $x / 5; })->each_with_index(function($i, $x) { echo $i.' => '.$x."\n"; });

echo "getting methods\n";

# the method 'methods' is availble to any object and returns an Array of all methods the objecth as available to it.

echo "sorting/joining methods : ".$a->methods()->sort()->join(' ')."\n";

# any Enumerable object can be sorted so long as the the objects contained within the Enumerable object are 'Comparable'

echo "sorting some various integers : ".A(1,4,6,4,2,23,4,4,11,111)->map(function($x) { return I::N($x); })->sort()->join(' ')."\n";

echo "assigning a string\n";

# creating a String (S) is much like creating an Array
# the String class is actually named S

$s = new S("I'm with stupid");
$s = S("Magical");

echo "the string is : ".$s."\n";

echo "interating over a string\n";

# map() on a string works but returns an Array
# reverse_each() is a superfluous iterator method like each() but in reverse