Functional programming in PHP is pretty ugly, but at least it's possible.
composer require cullylarson/phugly
$ composer install
$ composer run tests
A curried functional programming library for PHP.
I find myself very often calling call(compose(...))
. It would be nice to have a function that shortcuts this (e.g. ccompose
or cc
).
So, if value === null
, just return the value (null
), otherwise do something. It's sort of a poor-man's catamorphism, just assuming the the Nothing
case is F\Id
. It essentially assumes a Monad where null
is Nothing
and all other values are Just
.
I'm not sure if it's a great idea to continue to avoid explicit Monads in this library, and keep making exceptions/work-arounds. Though it does make using the library really easy.
Would it be more useful to have another function for when the value is null, rather than assuming F\id
? Maybe add a cata
function for that?
The function will be something like:
// 'nn' means 'not null'. good name?
function nn(...$args) {
$nn = curry(function($f, $x) {
return $x === null
? $x
: $f($x);
});
return call_user_func_array($nn, $args);
}
Now it assumes output as HTML. Would be nice if it formatted output better if running on the CLI.
The function signature is setAt(idx, val, x)
. If idx
is empty, val
will be returned.
Expected: x
should be returned, without any changes.
Gets a random number of values from an array. Potential implementation:
$getRandom = curry(function($num, $xs) {
if(empty($xs) || !is_array($xs) || count($xs) <= $num) return $xs;
$results = [];
for($i = 0; $i < $num; $i++) {
$idx = rand(0, count($xs) - 1);
$results []= array_splice($xs, $idx, 1)[0];
}
return $results;
});
Basically just call objProp
if it's an object. Not sure if this is good or bad. Maybe you should know what you're working with?
It's mapping at an index, so mapAt
is a more meaningful name.
At some point I'd like to refactor to use lenses throughout the library, so this "rename" might not be necessary. Or, mapAt
could be the lens version? I don't really like the idea of having a bunch of old functions around. Maybe the lens version of the library could be a new major version and we can scrap the old function names if necessary.
Maybe call it rreduce
, thought that might be hard to site-read and could easily be misapplied. Maybe revReduce
or reverseReduce
?
E.g. objProp(['firstKey', 'secondKey'])
. Now it only takes a string key, so cannot access nested objects. Ideally would replace objProp
by expanding getAt
to work with objects (c.f. #5). That way you wouldn't have to worry if an array is nested in an object nested in an array, etc.
Document the rationale of the library (why it is, how it is). Document all of the functions. Include examples.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.