Skip to content

Commit

Permalink
Simplified implementations for A methods
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed May 10, 2021
1 parent 6ab30f3 commit bac538d
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,7 @@ public static function filter(callable $fn, array $data): array
// A::find(x => x.a > 3, [['a' => 8], ['a' => 10]]) -> ['a' => 8]
public static function find(callable $fn, array $data)
{
$results = self::filter($fn, $data);
if (self::isEmpty($results)) {
return null;
}

return self::first($results);
return self::head(self::filter($fn, $data));
}

// A::findLast(x => x.a > 3, [['a' => 8], ['a' => 10]]) -> ['a' => 10]
Expand Down Expand Up @@ -332,20 +327,13 @@ public static function pickRandom(array $data)

// A::concat([1, 2], 3, [4, 5]) -> [1, 2, 3, 4, 5]
public static function concat(...$args) {
return self::unnest(A::map(function($arg) {
return self::ensureArray($arg);
},$args));
return self::unnest(A::map(fn($arg) => self::ensureArray($arg),$args));
}

// A::zipObj(['a', 'b'], [1, 2]) -> {a:1, b:2}
public static function zipObj(array $keys, array $values): object {
$_keys = self::uniq(self::concat(self::keys($keys), self::keys($values)));
$_keys = self::filter(function($key) use ($keys, $values) {
return array_key_exists($key, $keys) && array_key_exists($key, $values);
}, $_keys);

return self::reduce(function($result, $key) use ($keys, $values) {
return O::assoc($keys[$key], $values[$key], $result);
}, new stdClass(), $_keys);
$_keys = self::filter(fn($key) => array_key_exists($key, $keys) && array_key_exists($key, $values), $_keys);
return self::reduce(fn($result, $key) => O::assoc($keys[$key], $values[$key], $result), new stdClass(), $_keys);
}
}

0 comments on commit bac538d

Please sign in to comment.