PHP code example of nextbigsoundinc / dash
1. Go to this page and download the library: Download nextbigsoundinc/dash library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nextbigsoundinc / dash example snippets
$avgMaleAge = Dash\chain([
['name' => 'John', 'age' => 12, 'gender' => 'male'],
['name' => 'Jane', 'age' => 34, 'gender' => 'female'],
['name' => 'Pete', 'age' => 23, 'gender' => 'male'],
['name' => 'Mark', 'age' => 11, 'gender' => 'male'],
['name' => 'Mary', 'age' => 42, 'gender' => 'female'],
])
->filter(['gender', 'male'])
->map('age')
->average()
->value();
echo "Average male age is $avgMaleAge.";
$people = [
['name' => 'John', 'age' => 12, 'gender' => 'male'],
['name' => 'Jane', 'age' => 34, 'gender' => 'female'],
['name' => 'Pete', 'age' => 23, 'gender' => 'male'],
['name' => 'Mark', 'age' => 11, 'gender' => 'male'],
['name' => 'Mary', 'age' => 42, 'gender' => 'female'],
];
$males = array_filter($people, function ($person) {
return $person['gender'] === 'male';
});
$avgMaleAge = array_sum(array_column($males, 'age')) / count($males);
$avgMaleAge = Dash\chain($people)
->filter(['gender', 'male'])
->map('age')
->average()
->value();
Dash\map([1, 2, 3], function ($n) { return $n * 2; }); // === [2, 4, 6]
use Dash\Dash;
Dash::map([1, 2, 3], function ($n) { return $n * 2; }); // === [2, 4, 6]
use Dash\_;
_::map([1, 2, 3], function ($n) { return $n * 2; }); // === [2, 4, 6]
$result = Dash\chain([1, 2, 3, 4, 5])
->filter('Dash\isOdd')
->map(function ($n) { return $n * 2; })
->value();
// $result === [2, 6, 10]
$result = Dash\chain(['a' => 1, 'b' => 2, 'c' => 3])
->filter('Dash\isOdd')
->mapValues(function ($n) { return $n * 2; })
->objectValue();
// $result === (object) ['a' => 2, 'c' => 6]
// In your application bootstrap:
Dash::addGlobalAlias('__');
// Elsewhere:
$result = __([1, 2, 3, 4, 5])
->filter('Dash\isOdd')
->map(function ($n) { return $n * 2; })
->value();
$chain = Dash\chain(range(1, 5))
->reverse()
->each(function ($n) {
echo "T-minus $n...\n";
sleep(1);
});
// Nothing echoed yet
$chain->value();
// or
$chain->run();
// Echoes each of the following lines 1 second apart:
// T-minus 5...
// T-minus 4...
// T-minus 3...
// T-minus 2...
// T-minus 1...
Dash\chain([1, 2, 3, 4])
->filter('Dash\isEven')
->map(function ($value) {
return $value * 2;
})
->value();
// === [4, 8]
Dash\chain((object) ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4])
->filter('Dash\isOdd')
->keys()
->join(', ')
->value();
// === 'a, c'
Dash\chain(new ArrayObject(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]))
->pick(['b', 'c'])
->values()
->sum()
->value();
// === 5
$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
$filenames = Dash\chain($iterator)
->reject(function ($fileinfo) {
return $fileinfo->isDir();
})
->map(function ($fileinfo) {
return pathinfo($fileinfo)['filename'];
})
->value();
function listThree($a, $b, $c) {
return "$a, $b, and $c";
}
$listThree = Dash\curry('listThree');
$listTwo = $listThree('first');
$listTwo('second', 'third'); // === 'first, second, and third'
Dash\chain([
'a' => 3,
'b' => '3',
'c' => 3,
'd' => 3.0
])
->filter(Dash\Curry\identical(3))
->value();
// === ['a' => 3, 'c' => 3]
$greet = function ($greeting, $name) {
return "$greeting, $name!";
};
$sayHello = Dash\partial($greet, 'Hello');
$sayHowdy = Dash\partial($greet, 'Howdy');
$sayHello('Mark'); // === 'Hello, Mark!'
$sayHowdy('Jane'); // === 'Howdy, Jane!'
$chain = Dash\chain()
->filter('Dash\isOdd')
->map(function ($n) { return $n * 2; });
$chain->with([1, 2, 3])->value(); // === [2, 6]
$chain->with([4, 5, 6, 7])->value(); // === [10, 14]
// …continued from above
$clone = clone $chain;
$clone->map(function ($n) { $n + 1; })
$clone->value(); // === [11, 15]
// The original chain is untouched
$chain->value(); // === [10, 14]
Dash::setCustom('triple', function ($n) { return $n * 3; });
// Standalone
Dash::triple(4); // === 12
// Chained
Dash\chain([1, 2, 3])
->sum()
->triple()
->value(); // === 18
// As an argument
Dash\chain([1, 2, 3])
->map('Dash\Dash::triple')
->value(); // === [3, 6, 9]
// As an argument using the Dash::getCustom() method
Dash\chain([1, 2, 3])
->map(Dash::getCustom('triple'))
->value(); // === [3, 6, 9]
// Using the Dash\custom() operation
Dash\chain([1, 2, 3])
->map(Dash\custom('triple'))
->value(); // === [3, 6, 9]
Dash::unsetCustom('triple');
Dash::setCustom('divide', function($numerator, $denominator) { return $numerator / $denominator; });
Dash\chain(6)->divide(2)->value(); // === 2
$result = Dash\chain(['one' => 1, 'two' => 2, 'three' => 3])
->filter('Dash\isOdd')
->thru(function($input) {
return array_change_key_case($input, CASE_UPPER);
})
->keys()
->value();
// $result === ['ONE', 'THREE']
Dash::setCustom('keyCase', function ($input, $case) {
return array_change_key_case($input, $case);
});
$result = Dash\chain(['one' => 1, 'two' => 2, 'three' => 3])
->filter('Dash\isOdd')
->keyCase(CASE_UPPER)
->keys()
->value();
// $result === ['ONE', 'THREE']