Skip to content

Commit e8efbf1

Browse files
committed
feat(examples): add typical usage examples
1 parent 064b896 commit e8efbf1

File tree

6 files changed

+283
-0
lines changed

6 files changed

+283
-0
lines changed

examples/assignById.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* ----------------------------------------------------------------------------
4+
* This code is part of the Sclable Business Application Development Platform
5+
* and is subject to the provisions of your License Agreement with
6+
* Sclable Business Solutions GmbH.
7+
*
8+
* @copyright (c) 2016 Sclable Business Solutions GmbH
9+
* ----------------------------------------------------------------------------
10+
*/
11+
12+
require_once dirname(__DIR__) . '/vendor/autoload.php';
13+
$instances = include __DIR__ . '/data/listOfInstances.php';
14+
15+
$foreach = \sclable\arrayFunctions\ArrayWrap::create([]);
16+
$map = \sclable\arrayFunctions\ArrayWrap::create([]);
17+
$wrapMap = \sclable\arrayFunctions\ArrayWrap::create([]);
18+
19+
for ($i = 0; $i < 100; $i++) {
20+
// foreach
21+
$start = microtime(true);
22+
$assigned = [];
23+
foreach ($instances as $instance) {
24+
$assigned[$instance->id] = $instance;
25+
}
26+
$end = microtime(true);
27+
$foreach->push($end - $start);
28+
29+
// map
30+
$start = microtime(true);
31+
$assigned2 = array_combine(array_map(function ($instance) {
32+
return $instance->id;
33+
}, $instances), $instances);
34+
$end = microtime(true);
35+
$map->push($end - $start);
36+
37+
// ArrayWrap map
38+
$start = microtime(true);
39+
$assigned3 = array_combine(\sclable\arrayFunctions\ArrayWrap::create($instances)->map(function ($instance) {
40+
return $instance->id;
41+
})->getRaw(), $instances);
42+
$end = microtime(true);
43+
$wrapMap->push($end - $start);
44+
}
45+
46+
echo 'foreach: ' . printResult($foreach) . PHP_EOL;
47+
echo 'map: ' . printResult($map) . PHP_EOL;
48+
echo 'wrapMap: ' . printResult($wrapMap) . PHP_EOL;
49+
50+
function printResult(\sclable\arrayFunctions\ArrayWrap $results)
51+
{
52+
return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}";
53+
}

examples/data/listOfInstances.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* ----------------------------------------------------------------------------
4+
* This code is part of the Sclable Business Application Development Platform
5+
* and is subject to the provisions of your License Agreement with
6+
* Sclable Business Solutions GmbH.
7+
*
8+
* @copyright (c) 2016 Sclable Business Solutions GmbH
9+
* ----------------------------------------------------------------------------
10+
*/
11+
12+
13+
return \sclable\arrayFunctions\ArrayWrap::create(range(1, 10000))->map(function ($id) {
14+
return (object) [
15+
'id' => $id,
16+
'name' => 'name - ' . $id,
17+
];
18+
})->getRaw();

examples/extractIds.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* ----------------------------------------------------------------------------
4+
* This code is part of the Sclable Business Application Development Platform
5+
* and is subject to the provisions of your License Agreement with
6+
* Sclable Business Solutions GmbH.
7+
*
8+
* @copyright (c) 2016 Sclable Business Solutions GmbH
9+
* ----------------------------------------------------------------------------
10+
*/
11+
12+
require_once dirname(__DIR__) . '/vendor/autoload.php';
13+
$instances = include __DIR__ . '/data/listOfInstances.php';
14+
15+
$foreach = \sclable\arrayFunctions\ArrayWrap::create([]);
16+
$map = \sclable\arrayFunctions\ArrayWrap::create([]);
17+
$mapMethod = \sclable\arrayFunctions\ArrayWrap::create([]);
18+
$wrapMap = \sclable\arrayFunctions\ArrayWrap::create([]);
19+
20+
for ($i = 0; $i < 100; $i++) {
21+
// foreach
22+
$start = microtime(true);
23+
$assigned = [];
24+
foreach ($instances as $instance) {
25+
$assigned[] = $instance->id;
26+
}
27+
$end = microtime(true);
28+
$foreach->push($end - $start);
29+
30+
// map
31+
$start = microtime(true);
32+
$assigned2 = array_map(function ($instance) {
33+
return $instance->id;
34+
}, $instances);
35+
$end = microtime(true);
36+
$mapMethod->push($end - $start);
37+
38+
// method call
39+
$start = microtime(true);
40+
$assigned2 = array_map('extractId', $instances);
41+
$end = microtime(true);
42+
$map->push($end - $start);
43+
44+
// ArrayWrap map
45+
$start = microtime(true);
46+
$assigned3 = \sclable\arrayFunctions\ArrayWrap::create($instances)->map(function ($instance) {
47+
return $instance->id;
48+
});
49+
$end = microtime(true);
50+
$wrapMap->push($end - $start);
51+
}
52+
53+
echo 'foreach: ' . printResult($foreach) . PHP_EOL;
54+
echo 'map: ' . printResult($map) . PHP_EOL;
55+
echo 'map method: ' . printResult($mapMethod) . PHP_EOL;
56+
echo 'wrapMap: ' . printResult($wrapMap) . PHP_EOL;
57+
58+
function printResult(\sclable\arrayFunctions\ArrayWrap $results)
59+
{
60+
return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}";
61+
}
62+
63+
function extractId($instance)
64+
{
65+
return $instance->id;
66+
}

