Skip to main content

Tuples

PHP does not provide out-of-the-box, like many artifacts in the Functional Programming realm, tuples. A tuple is an immutable linear structure of finite size capable of accommodating values of different types. Unlike immutable lists|collections, tuples cannot be appended to. The text to follow documents functions exclusive to the much improvised Tuple type.

fst#

fst()

fst :: (a, b) -> a

Extracts the first component of a tuple pair.

Since:

  • bingo-functional - v1.13.0

Argument(s)

None

Note: The function throws an exception in the event that a non-pair is encountered.

use Chemem\Bingo\Functional\Immutable\Tuple;
$tuple = Tuple::from([new stdClass, 'foo']);
$tuple->fst();

snd#

snd()

snd :: (a, b) -> b

Extracts the second component of a tuple pair.

Since:

  • bingo-functional - v1.13.0

Argument(s)

None

Note: The function throws an exception in the event that a non-pair is encountered.

use Chemem\Bingo\Functional\Immutable\Tuple;
$tuple = Tuple::from([new stdClass, 'foo']);
$tuple->snd();

swap#

swap()

swap :: (a, b) -> (b, a)

Swaps the components of a pair: inverts pair order.

Since:

  • bingo-functional - v1.13.0

Argument(s)

None

Note: The function throws an exception in the event that a non-pair is encountered.

use Chemem\Bingo\Functional\Immutable\Tuple;
$tuple = Tuple::from([new stdClass, 'foo']);
$tuple->swap();

get#

get(index)

get :: (a, b) -> Int -> a

Returns element associated with a specified index.

Since:

  • bingo-functional - v1.13.0

Argument(s)

  • index - The numerical key with a unique data value binding
use Chemem\Bingo\Functional\Immutable\Tuple;
$tuple = Tuple::from(range(4, 9));
$tuple->get(2);

head#

head()

head :: (a, b) -> a

Returns first element in the tuple.

Since:

  • bingo-functional - v1.13.0

Argument(s)

None

use Chemem\Bingo\Functional\Immutable\Tuple;
$tuple = Tuple::from(range(4, 9));
$tuple->head();

last#

last()

last :: (a, b) -> b

Returns last element in the tuple.

Since:

  • bingo-functional - v1.13.0

Argument(s)

None

use Chemem\Bingo\Functional\Immutable\Tuple;
$tuple = Tuple::from(range(4, 9));
$tuple->last();

tail#

tail()

tail :: (a, b, c) -> (b, c)

Returns a new tuple containing all initial tuple values but the first.

Since:

  • bingo-functional - v1.13.0

Argument(s)

None

use Chemem\Bingo\Functional\Immutable\Tuple;
$tuple = Tuple::from(range(4, 9));
$tuple->tail();