Пример #1
0
/**
 * f\method($method)
 *
 * Returns a closure that calls the given method and returns its value in an object.
 * Optionally additional args can be passed and will be sent when called the method.
 *
 * $getTimestamp = f\method('getTimestamp');
 * $getId(new \DateTime();
 * => `the timestamp`
 *
 * // with bound args
 * $format = f\method('format', 'Y-m-d H:i:s')
 * $format(new \DateTime())
 *
 * // useful with another functions
 * f\map(method('getId'), $articles)
 * => array(`ids of articles`)
 */
function method($method)
{
    $args = f\rest(func_get_args());
    return function ($object) use($method, $args) {
        return call_user_func_array(array($object, $method), $args);
    };
}
 private function matchesForName($name)
 {
     $matches = f\rest($this->matchesForRegex($name));
     if (count($matches)) {
         return $matches;
     }
     throw new \RuntimeException(sprintf('The name "%s" does not have matches.', $name));
 }
Пример #3
0
/**
 * f\equal($value1, $value2 & more)
 *
 * Returns whether two or more values are equal.
 *
 * f\equal(1, 1)
 * => true
 *
 * f\equal(1, 1, 1)
 * => true
 *
 * f\equal(1, 2)
 * => false
 *
 * f\equal(1, 1, 2)
 * => false
 */
function equal($v1, $v2)
{
    foreach (f\rest(func_get_args()) as $v) {
        if ($v1 != $v) {
            return false;
        }
    }
    return true;
}
Пример #4
0
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);
}
Пример #5
0
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;
}
Пример #6
0
/**
 * 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;
}
Пример #7
0
 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()));
 }
Пример #8
0
 /**
  * @dataProvider provideRest
  */
 public function testIndexedCollection($expected, $coll)
 {
     $this->assertSame($expected, f\rest($coll));
 }