php/accumulate
This commit is contained in:
parent
b751a92b9f
commit
9c00dded72
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"blurb": "Implement the `accumulate` operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.",
|
||||
"authors": ["pmatseykanets"],
|
||||
"contributors": ["arueckauer", "kunicmarko20", "kytrinyx", "petemcfarlane"],
|
||||
"files": {
|
||||
"solution": ["Accumulate.php"],
|
||||
"test": ["AccumulateTest.php"],
|
||||
"example": [".meta/example.php"]
|
||||
},
|
||||
"source": "Conversation with James Edward Gray II",
|
||||
"source_url": "https://twitter.com/jeg2"
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
{"track":"php","exercise":"accumulate","id":"eaba0161e1314aeabdfd452f0293d217","url":"https://exercism.org/tracks/php/exercises/accumulate","handle":"benharri","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function accumulate(array $input, callable $accumulator): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($input as $i)
|
||||
$result[] = $accumulator($i);
|
||||
return $result;
|
||||
}
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* By adding type hints and enabling strict type checking, code can become
|
||||
* easier to read, self-documenting and reduce the number of potential bugs.
|
||||
* By default, type declarations are non-strict, which means they will attempt
|
||||
* to change the original type to match the type specified by the
|
||||
* type-declaration.
|
||||
*
|
||||
* In other words, if you pass a string to a function requiring a float,
|
||||
* it will attempt to convert the string value to a float.
|
||||
*
|
||||
* To enable strict mode, a single declare directive must be placed at the top
|
||||
* of the file.
|
||||
* This means that the strictness of typing is configured on a per-file basis.
|
||||
* This directive not only affects the type declarations of parameters, but also
|
||||
* a function's return type.
|
||||
*
|
||||
* For more info review the Concept on strict type checking in the PHP track
|
||||
* <link>.
|
||||
*
|
||||
* To disable strict typing, comment out the directive below.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
class AccumulateTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
public static function setUpBeforeClass(): void
|
||||
{
|
||||
require_once 'Accumulate.php';
|
||||
}
|
||||
|
||||
public function testAccumulateEmpty(): void
|
||||
{
|
||||
$accumulator = function ($value) {
|
||||
return $value ** 2;
|
||||
};
|
||||
|
||||
$this->assertEquals([], accumulate([], $accumulator));
|
||||
}
|
||||
|
||||
public function testAccumulateSquares(): void
|
||||
{
|
||||
$accumulator = function ($value) {
|
||||
return $value ** 2;
|
||||
};
|
||||
|
||||
$this->assertEquals([1, 4, 9], accumulate([1, 2, 3], $accumulator));
|
||||
}
|
||||
|
||||
public function testAccumulateUpperCases(): void
|
||||
{
|
||||
$accumulator = function ($string) {
|
||||
return mb_strtoupper($string);
|
||||
};
|
||||
|
||||
$this->assertEquals(['HELLO', 'WORLD!'], accumulate(['Hello', 'World!'], $accumulator));
|
||||
}
|
||||
|
||||
public function testAccumulateReversedStrings(): void
|
||||
{
|
||||
$accumulator = function ($string) {
|
||||
return strrev($string);
|
||||
};
|
||||
|
||||
$this->assertEquals(['Hello', 'World!'], accumulate(['olleH', '!dlroW'], $accumulator));
|
||||
}
|
||||
|
||||
public function testAccumulateConstants(): void
|
||||
{
|
||||
$accumulator = function () {
|
||||
return 1;
|
||||
};
|
||||
|
||||
$this->assertEquals([1, 1], accumulate(['Hello', 'World!'], $accumulator));
|
||||
}
|
||||
|
||||
public function testAccumulateWithinAccumulate(): void
|
||||
{
|
||||
$chars = ['a', 'b', 'c'];
|
||||
$digits = [1, 2, 3];
|
||||
$expected = [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3']];
|
||||
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
accumulate($chars, function ($char) use ($digits) {
|
||||
return accumulate($digits, function ($digit) use ($char) {
|
||||
return $char . $digit;
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// Additional points for making the following tests pass
|
||||
|
||||
public function testAccumulateUsingBuiltInFunction(): void
|
||||
{
|
||||
$this->assertEquals(['Hello', 'World!'], accumulate([" Hello\t", "\t World!\n "], 'trim'));
|
||||
}
|
||||
|
||||
public function testAccumulateUsingStaticMethod(): void
|
||||
{
|
||||
$this->assertEquals([5, 6], accumulate(['Hello', 'World!'], 'Str::len'));
|
||||
}
|
||||
|
||||
public function testAccumulateUsingInvoke(): void
|
||||
{
|
||||
$this->assertEquals([['f', 'o', 'o']], accumulate(['foo'], new StrSpliter()));
|
||||
}
|
||||
|
||||
public function testAccumulateUsingObjectAndArrayNotation(): void
|
||||
{
|
||||
$this->assertEquals([true, false, false], accumulate(['Yes', 0, []], [new Is(), 'truthy']));
|
||||
}
|
||||
}
|
||||
|
||||
class Str
|
||||
{
|
||||
public static function len($string): int
|
||||
{
|
||||
return strlen($string);
|
||||
}
|
||||
}
|
||||
|
||||
class StrSpliter
|
||||
{
|
||||
public function __invoke($value)
|
||||
{
|
||||
return str_split($value);
|
||||
}
|
||||
}
|
||||
|
||||
class Is
|
||||
{
|
||||
public function truthy($value): bool
|
||||
{
|
||||
return boolval($value);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
## Running the tests
|
||||
|
||||
1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`.
|
||||
To find the Exercism workspace run
|
||||
|
||||
➜ exercism debug | grep Workspace
|
||||
|
||||
1. Get [PHPUnit] if you don't have it already.
|
||||
|
||||
➜ wget -O phpunit https://phar.phpunit.de/phpunit-9.phar
|
||||
➜ chmod +x phpunit
|
||||
➜ ./phpunit --version
|
||||
|
||||
2. Execute the tests:
|
||||
|
||||
➜ ./phpunit file_to_test.php
|
||||
|
||||
For example, to run the tests for the Hello World exercise, you would run:
|
||||
|
||||
➜ ./phpunit HelloWorldTest.php
|
||||
|
||||
[PHPUnit]: http://phpunit.de
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit Accumulate.php` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [PHP track's documentation](https://exercism.org/docs/tracks/php)
|
||||
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
To get help if you're having trouble, you can use one of the following resources:
|
||||
|
||||
- [/r/php](https://www.reddit.com/r/php) is the PHP subreddit.
|
||||
- [StackOverflow](https://stackoverflow.com/questions/tagged/php) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
|
@ -0,0 +1,48 @@
|
|||
# Accumulate
|
||||
|
||||
Welcome to Accumulate on Exercism's PHP Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Implement the `accumulate` operation, which, given a collection and an
|
||||
operation to perform on each element of the collection, returns a new
|
||||
collection containing the result of applying that operation to each element of
|
||||
the input collection.
|
||||
|
||||
Given the collection of numbers:
|
||||
|
||||
- 1, 2, 3, 4, 5
|
||||
|
||||
And the operation:
|
||||
|
||||
- square a number (`x => x * x`)
|
||||
|
||||
Your code should be able to produce the collection of squares:
|
||||
|
||||
- 1, 4, 9, 16, 25
|
||||
|
||||
Check out the test suite to see the expected function signature.
|
||||
|
||||
## Restrictions
|
||||
|
||||
Keep your hands off that collect/map/fmap/whatchamacallit functionality
|
||||
provided by your standard library!
|
||||
Solve this one yourself using other basic tools instead.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @pmatseykanets
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @arueckauer
|
||||
- @kunicmarko20
|
||||
- @kytrinyx
|
||||
- @petemcfarlane
|
||||
|
||||
### Based on
|
||||
|
||||
Conversation with James Edward Gray II - https://twitter.com/jeg2
|
Loading…
Reference in New Issue