1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace { |
6
|
|
|
require_once __DIR__.'/Conversion/implicit_conversion.php'; |
7
|
|
|
require_once __DIR__.'/PatternMatching/functions.php'; |
8
|
|
|
require_once __DIR__.'/Reflection/functions.php'; |
9
|
|
|
require_once __DIR__.'/Type/restrictions.php'; |
10
|
|
|
require_once __DIR__.'/Utils/functions.php'; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
namespace Scalp { |
14
|
|
|
use function Scalp\Conversion\AnyToString; |
15
|
|
|
|
16
|
|
|
const identity = __NAMESPACE__.'\identity'; |
17
|
|
|
|
18
|
|
|
function identity($x) |
19
|
|
|
{ |
20
|
|
|
return $x; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
const concat = __NAMESPACE__.'\concat'; |
24
|
|
|
|
25
|
|
|
function concat(string ...$strings): string |
26
|
|
|
{ |
27
|
|
|
return implode('', $strings); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
const inc = __NAMESPACE__.'\inc'; |
31
|
|
|
|
32
|
|
|
function inc(int $x, int $step = 1): int |
33
|
|
|
{ |
34
|
|
|
return $x + $step; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
const throwE = __NAMESPACE__.'\throwE'; |
38
|
|
|
|
39
|
|
|
function throwE(string $class, string $message): void |
40
|
|
|
{ |
41
|
|
|
throw new $class($message); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
const println = __NAMESPACE__.'\println'; |
45
|
|
|
|
46
|
|
|
function println($x): void |
47
|
|
|
{ |
48
|
|
|
echo AnyToString($x)."\n"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
const None = __NAMESPACE__.'\None'; |
52
|
|
|
|
53
|
|
|
function None(): None |
54
|
|
|
{ |
55
|
|
|
return new None(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
const Some = __NAMESPACE__.'\Some'; |
59
|
|
|
|
60
|
|
|
function Some($x): Some |
61
|
|
|
{ |
62
|
|
|
return new Some($x); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
function Option($x): Option |
66
|
|
|
{ |
67
|
|
|
return ($x === null) ? None() : Some($x); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
const __ = '$argument$'; |
71
|
|
|
|
72
|
|
|
function papply(callable $f, ...$args): callable |
73
|
|
|
{ |
74
|
|
|
return new PartialApplication($f, $args); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
function Pair($_1, $_2): Tuple |
78
|
|
|
{ |
79
|
|
|
return new Tuple($_1, $_2); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function Tuple(...$elements): Tuple |
83
|
|
|
{ |
84
|
|
|
return new Tuple(...$elements); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
function curry(callable $f): callable |
88
|
|
|
{ |
89
|
|
|
return CurriedFunction::lift($f); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
function curryN(callable $f, int $arity): callable |
93
|
|
|
{ |
94
|
|
|
return CurriedFunction::liftN($f, $arity); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
namespace Scalp\Conversion { |
99
|
|
|
const AnyToString = __NAMESPACE__.'\AnyToString'; |
100
|
|
|
|
101
|
|
|
function AnyToString($any): string |
102
|
|
|
{ |
103
|
|
|
static $anyToString = null; |
104
|
|
|
|
105
|
|
|
if ($anyToString === null) { |
106
|
|
|
$anyToString = new AnyToString(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $anyToString($any); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|