/
Cons.php
134 lines (116 loc) · 3.16 KB
/
Cons.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Teto\Functools\DataStructure;
/**
* Cons cell
*
* @package Teto
* @subpackage Functools\DataStructure
* @author USAMI Kenta <tadsan@zonu.me>
* @copyright 2015 USAMI Kenta
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
final class Cons implements \ArrayAccess, \Countable
{
/** @var mixed */
private $car;
/** @var mixed */
private $cdr;
/**
* @param mixed $car
* @param mixed $cdr
*/
public function __construct($car, $cdr)
{
$this->car = $car;
$this->cdr = $cdr;
}
public function __get($offset) { return $this->$offset; }
/** @return boolean */
public function count($n = 0)
{
if (is_null($this->car)) { return $n; }
if (is_null($this->cdr)) { return $n + 1; }
if (!$this->cdr instanceof self) { return $n + 2; }
return $this->cdr->count($n + 1);
}
/**
* @param int $offset
* @return boolean
*/
public function offsetExists($offset)
{
if ($offset === 0) { return isset($this->car); }
if ($offset === 1 && is_null($this->cdr)) {
return false;
}
if (is_int($offset) && $offset >= 1 && $this->cdr instanceof self) {
return isset($this->cdr[$offset - 1]);
}
return false;
}
/**
* Lisp nth
*
* @param int $offset
* @return boolean
*/
public function offsetGet($offset)
{
if ($offset === 0) { return $this->car; }
if ($offset === 1 && is_null($this->cdr)) {
return null;
}
if (is_int($offset) && $offset >= 1 && $this->cdr instanceof self) {
return $this->cdr[$offset - 1];
}
throw new \OutOfRangeException;
}
/**
* @param mixed $_offset
* @param mixed $_value
* @throws \OutOfRangeException
*/
public function offsetSet($_offset, $_value)
{
throw new \OutOfRangeException;
}
public function offsetUnset($_offset) {}
/**
* Get by property
*
* @param mixed $key
* @param mixed $default_val
* @return mixed
* @note It is similar to `plist-get' of Lisp
*/
public function pget($key, $default_val = null)
{
if ($this->car === $key) { return $this[1]; }
if ($this->cdr === null) { return $default_val; }
return $this->cdr->pget($key, $default_val);
}
/**
* @param mixed $key
* @param mixed $default_val
* @return mixed
* @note It is similar to `assoc' of Lisp
*/
public function assoc($key, $default_val = null)
{
if ($this->car->car === $key) { return $this->car; }
if ($this->cdr === null) { return $default_val; }
return $this->cdr->assoc($key, $default_val);
}
/**
* @param mixed $key
* @param mixed $default_val
* @return mixed
* @note It is similar to `rassoc' of Lisp
*/
public function rassoc($key, $default_val = null)
{
if ($this->car->cdr === $key) { return $this->car; }
if ($this->cdr === null) { return $default_val; }
return $this->cdr->rassoc($key, $default_val);
}
}