public function __construct(){ $this->handlers = __::reduce( func_get_args(), function($memo, $fulfillmentHandler){ $memo[$fulfillmentHandler->handlesType()] = $fulfillmentHandler; return $memo; },array()); }
echo "No Talking Points Found. Exiting\n"; exit(0); } $points = __::reduce( $points_results->rows, function($memo, $row){ $memo[$row->value->handle] = $row->value->points; return $memo; },array() ); /* ======================= Configure SubscriptionFulfilment ----------------------- */ $fulfillment = new SubscriptionFulfillment(new EmailSubscriptionFulfillment()); /* ======================== Main Distribution processing ------------------------ */ __::each($subscriptions, function($subscription) use($points, $fulfillment){ if(!$subscription) continue; $pointsForSubscriber = __::reduce( $subscription->handles, function($memo, $handle) use ($points){ if(empty($points[$handle])) return $memo; return array_merge($memo, $points[$handle]); }, array()); $fulfillment->handle($subscription, $pointsForSubscriber); });
public function testReduce() { // from js $sum = __::reduce(array(1, 2, 3), function ($sum, $num) { return $sum + $num; }, 0); $this->assertEquals(6, $sum, 'can sum up an array'); $context = array('multiplier' => 3); $sum = __::reduce(array(1, 2, 3), function ($sum, $num) use($context) { return $sum + $num * $context['multiplier']; }, 0); $this->assertEquals(18, $sum, 'can reduce with a context object'); $sum = __::reduce(array(1, 2, 3), function ($sum, $num) { return $sum + $num; }, 0); $this->assertEquals(6, $sum, 'default initial value'); $ifnull = null; try { __::reduce(null, function () { }); } catch (Exception $e) { $ifnull = $e; } $this->assertFalse($ifnull === null, 'handles a null (without initial value) properly'); $this->assertEquals(138, __::reduce(null, function () { }, 138), 'handles a null (with initial value) properly'); $sum = __(array(1, 2, 3))->reduce(function ($sum, $num) { return $sum + $num; }); $this->assertEquals(6, $sum, 'OO-style reduce'); $sum = __::inject(array(1, 2, 3), function ($sum, $num) { return $sum + $num; }, 0); $this->assertEquals(6, $sum, 'aliased as "inject"'); $sum = __::foldl(array(1, 2, 3), function ($sum, $num) { return $sum + $num; }, 0); $this->assertEquals(6, $sum, 'aliased as "foldl"'); // docs $this->assertEquals(6, __::reduce(array(1, 2, 3), function ($memo, $num) { return $memo + $num; }, 0)); }