Phunkie provides a rich set of functional data types inspired by Scala and Haskell. These types are immutable and implement various type classes for functional operations.
Represents optional values - a value that may or may not exist:
// Some represents presence of a value
$some = Some(42);
$result = $some->map(fn($x) => $x * 2); // Some(84)
// None represents absence
$none = None();
$result = $none->map(fn($x) => $x * 2); // None
An immutable linked list:
// Create a list
$list = ImmList(1, 2, 3);
// Empty list
$empty = Nil();
// List operations
$head = $list->head; // 1
$tail = $list->tail; // ImmList(2, 3)
$mapped = $list->map(fn($x) => $x * 2); // ImmList(2, 4, 6)
Represents a single-argument function with composition capabilities:
$f = Function1(fn($x) => $x + 1);
$g = Function1(fn($x) => $x * 2);
// Function composition
$h = $f->andThen($g); // x + 1, then * 2
$result = $h(5); // 12
// Alternative composition
$h = $g->compose($f); // Same as above
An immutable key-value map:
// Create a map
$map = ImmMap(["hello" => "world"]);
// Access values
$value = $map->get("hello"); // Some("world")
$missing = $map->get("missing"); // None
// Transform values
$upper = $map->map(fn($v) => strtoupper($v));
An immutable set of unique values:
// Create a set
$set = ImmSet(1, 2, 2, 3); // Duplicates removed
// Set operations
$union = $set->union(ImmSet(3, 4)); // ImmSet(1, 2, 3, 4)
$intersect = $set->intersect(ImmSet(2, 3)); // ImmSet(2, 3)
Fixed-size heterogeneous collections:
// Pair (2-tuple)
$pair = Pair("hello", 42);
$first = $pair->_1; // "hello"
$second = $pair->_2; // 42
// Larger tuples
$tuple = Tuple("a", 1, true);
$values = [$tuple->_1, $tuple->_2, $tuple->_3]; // ["a", 1, true]
Represents the absence of a specific value (similar to void):
$unit = Unit();
echo $unit->toString(); // "()"
For handling validation results with error accumulation:
// Success case
$success = Success("Valid value");
// Failure case with error
$failure = Failure("Invalid input");
// Combine validations
$result = map2(
fn($name, $age) => ["name" => $name, "age" => $age],
validateName("Bob"),
validateAge(20)
);
Most Phunkie types implement the following type classes:
$result = match(true) {
$on(Some($x)) => "Got $x",
$on(None()) => "Nothing",
$on(ImmList($head, ...$tail)) => "List starting with $head",
};
Phunkie types maintain type information:
$option = Some(42);
echo $option->showType(); // "Option<Int>"
$list = ImmList(1, 2, 3);
echo $list->showType(); // "List<Int>"