Giter Site home page Giter Site logo

chainer's Introduction

Chainer

Chainer is a small PHP library that makes functional style programming easier, as well as including a set of functions for data analysis.

Features

  • Implements itself as an array (allows for $chain0)
  • Implements itself as an Iterator (allows for foreach)
  • Supports chaining any class that implements Iterator
  • Supports PHP5+ (Recommended 5.3+)
  • Side-effect free, each call returns a new instance.
  • Supports static calling syntax

Examples


<?php

include 'chain.php';

/*********************
 * 3 Line Demo
 ********************/

$data = array(0, 1, 2, 3, 4);
Chain($data)
	->map(function($x) { return $x * $x; })
	->reduce(function($sum, $x) { return $sum + $x; }, 0);
// Squares each function and then sums it (in a verbose way)

$result = array(); // Array of database rows



/*********************
 * Real World Demo
 ********************/

// Normalize data (run on all rows)
$results = Chain($result)->map(function($row) {
	$row->date_created = strtotime($row->date_created)
});

// Select all featured ($results is left un-touched)
$featured = $results->select(function($x) {
	return $x->featured;
});

// Get all id attributes
$showing = $results->pluck('id');



/*********************
 * Static Calls
 ********************/
// Used if you only want to use one method
// all chainable functions available, first
// paramater is what the contuctor normally
// takes

$data = array('one', 'two', 'three');

$static = Chain::map($data, 'strtoupper');
$method = Chain($data)->map('strtoupper')->toArray();
// toArary is not nessesarily needed (only in case of an array cast)

?>

chainer's People

Contributors

stuartloxton avatar

Stargazers

André Philip avatar Mark Perkins avatar Mohammed Irfan avatar Dirk Gadsden avatar  avatar

Watchers

André Philip avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.