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 take() function #10

Merged
merged 1 commit into from
Jan 3, 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The target can be any expression. It will be passed as the first argument to the
| `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)` |

#### Custom Functions

Expand Down
12 changes: 12 additions & 0 deletions src/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use function array_is_list;
use function array_keys;
use function array_map;
use function array_slice;
use function array_values;
use function count;
use function get_debug_type;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function __construct(private readonly array $vars = [], array $funcs = []
'filter' => self::filter(...),
'some' => self::some(...),
'substr' => substr(...),
'take' => self::take(...),
] : [];
$shadowed = array_intersect(array_keys($predefinedFuncs), array_keys($funcs));
if ($shadowed !== []) {
Expand Down Expand Up @@ -126,6 +128,16 @@ private static function filter(array $items, callable $predicate): array
return array_is_list($items) ? array_values($out) : $out;
}

/**
* @template T
* @param list<T> $items
* @return list<T>
*/
private static function take(array $items, int $n): array
{
return array_slice($items, 0, $n);
}

/**
* @internal
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/ExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public static function evaluateCases(): iterable
true,
],
['myMap:map<int, string>', new Scope(['myMap' => [42 => 'a', 69 => 'b']]), [42 => 'a', 69 => 'b']],
[
'items:list<string>.take:list<string>(3)',
new Scope(['items' => ['a', 'b', 'c', 'd', 'e']]),
['a', 'b', 'c'],
],
['items:list<string>.take:list<string>(3)', new Scope(['items' => ['a', 'b']]), ['a', 'b']],
['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']]), []],
];
foreach ($cases as [$expr, $scope, $expected]) {
$expectedStr = (string)Expr::literal($expected);
Expand Down