Ejemplo n.º 1
0
 public function testFnNestedMap()
 {
     $fnMap = function ($v) {
         $v['mul'] = strlen($v['name']) * $v['count'];
         return $v;
     };
     $fnMap2 = function ($v) {
         $v['mul'] = -strlen($v['name']) * $v['count'];
         return $v;
     };
     $fruitBasket = array(TestData::$fruit, TestData::$fruit, TestData::$fruit, TestData::$fruit, TestData::$fruit);
     $n1 = Sequence::make($fruitBasket)->map(FnGen::fnNestedMap($fnMap))->to_a();
     $n2 = Sequence::make($fruitBasket)->map(FnSequence::make()->map($fnMap)->to_a())->to_a();
     $n3 = Sequence::make($fruitBasket)->map(function ($values) use($fnMap) {
         return Sequence::make($values)->map($fnMap)->to_a();
     })->to_a();
     $x1 = Sequence::make($fruitBasket)->map(FnGen::fnNestedMap($fnMap2))->to_a();
     $this->assertEquals($n3, $n1);
     $this->assertEquals($n3, $n2);
     $this->assertNotEquals($n1, $x1);
 }
Ejemplo n.º 2
0
/**
 * Returns a function that applies a function to a nested array and returns the results.
 *
 * @param $fn
 * @return Closure
 */
function fnNestedMap($fn)
{
    return FnSequence::make()->map($fn)->to_a();
}
Ejemplo n.º 3
0
 /**
  * Inject another FnSequence
  *
  * @param FnSequence $fnSequence
  * @return FnSequence
  */
 public function inject(FnSequence $fnSequence)
 {
     return new FnSequence($this->append($fnSequence->to_fn()));
 }