Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add map() function #13

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ The target can be any expression. It will be passed as the first argument to the

#### Built-In Functions

| Function | Description | Example |
|------------|-------------------------------------------------------------|--------------------------------------------------|
| `count` | Returns the number of elements in a list | `foo:list<string>.count:int()` |
| `contains` | Returns whether a list contains a value | `foo:list<string>.contains:bool("bar")` |
| `some` | Returns whether any element matches a [predicate](#lambdas) | `foo:list<int>.some:bool(\|item\| item:int > 5)` |
| `substr` | Returns a substring of a string | `foo:string.substr:string(0, 5)` |
| `take` | Returns the first n elements of a list | `foo:list<string>.take:list<string>(5)` |
| Function | Description | Example |
|------------|------------------------------------------------------------------------|--------------------------------------------------|
| `count` | Returns the number of elements in a list | `foo:list<string>.count:int()` |
| `contains` | Returns whether a list contains a value | `foo:list<string>.contains:bool("bar")` |
| `map` | Returns a new list with the results of applying a [function](#lambdas) | `foo:list<int>.map:list<int>(\|i\| i:int - 2)` |
| `some` | Returns whether any element matches a [predicate](#lambdas) | `foo:list<int>.some:bool(\|item\| item:int > 5)` |
| `substr` | Returns a substring of a string | `foo:string.substr:string(0, 5)` |
| `take` | Returns the first n elements of a list | `foo:list<string>.take:list<string>(5)` |

#### Custom Functions

Expand Down
14 changes: 14 additions & 0 deletions src/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function __construct(private readonly array $vars = [], array $funcs = []
'contains' => self::contains(...),
'count' => self::count(...),
'filter' => self::filter(...),
'map' => self::map(...),
'some' => self::some(...),
'substr' => substr(...),
'take' => self::take(...),
Expand All @@ -69,6 +70,19 @@ public function __construct(private readonly array $vars = [], array $funcs = []
}
}

/**
* @template K of array-key
* @template From
* @template To
* @param array<K, From> $items
* @param callable(From): To $f
* @return array<K, To>
*/
private static function map(array $items, callable $f): array
{
return array_map($f, $items);
}

/**
* @template T
* @param array<array-key, T> $haystack
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/ExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public static function evaluateCases(): iterable
['items:list<string>.take:list<string>(3)', new Scope(['items' => []]), []],
['items:list<string>.take:list<string>(0)', new Scope(['items' => ['a', 'b', 'c', 'd', 'e']]), []],
['-myval:int', new Scope(['myval' => 42]), -42],
[
'names:list<string>.map:list<bool>(|name| name:string === "bar")',
new Scope(['names' => ['foo', 'bar', 'baz']]),
[false, true, false],
],
['ints:list<int>.map:list<int>(|i| i:int -2)', new Scope(['ints' => []]), []],
];
foreach ($cases as [$expr, $scope, $expected]) {
$expectedStr = (string)Expr::literal($expected);
Expand Down
Loading