Skip to content

Commit

Permalink
Callable and cast md doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
whsv26 committed May 16, 2021
1 parent 4d7be7d commit da47a80
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 5 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
PSALM := vendor/bin/psalm
PHPUNIT := vendor/bin/phpunit
PHP=php
PSALM=vendor/bin/psalm
PHPUNIT=vendor/bin/phpunit

build-doc:
php ./linker.php

psalm-analyse:
$(PSALM) src
Expand All @@ -12,3 +16,4 @@ run-static-tests:

run-runtime-tests:
$(PHPUNIT) tests/Runtime

13 changes: 13 additions & 0 deletions linker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Doc\DocLinker;

require_once 'vendor/autoload.php';

$linker = new DocLinker();
$linker->link(__DIR__ . '/src/Doc/Md/*/');

return 0;

6 changes: 3 additions & 3 deletions src/Doc/DocLinker.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ private function parseHeaders(string $path): array
return $parser->getHeaders();
}

public function link(): void
public function link(string $pattern): void
{
$dirs = glob(__DIR__ . '/doc/*/', GLOB_ONLYDIR);
$dirs = glob($pattern, GLOB_ONLYDIR);

foreach ($dirs as $dir) {
$fromPattern = $dir . '*.md';
$to = __DIR__ . '/../doc/' . basename($dir) . '.md';
$to = __DIR__ . '/../../doc/' . basename($dir) . '.md';
$commandLine = "pandoc --from gfm --to gfm --output $to $fromPattern";

Process::fromShellCommandline($commandLine)->run();
Expand Down
37 changes: 37 additions & 0 deletions src/Doc/Md/Functions/Callable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Callable
- #### compose
Compose functions

Output of one function will be passed as input to another function

```php
$aToB = fn(int $a): bool => true;
$bToC = fn(bool $b): string => (string) $b;
$cTod = fn(string $c): float => (float) $c;

/** @var callable(int): float $result */
$result = \Fp\Callable\compose($aToB, $bToC, $cTod);
```

- #### partial and partialLeft
Partial application from first function argument

```php
$callback = fn(int $a, string $b, bool $c): bool => true;

/** @var callable(bool): bool $result */
$result = \Fp\Callable\partial($callback, 1, "string");

/** @var callable(bool): bool $result */
$result = \Fp\Callable\partialLeft($callback, 1, "string");
```

- #### partialRight
Partial application from last function argument

```php
$callback = fn(int $a, string $b, bool $c): bool => true;

/** @var callable(int): bool $result */
$result = \Fp\Callable\partialRight($callback, true, "string");
```
74 changes: 74 additions & 0 deletions src/Doc/Md/Functions/Cast.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Cast
- #### asArray
Copy collection as array

```php
/** @psalm-return iterable<string, int> */
function getCollection(): array { return []; }

/** @var array<string, int> $result */
$result = asArray(getCollection());
```

- #### asBool
Try cast boolean like value

Returns None if cast is not possible

```php
/** @var Option<bool> $result */
$result = asBool('yes');
```

- #### asFloat
Try cast float like value

Returns None if cast is not possible

```php
/** @var Option<float> $result */
$result = asFloat('1.1');
```

- #### asInt
Try cast integer like value

Returns None if cast is not possible

```php
/** @var Option<int> $result */
$result = asInt(1);
```

- #### asList
Copy one or multiple collections as list

```php
$result = asList([1], ['prop' => 2], [3, 4]); // [1, 2, 3, 4]
```

- #### asNonEmptyArray
Try copy and cast collection to non-empty-array

Returns None if there is no first collection element

```php
/** @psalm-return iterable<string, int> */
function getCollection(): array { return []; }

/** @var Option<non-empty-array<string, int>> $result */
$result = asNonEmptyArray(getCollection());
```

- #### asNonEmptyList
Try copy and cast collection to non-empty-list

Returns None if there is no first collection element

```php
/** @psalm-return iterable<string, int> */
function getCollection(): array { return []; }

/** @var Option<non-empty-list<int>> $result */
$result = asNonEmptyList(getCollection());
```

0 comments on commit da47a80

Please sign in to comment.