This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function fold($array, $callback, $initial=null) | |
{ | |
$acc = $initial; | |
foreach($array as $a) | |
$acc = $callback($acc, $a); | |
return $acc; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T, R> Collection<T>.fold( | |
initial: R, | |
combine: (acc: R, nextElement: T) -> R | |
): R { | |
var accumulator: R = initial | |
for (element: T in this) { | |
accumulator = combine(accumulator, element) | |
} | |
return accumulator | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
//ARRAYS | |
val array = arrayOf(1,2,3,4,5,6) | |
// array size is just a property not another function | |
val arrayLength = array.size | |
// methods are chained and lambda function can be used | |
val multipliedArrayReversed = array.map { it * 2 }.reversed() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ARRAYS | |
$array = [1,2,3,4,5,6]; | |
// we need to use count function to get array length | |
$arrayLength = count($array); | |
// we need to combine 2 functions to get reversed array with elements multiplied by 2 | |
$multipliedArrayReversed = array_reverse(array_map(fn(int $value): int => $value * 2, $array)); |