/** * @SuppressWarnings(PHPMD.UnusedLocalVariable) * @SuppressWarnings(PHPMD.ShortVariableName) */ private static function extractTermSet(\SimpleXMLElement $xmlObj) { return F\reduce_left($xmlObj->terms->term, function ($currentTerm, $_, $__, $termSet) { $termName = (string) $currentTerm['name']; if (!isset($termSet->{$termName})) { $termSet->{$termName} = []; } $form = (string) $currentTerm['form'] ?: 'long'; $entry = self::extractEntry($currentTerm, $form); array_push($termSet->{$termName}, $entry); return $termSet; }, new \stdClass()); }
public function testPassNonCallableToReduceLeft() { $this->expectArgumentError("Argument 2 passed to Functional\\reduce_left() must be callable"); reduce_left($this->list, 'undefinedFunction'); }
return $reduction + $value; }; $numbers = reduce_left(select(range(3, 999), $isDivisibleBy3or5), $addReduce); return $numbers; }); Route::get('problem2', function () { // Callback that determines whether a value is even $isEven = function ($number) { return $number % 2 == 0; }; // Sorta cheated here... // Really wanted to do this in a functional way // So I created a iterator object // That returns a fib sequence under a certain number $solution = reduce_left(select(new takeWhileFib(4000000), $isEven), function ($value, $index, $collection, $reduction) { return $reduction + $value; }, 0); return $solution; }); Route::get('problem3', function () { //The prime factors of 13195 are 5, 7, 13 and 29. //What is the largest prime factor of the number 600851475143 ? // How can we do this in a functional way? // Well Brute Force would be... // max($number => getPrimeFactors) $pf = function ($number) { return select(new takeWhile($number), function ($n, $number) { if ($n > 0) { return $number % $n == 0; } return false;
/** * Calculates what the final dimensions of an image will be after the * passed style is applied to it. * * @param array $style * A Drupal style definition array. * * @return \stdClass * An object with the integer|null properties `width` and `height`, and * the boolean property `firm`. `firm` represents whether or not the * dimensions are guaranteed. If not guaranteed, `width` and `height` * are only maximums. * */ private function finalDimensionsOfStyle(array $style) { $dimensions = F\reduce_left($style['effects'], function ($effect, $i, $c, $dim) { $width = (int) $effect['data']['width'] ?: null; $height = (int) $effect['data']['height'] ?: null; if ($effect['name'] !== 'image_scale') { # Every targeted effect except for `image_scale` sets a firm width and # height. This means that the width and height can be guaranteed. return (object) ['width' => $width, 'height' => $height, 'firm' => true]; } foreach (['width', 'height'] as $side) { if ($dim->{$side} === 0) { $dim->{$side} = (int) ${$side}; } } $upscale = $effect['data']['upscale']; if (!$upscale and (!$width or $width > $dim->width) and (!$height or $height > $dim->height)) { return $dim; } $dim = (array) $dim; image_dimensions_scale($dim, $width, $height, $upscale); return (object) $dim; }, (object) ['width' => 0, 'height' => 0, 'firm' => false]); $dimensions->width = $dimensions->width ?: null; $dimensions->height = $dimensions->height ?: null; return $dimensions; }
/** * Process the compare feature. * * @param array $tables * @param Config $config * * @return array */ private function processCompare(array $tables, Config $config) { if (!isset($config['compare'])) { return $tables; } $conditions = array_diff($config['cols'], $this->statKeys, [$config['compare']]); $compare = $config['compare']; $compareFields = $config['compare_fields']; return F\map($tables, function ($table) use($conditions, $compare, $compareFields) { $groups = F\group($table, function ($row) use($conditions, $compare, $compareFields) { $values = array_intersect_key($row->getArrayCopy(), array_flip($conditions)); return F\reduce_left($values, function ($value, $i, $c, $reduction) { return $reduction . $value; }); }); $table = []; $colNames = null; foreach ($groups as $group) { $firstRow = null; foreach ($group as $row) { if (null === $firstRow) { $firstRow = $row->newInstance(array_diff_key($row->getArrayCopy(), array_flip($this->statKeys))); if (isset($firstRow[$compare])) { unset($firstRow[$compare]); } foreach ($compareFields as $compareField) { if (isset($firstRow[$compareField])) { unset($firstRow[$compareField]); } } } if (null === $colNames) { $colNames = array_combine($firstRow->getNames(), $firstRow->getNames()); } $compared = $row[$compare]; foreach ($compareFields as $compareField) { $name = $compare . ':' . $compared . ':' . $compareField; $name = $this->resolveCompareColumnName($firstRow, $name); $firstRow[$name] = $row[$compareField]; $colNames[$name] = $name; // we invent a new col name here, use the compare field's class. $this->classMap[$name] = $this->classMap[$compareField]; } } $table[] = $firstRow; } $table = F\map($table, function ($row) use($colNames) { $newRow = $row->newInstance([]); foreach ($colNames as $colName) { $newRow[$colName] = isset($row[$colName]) ? $row[$colName] : null; } return $newRow; }); return $table; }); return $tables; }
/** * Generates the HTML to represent the passed image in this slot definition. * * @param SrcsetGeneratorInterface $srcset_gen * The generator instance that will be used to generate the srcset * attributes. * @param mixed $image * The image representation that will be passed to $srcset_gen * @param string $alt * The value of the <img> `alt` attribute. * * @return string * The HTML for the image. An <img> if the slot can be represented by a * * @see SrcsetGeneratorInterface * @see Source::renderWith() */ public function renderWith(SrcsetGeneratorInterface $srcset_gen, $image, $alt = '') { # A naked <img> is used if possible since browser support for <picture> # is still not great. However, support for <img> with `srcset` and `sizes` # attributes is much better. This reduces how often a polyfill is required # to run. # @see http://caniuse.com/#search=picture if (count($this->sources) === 1) { return $this->sources[0]->renderWith($srcset_gen, $image, $alt); } $html = F\reduce_left($this->sources, function (Source $source, $i, $c, $acc) use($image, $srcset_gen, $alt) { return "{$acc}\n " . $source->renderWith($srcset_gen, $image, $alt); }, ''); return "<picture>{$html}\n</picture>"; }
/** * @param $initial * @param $f * @return array */ public function reduce($initial, $f) { return F\reduce_left($this->_data, $f, $initial); }