2017 © Pedro Peláez
 

library pattern-matching

Pattern matching for PHP with automatic destructuring.

image

functional-php/pattern-matching

Pattern matching for PHP with automatic destructuring.

  • Saturday, January 27, 2018
  • by krtek
  • Repository
  • 3 Watchers
  • 51 Stars
  • 1,643 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 5 Open issues
  • 5 Versions
  • 51 % Grown

The README.md

Pattern Matching

pattern-matching CI StyleCI Code Coverage Average time to resolve an issue Percentage of issues still open Chat on Gitter, (*1)

Pattern matching is the process of checking a series of token against a pattern. It is different from pattern recognition as the match needs to be exact. The process does not only match as a switch statement does, it also assigns the value a bit like the list construct in PHP, a process called destructuring., (*2)

Most functional languages implement it as a core feature. Here is are some small examples in Haskell:, (*3)


fact :: (Integral a) => a -> a fact 0 = 1 fact n = n * fact (n-1) head :: [a] -> a head xs = case xs of [] -> error "empty list" (x:_) -> x firstThree :: [a] -> (a, a, a) firstThree (x:y:z:_) = (x, y, z) firstThree _ = error "need at least 3 elements"

If you want to read more about the topic, you can head over to Wikipedia : Pattern matching, (*4)

Installation

composer require functional-php/pattern-matching

Basic Usage

As we cannot extend the syntax of PHP, the choice was made to use a syntax based on arrays. The key representes the pattern and the value is the function to call with the value or a constant if you want to do nothing with it., (*5)

Let's see how we could implement our 3 Haskell examples:, (*6)


use FunctionalPHP\PatternMatching as m; $fact = m\func([ '0' => 1, 'n' => function($n) use(&$fact) { return $n * $fact($n - 1); } ]); $head = m\func([ '(x:_)' => function($x) { return $x; }, '_' => function() { throw new RuntimeException('empty list'); } ]); $firstThree= m\func([ '(x:y:z:_)' => function($x, $y, $z) { return [$x, $y, $z]; }, '_' => function() { throw new RuntimeException('need at least 3 elements'); } ]);

You can also use the match function if you want to have a beefed up version of the switch statement or if you don't like anonymous functions:, (*7)


use FunctionalPHP\PatternMatching as m; function factorial($n) { return m\pmatch([ '0' => 1, 'n' => function($n) use(&$fact) { return $n * factorial($n - 1); } ], $n); } echo m\pmatch([ '"toto"' => 'first', '[a, [b, c], d]' => 'second', '[a, _, (x:xs), c]' => 'third', '_' => 'default', ], [1, 2, ['a', 'b'], true]); // third

If you are just interested in destructuring your values, there is also a helper for that:, (*8)


use FunctionalPHP\PatternMatching as m; print_r(m\extract([1, 2, ['a', 'b'], true], '[a, _, (x:xs), c]')); // Array ( // [a] => 1 // [x] => a // [xs] => Array ( [0] => b ) // [c] => 1 // )

Patterns

Here is a quick recap of the available patterns:, (*9)

Name Format Example
Constant Any scalar value (int, float, string, boolean) 1.0, 42, "test"
Variable identifier a, name, anything
Array [<pattern>, ..., <pattern>] [], [a], [a, b, c]
Cons (identifier:list-identifier) (x:xs), (x:y:z:xs)
Wildcard _ _
As identifier@(<pattern>) all@(x:xs)

Testing

You can run the test suite for the library using:, (*10)

composer test

A test report will be available in the reports directory., (*11)

Contributing

Any contribution welcome :, (*12)

  • Ideas
  • Pull requests
  • Issues

The Versions

27/01 2018

dev-master

9999999-dev

Pattern matching for PHP with automatic destructuring.

  Sources   Download

BSD BSD-3-Clause

The Requires

  • php >=5.6.0

 

The Development Requires

by Gilles Crettenand

pattern functional pattern matching destructuring

26/10 2016

0.3.1

0.3.1.0

Pattern matching for PHP with automatic destructuring.

  Sources   Download

BSD

The Requires

  • php >=5.6.0

 

The Development Requires

by Gilles Crettenand

pattern functional pattern matching destructuring

26/10 2016

0.3.0

0.3.0.0

Pattern matching for PHP with automatic destructuring.

  Sources   Download

BSD

The Requires

  • php >=5.6.0

 

The Development Requires

by Gilles Crettenand

pattern functional pattern matching destructuring

25/10 2016

0.2.0

0.2.0.0

Pattern matching for PHP with automatic destructuring.

  Sources   Download

BSD

The Requires

  • php >=5.6.0

 

The Development Requires

by Gilles Crettenand

pattern functional pattern matching destructuring

24/10 2016

0.1.0

0.1.0.0

Pattern matching for PHP with automatic destructuring.

  Sources   Download

BSD

The Requires

  • php >=5.6.0

 

The Development Requires

by Gilles Crettenand

pattern functional pattern matching destructuring