Esempio n. 1
0
/**
 * 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));
}
 public function convert(Request $request)
 {
     $headers = $request->getHeaders();
     $keysMap = f\map_indexed(function ($v, $k) {
         return 'HTTP_' . $k;
     }, $headers);
     $server = f\rename_keys($headers, $keysMap);
     return SymfonyRequest::create($request->getUri(), $request->getMethod(), $request->getParameters(), $cookies = array(), $files = array(), $server, $request->getContent());
 }
Esempio n. 3
0
function functions_info()
{
    $functionFromFile = f\compose(f\partial('substr', f\_(), 0, -4), 'basename');
    $getFunctions = function () use($functionFromFile) {
        $files = glob(__DIR__ . '/functions/*.php');
        $keysMap = f\map($functionFromFile, $files);
        return f\rename_keys($files, $keysMap);
    };
    $functions = $getFunctions();
    $buildInfo = function ($file) use($functionFromFile) {
        $function = $functionFromFile($file);
        $code = file_get_contents($file);
        return array('name' => $function, 'file' => $file, 'code' => $code, 'doc' => function_doc_info_from_code($code));
    };
    return f\map($buildInfo, $functions);
}
Esempio n. 4
0
function benchmark_compares()
{
    $number = 100;
    $array = range(1, $number);
    return ['map' => ['array_map' => function () use($array) {
        array_map(function () {
        }, $array);
    }, 'f/map' => function () use($array) {
        f\map(function () {
        }, $array);
    }], 'reduce' => ['array_reduce' => function () use($array) {
        array_reduce($array, function ($a, $b) {
            return $a + $b;
        });
    }, 'f/reduce' => function () use($array) {
        f\reduce(function ($a, $b) {
            return $a + $b;
        }, $array);
    }], 'filter' => ['array_filter' => function () use($array) {
        array_filter($array, function ($v) {
            return $v % 2;
        });
    }, 'f/filter' => function () use($array) {
        f\filter(function ($v) {
            return $v % 2;
        }, $array);
    }], 'rename_keys' => ['raw' => function () use($array) {
        $result = array();
        foreach ($array as $key => $v) {
            $result['a' . $key] = $v;
        }
    }, 'f\\rename_keys' => function () use($array) {
        f\rename_keys($array, f\map(function ($k) {
            return 'a' . $k;
        }, $array));
    }]];
}
Esempio n. 5
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()));
 }
Esempio n. 6
0
 /**
  * @dataProvider renameKeysProvider
  */
 public function testRenameKeys($coll)
 {
     $this->assertSame(array('one' => 3, 'two' => 9), f\rename_keys($coll, array('foo' => 'one', 'bar' => 'two')));
     $this->assertSame(array('one' => 3, 'bar' => 9), f\rename_keys(f\to_array($coll), array('foo' => 'one', 'bar' => 'bar')));
 }