GithubHelp home page GithubHelp logo

elephox-dev / collection Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 401 KB

[READONLY] This is the Collection module of the Elephox framework.

Home Page: https://elephox.dev

License: MIT License

PHP 100.00%

collection's Introduction

Elephox Collection Module

This library (or rather module) was inspired by C#s LINQ library.

However, it is not a full-featured LINQ library. It is only a small subset of the functionality since user-land PHP code cannot fully support all the syntactic sugar. For example, the main feature of LINQ, SQL-like syntax directly in source, is not supported since it would require you to compile/transpile your code, which is not in the scope of this library.

The main idea, however, is to provide a way to iterate over a collection of objects in a more natural/handy way like you can do with IEnumerables in C#.

Examples

<?php
declare(strict_types=1);

use Elephox\Collection\Enumerable;

$data = [5, 2, 1, 4, 3];
$arr = Enumerable::from($data);

$arr->orderBy(fn (int $item) => $item)
    ->select(function (int $item) {
      echo $item;
    });

// output: 12345


$arr->where(fn (int $item) => $item % 2 === 0)
    ->select(function (int $item) {
        echo $item;
    });

// output: 24

echo $arr->aggregate(fn (int $acc, int $item) => $acc + $item, 0);

// output: 15

Differences to C# IEnumerable

  • PHP doesn't support generics. For now, only static analyzers like Psalm can provide full type safety when working with generic collections.
  • No extension methods from the System.Data namespace are implemented (CopyToDataTable)
  • Cast wouldn't make sense in a dynamically typed language, so it is not implemented either. You can use Enumerable::select to change the type of the values.
  • GroupJoin is not implemented (yet?)
  • Methods ending with OrDefault always has null as the default value. You can, of course, pass your own default value.
  • PHP has no default comparer for types, so we provide a DefaultEqualityComparer class that implements some methods to compare two values. Depending on if an order is required, DefaultEqualityComparer::same or DefaultEqualityComparer::compare is used if you don't provide a comparer function yourself.
  • LongCount is not implemented since PHP only has one integer type
  • OfType is not implemented since PHP doesn't have generics
  • ToHashSet, ToDictionary and ToLookup are not implemented. Instead, you can convert an Enumerable to native types via toList and toArray (whereas toArray keeps the keys or allows you to pass a key selector function)
  • AsParallel and AsQueryable are not implemented
  • None of the System.Xml extension methods are implemented (Ancestors, Descendants, Elements, etc.)
  • No read only or immutable interfaces or methods to get them exist (yet?)
  • TryGetNonEnumeratedCount is not implemented

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.