Numbers\isFactorOf()

predicate returns Closure returns bool pure
int → (int → bool)
At a glance — Bind a target integer; the returned predicate is true when the passed value is a factor of the target. Zero always returns false.

Creates a predicate Closure that returns true when the passed value is a whole factor of the pre-defined integer — i.e. when the fixed value divides cleanly by the passed one. Zero is rejected and always returns false.

/**
  * @param int $factor The fixed number to test factors of.
  * @return Closure(int):bool
  */
Numbers\isFactorOf(int $factor): Closure

Returned Closure

When Numbers\isFactorOf() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param int $value
  * @return bool  True when ($factor % $value === 0) and $value !== 0.
  */
$function (int $value): bool

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

// Create a predicate that asks "is X a factor of 12?".
$factorOf12 = Numbers\isFactorOf(12);

// Called as a function.
var_dump($factorOf12(3)); // true   — 12 / 3 has no remainder
var_dump($factorOf12(4)); // true
var_dump($factorOf12(5)); // false
var_dump($factorOf12(0)); // false  — zero is never a factor here

// Used in a higher order function.
$divisorsOf12 = array_filter([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], $factorOf12);
print_r($divisorsOf12); // [1, 2, 3, 4, 6, 12]

Curried

This can be called inline using currying.

var_dump(Numbers\isFactorOf(20)(5)); // true

Inlined with Higher Order Function

If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.

$factors = array_filter(range(1, 10), Numbers\isFactorOf(20));
print_r($factors); // [1, 2, 4, 5, 10]

Details

Numbers Functions

Releated Number predicate Functions