• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Immutable and persistent collections for PHP

Medusa

Build Status

Immutable and persistent collections for PHP.

Life would be a lot simpler if we had immutable data structures. Code would be easier to understand, easy to test and free of side-effects. Being immutable is not all, these data structures must be efficient. By making them persistent, collections reuse internal structure to minimize the number of operations needed to represent altered versions of an instance of a collection.

Installation

To install this library, run the command below and you will get the latest version

composer require keyvanakbary/medusa

Usage

Persistent Stack

$s = Medusa\Stack\PersistentStack::createEmpty();

$s1 = $s->push(1);
$s2 = $s1->pop();
echo $s1->peek();//1
echo $s2->peek();//Runtime exception

Complexity

operation big-O
push O(1)
peek O(1)
pop O(1)
isEmpty O(1)
count O(1)

Persistent Queue

$q = Medusa\Queue\PersistentQueue::createEmpty();

$q1 = $q->enqueue(1);
$q2 = $q1->dequeue();
echo $q1->peek();//1
echo $q2->peek();//Runtime exception

Complexity

operation big-O
isEmpty O(1)
peek O(1)
enqueue O(1)
dequeue O(1) in average, O(n) in some cases
count O(1)

Persistent AVL Tree

$t = Medusa\Tree\PersistentAvlTree::createEmpty();

$t1 = $t->add(1, 'one');
$t2 = $t1->remove(1);
echo $t1->search(1)->value();//one
echo $t2->lookup(1);//Runtime exception

Complexity

operation big-O
isEmpty O(1)
value O(1)
key O(1)
add O(1)
search O(log(n))
contains O(log(n))
height O(1)
lookup O(log(n))

Persistent Red-Black Tree

$t = Medusa\Tree\PersistentRedBlackTree::createEmpty();

$t1 = $t->add(1, 'one');
$t2 = $t1->remove(1);
echo $t1->search(1)->value();//one
echo $t2->lookup(1);//Runtime exception

Complexity

operation big-O
isEmpty O(1)
value O(1)
key O(1)
add O(1)
search O(log(n))
contains O(log(n))
height O(1)
lookup O(log(n))
min O(log(n))
removeMin O(log(n))

More Repositories

1

learning-notes

Notes on books I read, talks I watch, articles I study, and papers I love
SCSS
5,132
star
2

cqrs-documents

E-book of Greg Young's CQRS Documents
Ruby
186
star
3

slugifier

Just a simple, clean and pure functional implementation for creating slugs
PHP
67
star
4

princeton-algorithms

Princeton university algorithms course solutions
Java
34
star
5

mimic

Dead simple functional library for object prototyping, data hydration and data exposition
PHP
21
star
6

marko

Minimalistic markdown editor
Clojure
18
star
7

eter

Lightweight collections for JavaScript
TypeScript
16
star
8

memo

A CQRS and Event Sourcing blogging playground to explore Axon Framework
Java
14
star
9

greensync

Tool for syncing Greenhouse Harvest API into MySQL
Elixir
7
star
10

geshi-twig

Twig syntax recognition for Geshi
PHP
7
star
11

victorops

VictorOps client
CoffeeScript
6
star
12

reagent-content-editable

A Reagent content editable component
Clojure
5
star
13

keyvanakbary.com

Personal blog
PHP
4
star
14

php-tuenti

Unofficial Tuenti API for PHP
PHP
3
star
15

php-boilerplate

Boilerplate code for PHP projects
PHP
3
star
16

comparison

Java
2
star
17

clj-tuenti

Unofficial Tuenti API for Clojure
Clojure
2
star
18

slides

Presentations repository
Dockerfile
1
star
19

gilded-rose-kata

Gilded Rose Kata with CoffeeScript, Grunt, Mocha and Expect
CoffeeScript
1
star
20

kiwwito.com

Code snippets and more
CSS
1
star
21

dni-nie-validator

Spanish DNI/NIE PHP validator
PHP
1
star
22

clubsync

Tool for syncing Clubhouse API into MySQL
Elixir
1
star
23

typhoon

Minimal typing application written in ClojureScript
Clojure
1
star
24

coffeescript-boilerplate

Boilerplate code for CoffeeScript projects
CoffeeScript
1
star
25

syncodoc

Go
1
star
26

fizz-buzz

Groovy implementation for Fizz Buzz game
Groovy
1
star
27

clj-sorting-algorithms

Sorting algorithms playground
Clojure
1
star