Skip to content

Commit

Permalink
Wraps function declaration to avoid redeclare error
Browse files Browse the repository at this point in the history
  • Loading branch information
pitchart committed Feb 18, 2020
1 parent 71e5816 commit d159b83
Showing 1 changed file with 81 additions and 76 deletions.
157 changes: 81 additions & 76 deletions src/transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,90 @@

namespace Pitchart\Transformer;

/**
* Prepares a transformation for a collection of items
*
* @param array|\Traversable $iterable
* @param Composition $composition
* @param Termination $termination
* @param mixed $initial
*
* @return Transformer
*/
function transform($iterable, Composition $composition = null, Termination $termination = null, $initial = null)
{
return (new Transformer($iterable, $composition, $termination, $initial));
}
if (!function_exists(__NAMESPACE__.'\transform')) {

/**
* Alias for transform()
*
* @see transform()
* @param mixed $iterable
* @param null|mixed $initial
*/
function from($iterable, Composition $composition = null, Termination $termination = null, $initial = null)
{
return transform($iterable, $composition, $termination, $initial);
}
/**
* Prepares a transformation for a collection of items
*
* @param array|\Traversable $iterable
* @param Composition $composition
* @param Termination $termination
* @param mixed $initial
*
* @return Transformer
*/
function transform($iterable, Composition $composition = null, Termination $termination = null, $initial = null)
{
return (new Transformer($iterable, $composition, $termination, $initial));
}

/**
* Creates a composition of functions
*
* @param \callable[] ...$callbacks
*
* @return Composition
*/
function compose(callable ...$callbacks)
{
return new Composition(...$callbacks);
}
/**
* Alias for transform()
*
* @see transform()
*
* @param mixed $iterable
* @param null|mixed $initial
*/
function from($iterable, Composition $composition = null, Termination $termination = null, $initial = null)
{
return transform($iterable, $composition, $termination, $initial);
}

/**
* @param mixed $value
*
* @return mixed
*/
function identity($value)
{
return $value;
}
/**
* Creates a composition of functions
*
* @param \callable[] ...$callbacks
*
* @return Composition
*/
function compose(callable ...$callbacks)
{
return new Composition(...$callbacks);
}

/**
* Transforms a function into a pure function
*
* @param callable $function
* @param array ...$arguments
*
* @return mixed
*/
function curry(callable $function, ...$arguments)
{
return (new Curry($function))(...$arguments);
}
/**
* @param mixed $value
*
* @return mixed
*/
function identity($value)
{
return $value;
}

/**
* Creates a comparison function for a callable criterion
* The created function can be used with usort(), uasot() and uksort()
*
* @param callable $callback
*
* @return \Closure
*/
function comparator(callable $callback)
{
return function ($first, $second) use ($callback) {
$first = ($callback)($first);
$second = ($callback)($second);
if ($first == $second) {
return 0;
}
/**
* Transforms a function into a pure function
*
* @param callable $function
* @param array ...$arguments
*
* @return mixed
*/
function curry(callable $function, ...$arguments)
{
return (new Curry($function))(...$arguments);
}

/**
* Creates a comparison function for a callable criterion
* The created function can be used with usort(), uasot() and uksort()
*
* @param callable $callback
*
* @return \Closure
*/
function comparator(callable $callback)
{
return function ($first, $second) use ($callback) {
$first = ($callback)($first);
$second = ($callback)($second);
if ($first == $second) {
return 0;
}

return $first < $second ? -1 : 1;
};
}

return $first < $second ? -1 : 1;
};
}
}

0 comments on commit d159b83

Please sign in to comment.