Skip to content

Instantly share code, notes, and snippets.

@mtrojanowski
Created February 11, 2014 10:54
Show Gist options
  • Save mtrojanowski/8932814 to your computer and use it in GitHub Desktop.
Save mtrojanowski/8932814 to your computer and use it in GitHub Desktop.
<?php
/*
* Create an operation which will behave as the following
* set of functions (without using "if" statements):
*
* f(x) = 0, for x < 0;
* f(x) = x, for x >= 0 and x < 1;
* f(x) = 1, for x >= 1 and x < 2;
* f(x) = -x + 3, for x >=2 and x < 3;
* f(x) = 0, for x >= 3;
*
*/
function f($x)
{
$constZero = !($x < 0 || $x >= 3);
$ascendingFunction = ($x >= 0 && $x < 1);
$constValue = ($x >= 1 && $x < 2);
$descendingFunction = ($x >= 2 && $x < 3);
return $constZero * ($ascendingFunction*$x + $constValue*1 + $descendingFunction*(-$x + 3));
}
@kfurmann
Copy link

sweeeet! Now it seems so trivial...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment