GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - morrisonlevi/Algorithm: Composable algorithms for PHP 7

BranchesTags
Open more actions menu

Latest commit

 

History

59 Commits
59 Commits

Folders and files

NameName
Last commit message
Last commit date
docs
docs
 
 
src
src
 
 
test
test
 
 
.codeclimate.yml
.codeclimate.yml
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
config.mk
config.mk
 
 
make-loader.php
make-loader.php
 
 
make-phar.php
make-phar.php
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

Basic Algorithms for PHP

Code Climate Test Coverage

PHP's built-in functions such as array_map, array_filter and array_reduce have a few issues:

  • They only work with arrays
  • They are eager instead of lazy
  • They are cumbersome to compose

This repository provides definitions for common algorithms such as map, filter, and reduce with certain characteristics:

  • They work with any type that can be used in a foreach loop
  • They are lazy
  • They are not (too) cumbersome to compose

Building

PHP does not have function autoloading at the time of this writing. Since this project is mostly functions it uses a makefile to build load.php which will include all of the functions for use. You can also build a phar or run the unit tests:

  • make (or make load.php): builds load.php
  • make phar: builds morrisonlevi_algorithm.phar
  • make check: runs the phpunit test suite

There is a script registered in the composer.json that will build load.php if the composer autoloader gets built.

Examples

This example does a basic map. Note that the function that does the mapping comes first and the input data comes second:

<?php

namespace morrisonlevi\Algorithm;

require __DIR__ . '/load.php';

$mul2 = function ($value) {
    return $value * 2;
};

$result = map($mul2)([1,2,3]);

var_export(iterator_to_array($result));
/*
array (
  0 => 2,
  1 => 4,
  2 => 6,
)
*/

This example chains a filter, map and sum together:

<?php

namespace morrisonlevi\Algorithm;

require __DIR__ . '/load.php';

$odd = function($value) {
   return $value % 2 > 0;
};

$mul2 = function($value) {
    return $value * 2;
};

$algorithm = chain(
    filter($odd),
    map($mul2),
    sum()
);

var_dump($algorithm([1,2,3])); // int(8)

About

Composable algorithms for PHP 7

Resources

Readme
Activity

Stars

46 stars

Watchers

4 watching

Forks

2 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.