PHP code example of belt / underscore
1. Go to this page and download the library: Download belt/underscore 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/ */
belt / underscore example snippets
use Belt\_;
_::create($user->getFriends())->map(function ($f) {
return $f->getFriends();
})->select(function ($f) {
return $f->getAge() > 18;
})->pluck('username');
_::create($user->getFriends())->map(function ($f) {
return $f->getFriends();
})->select(function ($f) {
return $f->getAge() > 18;
})->reduce(function ($s, $f) {
return $s + count($f->getFriends());
});
$groups = _::create($user->getFriends())->groupBy(function ($friend) {
$name = $friend->getName();
return $name[0];
});
$groups['A'] = ...; // All friends with the letter 'A' as the first letter in their name
$users = _::create(['alice', 1337, 'bob', 42])->chunk(2);
foreach ($users as $name => $karma) {
// ...
}
_::create([1, 2, 3])->all(function ($n) {
return $n > 0;
}); // true
_::create([1, 2, 3])->any(function ($n) {
return $n > 2;
}); // true
_::create([1, 2, 3, 4])->chunk(2); // [[1, 2], [3, 4]]
_::create([1, 2, 3])->combine(['foo', 'bar', 'baz']); // [1 => 'foo', 2 => 'bar', 3 => 'baz']
_::create([1, 2])->concat([3, 4]); // [1, 2, 3, 4]
_::create([[1, 2], [3, 4]])->dict(); // [1 => 2, 3 => 4]
_::create([1, 2, 3, 4])->chunk(2)->dict(); // [1 => 2, 3 => 4]
_::create([1, 2, 3, 4])->each(function ($n) {
printf("%d\n", $n);
}); // outputs: 1\n2\n3\n4\n
_::create([1, 2, 3, 4])->each(function ($n, $i) {
printf("%d: %d\n", $i, $n);
}); //outputs: 0: 1\n1: 2\n2: 3\n3: 4\n
_::create([1, 2, 3, 4]->each(function ($n, $i, $array) {
// ...
}));
_::create([1, 2, 3, 4])->find(function ($n) {
return $n > 2;
}); // 3
_::create([1, 2, 3, 4])->first(2); // [1, 2]
_::create([1, [2], [3, [4]]])->flatten(); [1, 2, 3, 4]
_::create([1, 2, 3, 4])->flatMap(function ($n) {
return [$n, $n];
}); // [1, 1, 2, 2, 3, 3, 4, 4]
_::create([1, 2, 3, 4])->flatMap(function ($n) {
return [$n, [$n]];
}); // [1, [1], 2, [2], 3, [3], 4, [4]]
_::create([new User('bob', 32), new User('alice', 35)])->flatMap(function ($u) {
return [$n->getName(), $n->getAge()];
})->chunk(2)->dict(); // ['bob' => 32, 'alice' => 35]
_::create([1, 2, 3, 4])->groupBy(function ($n) {
return $n % 2;
}); // [0 => [2, 4], 1 => [1, 3]]
_::create(['foo', 'bar', 'baz'])->groupBy(function ($s) {
return $s[0];
}); // ['f' => ['foo'], 'b' => ['bar', 'baz']]
_::create([1, 2, 3, 4])->has(2); // true
_::create([1, 2, 3, 4])->has(0); // false
_::create([1, 2, 3, 4])->indexOf(2); // 1
_::create([1, 2, 3, 4])->indexOf(0); // null
_::create([1, 2, 3])->inject([], function ($m, $n) {
$m[$n] = $n * $n;
return $m;
}); // [1 => 1, 2 => 4, 3 => 9]
_::create(['foo', 'bar', 'baz'])->inject('', function ($m, $s) {
$m .= $s;
}); // foobarbaz
_::create([1, 2, 3, 4])->join(''); // 1234
_::create([1, 2, 3, 4])->join(','); // 1,2,3,4
_::create([1, 2, 3, 4, 5, 6])->last(2); // [5, 6]
_::create([1, 2, 3, 4])->map(function ($n) {
return $n * $n;
}); // [1, 4, 9, 16]
_::create([1, 2, 3, 4])->map(function ($n) {
return $n % 2 ? $n * $n : null;
}); // [1, 9]
_::create('1', 'two', 'three')->max(function ($s) {
return strlen($s);
}); // 'three'
_::create('1', 'two', 'three')->min(function ($s) {
return strlen($s);
}); // '1'
_::create([1, 2, 3, 4])->none(function ($n) {
return $n < 0;
}); // true
_::create([1, 2, 3, 4])->none(function ($n) {
return $n > 0;
}); // false
_::create(['A', 'B', 'C', 'AA'])->partition(function ($s) {
return $s[0] == 'A';
}); // [['A', 'AA'], ['B', 'C']]
_::create([new User('bob'), new User('alice')])->pluck('username'); // ['bob', 'alice']
_::create([1, 2, 3])->product(); // 6
_::create([1, 2, 3, 4])->reduce(function ($memo, $n) {
return $memo + $n;
}); // 10
_::create([1, 2, 3, 4])->reduce(function ($s, $n) {
return $s + $n;
}, 10); // 20
_::create([1, 2, 3, 4])->reject(function ($n) {
return ($n % 2) == 0;
}); // [1, 3]
_::create([1, 2, 3, 4])->reverse(); // [4, 3, 2, 1]
_::create([1, 2, 3, 4, 5, 6])->rotate(2); // [3, 4, 5, 6, 1, 2]
_::create([1, 2, 3, 4, 5, 6])->rotate(-2); // [5, 6, 1, 2, 3, 4]
_::create([1, 2, 3, 4, 5, 6])->sample(); // Basically a dice roll...
_::create([1, 2, 3, 4])->select(function ($n) {
return ($n % 2) == 0;
}); // [2, 4]
_::create([1, 2])->shuffle(); // Either [1, 2] or [2, 1]
_::create([1, 2, 3, 4, 5, 6])->skip(2); // [3, 4, 5, 6]
_::create([1, 2, 3, 4])->slice(1, 2); // [2, 3]
_::create([1, 2, 3, 4, 5, 6])->snip(2); // [1, 2, 3, 4]
_::create([1, 4, 2, 3])->sort(); // [1, 2, 3, 4]
$rhombas = new Shape('rhombas');
$ellipse = new Shape('ellipse');
$hexagon = new Shape('hexagon');
_::create([ $rhombas, $ellipse, $hexagon ])->sortBy(function ($s) {
return $s->getName();
}); // [ $ellipse, $hexagon, $rhombas ]
_::create([1, 2, 3, 4])->sum(); // 10
_::create([[1, 2, 3], [4, 5, 6]])->transpose(); // [[1, 4], [2, 5], [3, 6]]
_::create([1, 2, 3, 1, 2, 4, 1, 2, 5])->uniq(); // [3, 4, 5]
_::create([1, 2, 4, 3])->without([4]); // [1, 2, 3]
_::create([1, 2, 3, 4, 5])->without([4, 5]); // [1, 2, 3]
_::create()->push(1)->push(2)->push(3)->pop(); // 3
_::create()->push(1)->push(2)->push(3); // [1, 2, 3]
_::create([1, 2, 3])->shift(); // 1
_::create([2, 3])->unshift(1); // [1, 2, 3]
_::split('foo bar baz', ' '); // ['foo', 'bar', 'baz']
_::split('1234'); // ['1', '2', '3', '4']
_::split('1234')->sum(); // 10
_::create([1, 2, 3, 4, 5])->first(2); // [1, 2]
_::create([1, 2, 3, 4, 5])->last(2); // [4, 5]
_::create([1, 2, 3, 4, 5])->skip(2); // [3, 4, 5]
_::create([1, 2, 3, 4, 5])->snip(2); // [1, 2, 3]
_::create([1, 2, 3, 4, 5])->slice(2, 2); // [3, 4]