|
@@ -33,7 +33,7 @@ class F
|
|
|
* In case you're tired of writing call_user_func_array
|
|
|
* May be removed in the future, because it's quite meh
|
|
|
*/
|
|
|
- public static function apply(callable $func, $args)
|
|
|
+ public static function apply(callable $func, $args=[])
|
|
|
{
|
|
|
return call_user_func_array($func, $args);
|
|
|
}
|
|
@@ -95,6 +95,13 @@ class F
|
|
|
return $param;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a function that returns the given $property of an object
|
|
|
+ *
|
|
|
+ * @param string $property Name
|
|
|
+ *
|
|
|
+ * @return \Closure Function that returns $property from argument
|
|
|
+ */
|
|
|
public static function pick($property)
|
|
|
{
|
|
|
return function($obj) use ($property) {
|
|
@@ -104,6 +111,19 @@ class F
|
|
|
};
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Creates a function that calls a function depending on a condition
|
|
|
+ *
|
|
|
+ * When called, passes its argument to $test and calls either $onTrue or
|
|
|
+ * $onFalse depending on the return value of $test. $onFalse can be null
|
|
|
+ * and the function will by default simply return the argument.
|
|
|
+ *
|
|
|
+ * @param callable $test Evaluates its argument and returns bool
|
|
|
+ * @param callable $onTrue Called with argument in the true case
|
|
|
+ * @param callable $onFalse Called with argument in the false case
|
|
|
+ *
|
|
|
+ * @return \Closure takes one argument
|
|
|
+ */
|
|
|
public static function conditional($test, $onTrue, $onFalse = null)
|
|
|
{
|
|
|
return function($subject) use ($test, $onTrue, $onFalse)
|