Experiments in Elegant Functional Programming in PHP.
Read Higher Order Programming for an introduction.
- LISPy Love for Lists!
- Higher order Happiness!
- Posh Partial Application!
- Gloriously Global!
<?php
atom(@jim, @jenny, @jack, @jules);
$accounts = l(
pair(jim, 100),
pair(jenny, 30),
pair(jack, -50),
pair(jules, -43)
);
$negate = multiply(-1, __); // __ is a partial application placeholder
$isnegative = lt(__, 0); // lt is short for "less than"
$balance = tail; // Alias tail(), which returns the second item in a pair
$totalOutstanding = pipe( // think *nix pipes and filters
map($balance, __),
filter($isnegative, __),
reduce(add, __, 0),
$negate
);
assert(isequal(
$totalOutstanding($accounts),
93
));
Find more in the examples
folder, or read the tests.
What is this all about?
Read the The Little Schemer. Or learn LISP (or Clojure or some variant). There's also some Erlang-ish ideas in there.
Wait but why?
Why did I try to build a radio when I was ten? Why did I take apart my father's computer when I was eleven?
But but but... Global functions? Global constants?
You're very observant! Don't think of it as polluting the global namespace. Think of it as cleaning up the global namespace!
Are there unit tests?
Yes, check out the /topics
directory. These are omikron topics. To run them, use this command:
bin/omikron topics
Can you backport it to older PHP versions?
Boring, next question please.
Wouldn't this all be a lot easier and more elegant using HHVM/Hack?
I know right?
Should I use it in production?
Yes! Be a pioneer, be an early adopter, be an innovator! Show your boss you can take risks on company time! You are a strong individual who makes your own decisions, even in the face of constant change and breakage! You are a legend!
Really?
I don't think you're ready for this curry cuz my codez is too Lamdalicious for ya babe.
I want to contribute!