Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion test/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,16 @@ public function testAfter() {
$func();
$this->assertEquals('x', $str);
}
}

function testPartial() {
// partial once
$f = function( $x, $y ) { return $x + $y; };
$f2 = __::partial( $f, 2 );
$this->assertEquals( 5, $f2(3) );

// partial on a partial
$f3 = __::partial( $f2, 4 );
$this->assertEquals( 6, $f3() );
}

}
11 changes: 11 additions & 0 deletions underscore.php
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,17 @@ public function after($count=null, $function=null) {
};
return self::_wrap(($count) ? $func : $func());
}


// creates a partially applied version of the function
public function partial() {
$origArgs = func_get_args();
$f = array_shift( $origArgs );
return function() use ( $f, $origArgs ) {
$allArgs = array_merge( $origArgs, func_get_args() );
return call_user_func_array( $f, $allArgs );
};
}


// Singleton
Expand Down