public function nameToReplace($name) { if (!$this->hasToReplace($name)) { throw new \InvalidArgumentException(sprintf('The name "%s" does not have to replace.', $name)); } return f\first($this->matchesForName($name)); }
public function testFirst() { $coll1 = range(2, 10); $this->assertSame(2, f\first($coll1)); $coll2 = new \ArrayObject(array('a', 'b', 'c')); $this->assertSame('a', f\first($coll2)); }
/** * f\rename_keys($coll, $keysMap) * * Returns a new coll with the keys from keysMap renamed. * * f\rename_keys(array('a' => 1, 'b' => 2), array('a' => 'c', 'b' => 'd')) * => array('c' => 1, 'd' => 2) */ function rename_keys($coll, $keysMap) { if (f\not($keysMap)) { return $coll; } $from = f\first(f\keys($keysMap)); $to = f\first($keysMap); return f\rename_keys(f\rename_key($coll, $from, $to), f\dissoc($keysMap, $from)); }
function _partial_merge_args($left, $right) { foreach ($left as &$v) { if ($v instanceof placeholder) { if (empty($right)) { throw new \InvalidArgumentException('The placeholder cannot be resolved.'); } $v = f\first($right); $right = f\rest($right); } } return array_merge($left, $right); }
function _coll_depth($array, $depth) { $first = f\first($depth); if (f\contains($array, $first)) { $arrayIn = f\to_array(f\get($array, $first)); $inRest = f\rest($depth); if (count($inRest)) { return f\_coll_depth($arrayIn, $inRest); } return $arrayIn; } return false; }
/** * f\max($coll, $fn = null) * * Returns the maximum value of coll when applying fn. * If fn is not set, no function is applied. * * // without fn * f\max(array(1, 2, 3)) * => 3 * * //with fn * $users = array(array('name' => 'foo', 'age' => 10), array('name' => 'bar', 'age' => 20)) * $fn = function ($v) { return $v['age']; } * f\max($users, $fn) * => array('name' => 'bar', 'age' => 20) * * //with f\key * f\max($users, f\key('age')) * => array('name' => 'bar', 'age' => 20) */ function max($coll, $fn = null) { if ($fn === null) { $fn = function ($v) { return $v; }; } $maxValue = f\first($coll); $maxCompare = call_user_func($fn, $maxValue); foreach (f\rest($coll) as $value) { $compare = call_user_func($fn, $value); if ($compare > $maxCompare) { $maxValue = $value; $maxCompare = $compare; } } return $maxValue; }
private function parametersFromTable(TableNode $table) { return f\rename_keys(f\map(function ($v) { return f\first(f\rest($v)); }, $table->getRows()), f\map('felpado\\first', $table->getRows())); }