examples/htmlEntites.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* ----------------------------------------------------------------------------
4+
* This code is part of the Sclable Business Application Development Platform
5+
* and is subject to the provisions of your License Agreement with
6+
* Sclable Business Solutions GmbH.
7+
*
8+
* @copyright (c) 2016 Sclable Business Solutions GmbH
9+
* ----------------------------------------------------------------------------
10+
*/
11+
12+
require_once dirname(__DIR__) . '/vendor/autoload.php';
13+
$textList = array_map(function () {
14+
return 'abcdefgh';
15+
}, range(0, 10000));
16+
17+
$foreach = \sclable\arrayFunctions\ArrayWrap::create([]);
18+
$map = \sclable\arrayFunctions\ArrayWrap::create([]);
19+
$wrapMap = \sclable\arrayFunctions\ArrayWrap::create([]);
20+
21+
for ($i = 0; $i < 100; $i++) {
22+
// foreach
23+
$fTextList = $textList;
24+
$start = microtime(true);
25+
foreach ($fTextList as &$text) {
26+
$text = htmlentities($text);
27+
}
28+
$end = microtime(true);
29+
$foreach->push($end - $start);
30+
31+
// map
32+
$wTextList = $textList;
33+
$start = microtime(true);
34+
$wTextList = array_map('htmlentities', $wTextList);
35+
$end = microtime(true);
36+
$map->push($end - $start);
37+
38+
// ArrayWrap map
39+
$mTextList = $textList;
40+
$start = microtime(true);
41+
$mTextList = \sclable\arrayFunctions\ArrayWrap::create($mTextList)->apply('htmlentities');
42+
$end = microtime(true);
43+
$wrapMap->push($end - $start);
44+
}
45+
46+
echo 'foreach: ' . printResult($foreach) . PHP_EOL;
47+
echo 'map: ' . printResult($map) . PHP_EOL;
48+
echo 'wrapMap: ' . printResult($wrapMap) . PHP_EOL;
49+
50+
function printResult(\sclable\arrayFunctions\ArrayWrap $results)
51+
{
52+
return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}";
53+
}

examples/max.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* ----------------------------------------------------------------------------
4+
* This code is part of the Sclable Business Application Development Platform
5+
* and is subject to the provisions of your License Agreement with
6+
* Sclable Business Solutions GmbH.
7+
*
8+
* @copyright (c) 2016 Sclable Business Solutions GmbH
9+
* ----------------------------------------------------------------------------
10+
*/
11+
12+
use \sclable\arrayFunctions\ArrayWrap;
13+
14+
require_once dirname(__DIR__) . '/vendor/autoload.php';
15+
$numbers = ArrayWrap::range(-10000, -2)->shuffle()->getRaw();
16+
17+
18+
19+
// old
20+
$numbers = array_pad([], 10, 0);
21+
foreach ($numbers as &$number) {
22+
$number = rand();
23+
}
24+
25+
$max = null;
26+
foreach ($numbers as $number) {
27+
$max = $max === null ? $number : max($max, $number);
28+
}
29+
30+
echo $max . PHP_EOL;
31+
32+
// fp
33+
echo ArrayWrap::create([])
34+
->pad(10, 0)
35+
->map(function () { return rand(); })
36+
->reduce('max');

examples/sum.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* ----------------------------------------------------------------------------
4+
* This code is part of the Sclable Business Application Development Platform
5+
* and is subject to the provisions of your License Agreement with
6+
* Sclable Business Solutions GmbH.
7+
*
8+
* @copyright (c) 2016 Sclable Business Solutions GmbH
9+
* ----------------------------------------------------------------------------
10+
*/
11+
12+
use \sclable\arrayFunctions\ArrayWrap;
13+
14+
require_once dirname(__DIR__) . '/vendor/autoload.php';
15+
$numbers = ArrayWrap::range(-10000, -2)->shuffle()->getRaw();
16+
17+
18+
$foreach = ArrayWrap::create([]);
19+
$map = ArrayWrap::create([]);
20+
$wrapSum = ArrayWrap::create([]);
21+
22+
for ($i = 0; $i < 100; $i++) {
23+
// foreach
24+
$start = microtime(true);
25+
$max = null;
26+
foreach ($numbers as $number) {
27+
if ($max === null) {
28+
$max = $number;
29+
continue;
30+
}
31+
32+
$max = max($max, $number);
33+
}
34+
$end = microtime(true);
35+
$foreach->push($end - $start);
36+
37+
// map
38+
$start = microtime(true);
39+
$max = array_reduce($numbers, 'max', null);
40+
$end = microtime(true);
41+
$map->push($end - $start);
42+
43+
// ArrayWrap map
44+
$start = microtime(true);
45+
$max = ArrayWrap::create($numbers)->max();
46+
$end = microtime(true);
47+
$wrapSum->push($end - $start);
48+
}
49+
50+
echo 'foreach: ' . printResult($foreach) . PHP_EOL;
51+
echo 'map: ' . printResult($map) . PHP_EOL;
52+
echo 'wrapMap: ' . printResult($wrapSum) . PHP_EOL;
53+
54+
function printResult(ArrayWrap $results)
55+
{
56+
return "min: {$results->min()} / max: {$results->max()} / avg: {$results->avg()} || sum: {$results->sum()}";
57+
}

0 commit comments

Comments
 (0)