Skip to content

Instantly share code, notes, and snippets.

@mrbobbybryant
Last active January 3, 2016 06:21
Show Gist options
  • Save mrbobbybryant/e7a2eebeb354f9da2db2 to your computer and use it in GitHub Desktop.
Save mrbobbybryant/e7a2eebeb354f9da2db2 to your computer and use it in GitHub Desktop.
Just some functional programming ideas
<?php
//Closure Example
$add = function($x) {
return function($y) use ($x) {
return $x + $y;
};
};
$addTen = $add(10);
var_dump( $result = $addTen(2) );
//A functional programming example. A Map example that uses closures.
//This allows us to create any number of array processes by composing a custom function with our map function.
function map( $fn ) {
return function( $arr ) use ( $fn ) {
return array_map( $fn, $arr );
};
}
function sanitize_text_field( $string ) {
return filter_var( $string, FILTER_SANITIZE_STRING );
}
$sanitize_array = map( 'sanitize_text_field' );
var_dump( $sanitize_array( array( '<h1>title 1</h1>', '<h1>title 2</h1>', '<h1>title 3</h1>' ) ) );
function either( $default, $value ) {
if ( ! empty( $value ) ) {
return $value;
}
return $default;
}
$test = either('needs something', array());
var_dump($test);
//curried either
function either( $default ) {
return function($value) use( $default ) {
if ( ! empty( $value ) ) {
return $value;
}
return $default;
};
}
function return_error( $error_message ) {
return new Exception( $error_message );
}
function return_false() {
return false;
}
$maybe_false = either( return_false() );
$maybe_error = either ( return_error('something is missing') );
$test_false = $maybe_false(array());
$test_error = $maybe_error(array());
var_dump($test_false);
var_dump($test_error);
function error_check( $error_message, $value ) {
if ( ! empty( $value ) ) {
return $value;
}
return new Exception( $error_message );
}
function empty_check( $value ) {
if ( ! empty( $value ) ) {
return $value;
}
return false;
}
// Filter Example
//None Curried Either
function either( $default, $value ) {
if ( ! empty( $value ) ) {
return $value;
}
return $default;
}
//Same error function as above.
function return_error( $error_message ) {
return new Exception( $error_message );
}
$users = array( 'John', "Bobby", 'Alex' );
function filter( $fn ) {
return function( $arr ) use ( $fn ) {
return array_filter( $arr, $fn );
};
}
function get( $value ) {
return function( $el ) use ( $value ) {
return $el === $value;
};
}
function get_user( $username, $users ) {
$find = filter( get( $username ) );
return either( return_error( 'Users not found' ), $find( $users ) );
}
var_dump( get_user( 'Bobby', $users ) );
class Wrap {
protected $value;
public function __construct( $value ) {
$this->value = $value;
}
public static function of( $x ) {
return new Wrap( $x );
}
public function map($func) {
return static::of( call_user_func( $func, $this->value ) );
}
}
$test = wrap::of('small world');
var_dump($test);
var_dump($test->map('ucwords'));
var_dump($test->map('strtoupper'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment