-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSwiftskell.swift
129 lines (107 loc) · 2.71 KB
/
Swiftskell.swift
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
// --------------------------------
// A Swift × Haskell stdlib flavour
// --------------------------------
/// MARK: GHC.Show
public protocol Show: ~Copyable {
borrowing func show() -> String
}
public func print(_ s: borrowing some Show & ~Copyable) {
print(s.show())
}
/// MARK: Data.Eq
public protocol Eq: ~Copyable {
static func ==(_ a: borrowing Self, _ b: borrowing Self) -> Bool
static func /=(_ a: borrowing Self, _ b: borrowing Self) -> Bool
}
public extension Eq where Self: ~Copyable {
static func /=(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
return !(a == b)
}
}
/// MARK: Result
public enum Either<Success: ~Copyable, Failure: Error>: ~Copyable {
case success(Success)
case failure(Failure)
}
extension Either: Copyable {}
extension Either where Failure == Swift.Error {
public init(catching body: () throws -> Success) {
do {
self = .success(try body())
} catch {
self = .failure(error)
}
}
}
/// MARK: Iteration
public protocol Generator: ~Copyable {
associatedtype Element: ~Copyable
func next() -> Maybe<Element>
}
// MARK: Tuples
public enum Pair<L: ~Copyable, R: ~Copyable>: ~Copyable {
case elms(L, R)
}
/// MARK: Data.Maybe
public enum Maybe<Value: ~Copyable>: ~Copyable {
case just(Value)
case nothing
}
extension Maybe: Copyable {}
extension Maybe: Show where Value: Show & ~Copyable {
public borrowing func show() -> String {
switch self {
case let .just(borrowing elm):
return elm.show()
case .nothing:
return "<nothing>"
}
}
}
extension Maybe: Eq where Value: Eq, Value: ~Copyable {
public static func ==(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
switch a {
case let .just(borrowing a1):
switch b {
case let .just(borrowing b1):
return a1 == b1
case .nothing:
return false
}
case .nothing:
switch b {
case .just:
return false
case .nothing:
return true
}
}
}
}
// FIXME: triggers crash!
// @inlinable
// public func fromMaybe<A: ~Copyable>(_ defaultVal: consuming A,
// _ mayb: consuming Maybe<A>) -> A {
// switch mayb {
// case let .just(payload):
// return payload
// case .nothing:
// return defaultVal
// }
// }
public func isJust<A: ~Copyable>(_ m: borrowing Maybe<A>) -> Bool {
switch m {
case .just:
return true
case .nothing:
return false
}
}
@inlinable
public func isNothing<A: ~Copyable>(_ m: borrowing Maybe<A>) -> Bool {
return !isJust(m)
}
public struct UnownedRef<Instance: AnyObject> {
@usableFromInline
internal unowned(unsafe) var _value: Instance
}