Skip to content

Commit 190e71b

Browse files
committed
Initial commit
0 parents  commit 190e71b

File tree

10 files changed

+436
-0
lines changed

10 files changed

+436
-0
lines changed

.codeclimate.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
engines:
3+
duplication:
4+
enabled: true
5+
config:
6+
languages:
7+
- php
8+
phpcodesniffer:
9+
enabled: true
10+
config:
11+
ignore_warnings: true
12+
ratings:
13+
paths:
14+
- "**.php"
15+
exclude_paths:
16+
- tests/
17+
- vendor/

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# for common files/dirs (eg.: .idea) create a global gitignore file (eg. ~/.gitignore_global) and run
2+
# git config --global core.excludesfile ~/.gitignore_global
3+
composer.lock
4+
phpunit.xml
5+
vendor/

.travis.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
sudo: false
2+
3+
language: php
4+
5+
php:
6+
- 7.1
7+
- 7.2
8+
9+
before_install:
10+
- travis_retry composer self-update
11+
12+
install:
13+
- travis_retry composer install --no-interaction --prefer-dist
14+
15+
before_script:
16+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
17+
- chmod +x ./cc-test-reporter
18+
- ./cc-test-reporter before-build
19+
- echo 'date.timezone = "Europe/London"' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
20+
21+
script:
22+
- vendor/bin/phpcs --standard=PSR2 --warning-severity=0 src
23+
- vendor/bin/phpstan analyse -l 7 src
24+
- vendor/bin/phpunit --coverage-clover=clover.xml
25+
26+
after_script:
27+
- bash <(curl -s https://codecov.io/bash)
28+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Zsolt Szende
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

changelog.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6+
7+
## [1.0.0] - 2018-04-29
8+
### Added
9+
* Initial release

composer.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "pwm/treegami",
3+
"description": "A tree implementation that can be unfolded, mapped and folded.",
4+
"type": "library",
5+
"keywords": ["tree-folding", "catamorphism", "anamorphism"],
6+
"homepage": "https://github.com/pwm/treegami",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Zsolt Szende",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=7.1.0"
16+
},
17+
"require-dev": {
18+
"squizlabs/php_codesniffer": "^3.0",
19+
"phpstan/phpstan": "^0.7.0",
20+
"phpunit/phpunit": "^6.1"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"Pwm\\Treegami\\": "src/"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-4": {
29+
"Pwm\\Treegami\\": "tests/unit/"
30+
}
31+
},
32+
"extra": {
33+
"branch-alias": {
34+
"dev-master": "1.0-dev"
35+
}
36+
},
37+
"scripts": {
38+
"phpcs": "vendor/bin/phpcs --standard=PSR2 --warning-severity=0 src",
39+
"phpstan": "vendor/bin/phpstan analyse --ansi -l 7 src"
40+
}
41+
}

phpunit.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false">
10+
<testsuites>
11+
<testsuite name="unit">
12+
<directory suffix="Test.php">./tests/unit</directory>
13+
</testsuite>
14+
</testsuites>
15+
<filter>
16+
<whitelist processUncoveredFilesFromWhitelist="true">
17+
<directory suffix=".php">./src</directory>
18+
</whitelist>
19+
</filter>
20+
</phpunit>

readme.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# DateTimePeriod
2+
3+
[![Build Status](https://travis-ci.org/pwm/treegami.svg?branch=master)](https://travis-ci.org/pwm/treegami)
4+
[![codecov](https://codecov.io/gh/pwm/treegami/branch/master/graph/badge.svg)](https://codecov.io/gh/pwm/treegami)
5+
[![Maintainability](https://api.codeclimate.com/v1/badges/25356a7f11c642ee8ac5/maintainability)](https://codeclimate.com/github/pwm/treegami/maintainability)
6+
[![Test Coverage](https://api.codeclimate.com/v1/badges/25356a7f11c642ee8ac5/test_coverage)](https://codeclimate.com/github/pwm/treegami/test_coverage)
7+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
8+
9+
TBD
10+
11+
## Table of Contents
12+
13+
* [Requirements](#requirements)
14+
* [Installation](#installation)
15+
* [Usage](#usage)
16+
* [How it works](#how-it-works)
17+
* [Tests](#tests)
18+
* [Changelog](#changelog)
19+
* [Licence](#licence)
20+
21+
## Requirements
22+
23+
PHP 7.1+
24+
25+
## Installation
26+
27+
$ composer require pwm/treegami
28+
29+
## Usage
30+
31+
TBD
32+
33+
## How it works
34+
35+
TBD
36+
37+
## Tests
38+
39+
$ vendor/bin/phpunit
40+
$ composer phpcs
41+
$ composer phpstan
42+
43+
## Changelog
44+
45+
[Click here](changelog.md)
46+
47+
## Licence
48+
49+
[MIT](LICENSE)

src/Tree.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Pwm\Treegami;
5+
6+
use Closure;
7+
8+
final class Tree
9+
{
10+
/** @var null|mixed */
11+
private $node;
12+
/** @var Tree[] */
13+
private $children;
14+
15+
public function __construct($node = null, array $children = [])
16+
{
17+
$this->node = $node;
18+
$this->children = $children;
19+
}
20+
21+
// (b -> (a, [b])) -> b -> Tree a
22+
public static function unfold(callable $f, $seed): Tree
23+
{
24+
$unfold = function (callable $f) use (&$unfold): Closure {
25+
return function ($seed) use (&$unfold, $f): Tree {
26+
[$node, $remaining] = $f($seed);
27+
return new Tree($node, \array_map($unfold($f), $remaining));
28+
};
29+
};
30+
[$node, $remaining] = $f($seed);
31+
return new Tree($node, \array_map($unfold($f), $remaining));
32+
}
33+
34+
// (a -> [b] -> b) -> Tree a -> b
35+
public function fold(callable $f)
36+
{
37+
$fold = function (Tree $tree) use (&$fold, $f) {
38+
return $f($tree->node, \array_map($fold, $tree->children));
39+
};
40+
return $fold($this);
41+
}
42+
43+
// (a -> b) -> Tree a -> Tree b
44+
public function map(callable $f): Tree
45+
{
46+
return $this->fold(function ($node, array $children) use ($f): Tree {
47+
return new Tree($f($node), $children);
48+
});
49+
}
50+
}

0 commit comments

Comments
 (0)