public function testYouCanFyieldAFforInsteadOfAssembleThenReturn() { $test = FFor::create()->a(function () { return 1; })->fyield('a'); $this->assertEquals(1, $test); }
/** * Format the matrix contents for outputting * * @param Matrix $mA Matrix to format * @param array $options Options for formatter * - attribs => Collection of VertexDescription * - optional: edgeFunc => function($weight){return $newWeight;} * - optional: output:string script|object default = script * * If output is script, return the graphviz dot file contents * If output is object then return Fhaculty\Graph\Graph for your own * processing via Graphp\GraphViz\GraphViz * * @return Graph|string */ public function format(Matrix $mA, array $options = array()) { if (!$mA instanceof NumericMatrix) { throw new \InvalidArgumentException('Matrix is not NumericMatrix'); } return FFor::create(['mA' => $mA, 'graph' => new Graph(), 'options' => $options])->attribs(function ($options) { $attribs = array_key_exists('attribs', $options) ? $options['attribs'] : new Collection([], 'string'); if ($attribs instanceof Collection) { return $attribs; } throw new \InvalidArgumentException('options[attribs]'); })->edgeFunc(function ($options) { $edgeFunc = array_key_exists('edgeFunc', $options) ? $options['edgeFunc'] : function ($w) { return $w; }; if ($edgeFunc instanceof \Closure) { return $edgeFunc; } throw new \InvalidArgumentException('pptions[edgeFunc]'); })->output(function ($options) { return array_key_exists('output', $options) ? $options['output'] : 'script'; })->vertices(function (Collection $attribs, Matrix $mA, Graph $graph) { $vertices = []; foreach (range(0, $mA->rows() - 1) as $idx) { if (array_key_exists($idx, $attribs)) { $attribute = $attribs[$idx]; $vertices[$idx + 1] = $graph->createVertex($attribute->getName()); foreach ($attribute->getAttributes() as $key => $val) { $vertices[$idx + 1]->setAttribute($key, $val); } } else { $vertices[$idx + 1] = $graph->createVertex(); } } return $vertices; })->graphViz(function (Graph $graph, Matrix $mA, array $vertices, \Closure $edgeFunc, $output) { $comp = new Comparator(); $zero = TypeFactory::createInt(0); $rows = $mA->rows(); for ($row = 1; $row <= $rows; $row++) { for ($col = 1; $col <= $rows; $col++) { if ($comp->compare($zero, $mA->get($row, $col)) != 0) { $vertices[$row]->createEdgeTo($vertices[$col])->setWeight($edgeFunc($mA->get($row, $col)->get())); } } } return $output == 'script' ? (new GraphViz())->createScript($graph) : $graph; })->fyield('graphViz'); }
/** * Build and return the DIC * * @param StringType $definitionFile full path to dic definition file * * @throws \Exception * * @return ServiceContainer */ public static function buildDic(StringType $definitionFile) { if (!file_exists($definitionFile())) { throw new \Exception(self::ERR_NO_DIC); } //create dic /** @noinspection PhpUndefinedMethodInspection */ return FFor::create(['definitionFile' => $definitionFile])->dic(function () { return new ServiceContainer(); })->process(function ($dic, $definitionFile) { $fileLocator = new FileLocator(dirname($definitionFile())); $fileLoaders = [new XmlFileLoader($dic, $fileLocator), new YamlFileLoader($dic, $fileLocator)]; (new DelegatingLoader(new LoaderResolver($fileLoaders)))->load($definitionFile()); self::preCompile($dic); /** @noinspection PhpUndefinedMethodInspection */ $dic->compile(); self::postCompile($dic); })->fyield('dic'); }
return function () { return new PaintColour('red'); }; })->test(2, function () { return function () { return new PaintColour('black'); }; })->value()); } } class AssemblyLine { /** * @param Assembler $assembler * @return Car */ public function make(Assembler $assembler) { list($tyre, $colour) = $assembler->assemble()->release('tyre', 'colour'); return new Car("I made you a {$colour} car with {$tyre} tyres"); } } echo FFor::create()->assembler(function () { return Assembler::create(); })->tyre(function ($assembler) { return (new TyreMaker())->giveMe($assembler); })->colour(function ($assembler) { return (new Paintshop())->giveMe($assembler); })->assemblyLine(function ($assembler) { return (new AssemblyLine())->make($assembler); })->fyield('assemblyLine');
/** * Recursively build chart of account tree * * @param Node $tree * @param \DOMNode $node * @param Chart $chart * @param array $accountTypes */ protected function buildTree(Node $tree, \DOMNode $node, Chart $chart, array $accountTypes) { //create current node list($nominal, $type, $name) = FFor::create()->attributes(function () use($node) { return $node->attributes; })->nominal(function ($attributes) { return new Nominal($attributes->getNamedItem('nominal')->nodeValue); })->name(function ($attributes) { return new StringType($attributes->getNamedItem('name')->nodeValue); })->type(function ($attributes) use($accountTypes) { return new AccountType($accountTypes[strtoupper($attributes->getNamedItem('type')->nodeValue)]); })->fyield('nominal', 'type', 'name'); $tree->setValue(new Account($chart, $nominal, $type, $name)); //recurse through sub accounts foreach ($node->childNodes as $childNode) { if ($childNode instanceof \DOMElement) { $childTree = new Node(); $tree->addChild($childTree); $this->buildTree($childTree, $childNode, $chart, $accountTypes); } } }
/** * @param $sum * @param $rowArray * * @return mixed * * @throws MathMatrixException */ protected function getNextRow($sum, $rowArray) { return FFor::create(['sum' => $sum, 'rowArray' => $rowArray])->targetWeight(function ($sum) { return TypeFactory::createInt(mt_rand(1, $sum())); })->nextRow(function ($targetWeight, $rowArray) { foreach ($rowArray as $key => $weight) { if ($this->comp->lt($weight, $this->zero)) { throw new MathMatrixException('Negative weights not allowed'); } $targetWeight = $this->calc->sub($targetWeight, $weight); if ($this->comp->lte($targetWeight, $this->zero)) { return TypeFactory::createInt($key + 1); } } })->fyield('nextRow'); }