Skip to content

Instantly share code, notes, and snippets.

View petrkotek's full-sized avatar

Petr Kotek petrkotek

View GitHub Profile
@petrkotek
petrkotek / ranges_test.go
Created December 30, 2016 15:14
Combining (Aggregating/Adding/Subtracting) ranges in Golang - test coverage
package ranges_test
// tests for https://gist.github.com/petrkotek/317dbcf6a73387682bc8d899b09c36df
import (
"bitbucket.org/kayakrent/booking-api/lib/ranges"
"math"
"reflect"
"testing"
)
@petrkotek
petrkotek / ranges.go
Last active December 30, 2016 16:00
Combining (Aggregating/Adding/Subtracting) ranges in Golang
package ranges
import (
"math"
"sort"
)
type Range struct {
From int64
To int64
@petrkotek
petrkotek / recursive-generator-php70.php
Created September 19, 2016 08:38
Demonstrates how to implement recursive generators in PHP 7.0+
<?php
function walkTree($tree, $depth = 0) {
foreach ($tree as $key => $value) {
yield ['depth' => $depth, 'name' => $key];
if (is_array($value)) {
yield from walkTree($value, $depth + 1);
}
}
}
$tree = [
@petrkotek
petrkotek / recursive-generator-php55.php
Last active September 19, 2016 08:44
Demonstrates how to implement recursive generators in PHP 5.5+
<?php
function walkTree($tree, $depth = 0) {
foreach ($tree as $key => $value) {
yield ['depth' => $depth, 'name' => $key];
if (is_array($value)) {
foreach (walkTree($value, $depth + 1) as $yeilded) {
yield $yeilded;
}
}
}
/**
* Sets a default value for a given input.
*
* @param mixed input
* @param string defaultValue
* @return string
*/
module.filter('default', function() {
return function(input, defaultValue) {
return input ? input : defaultValue;
<?php
/**
* Replacement of str_split() function with multi-byte encoding support.
*
* @param string $string The input string.
* @param int $string_length [optional] Maximum length of the chunk.
* @param string $charset Character encoding.
*
* @return array