Skip to content

Instantly share code, notes, and snippets.

View marrek13's full-sized avatar

Mariusz Sołtysiak marrek13

View GitHub Profile
@marrek13
marrek13 / fold.php
Created April 22, 2022 16:36
Fold PHP
<?php
function fold($array, $callback, $initial=null)
{
$acc = $initial;
foreach($array as $a)
$acc = $callback($acc, $a);
return $acc;
}
@marrek13
marrek13 / fold.kt
Created April 22, 2022 16:35
Fold Kotlin
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
}
@marrek13
marrek13 / examples.kt
Last active April 22, 2022 06:28
Kotlin examples
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()
@marrek13
marrek13 / examples.php
Last active April 21, 2022 17:50
PHP examples
<?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));