-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathSequence.swift
157 lines (129 loc) · 3.59 KB
/
Sequence.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
public func catOptionals<S: Sequence, A>(_ xs: S) -> [A] where S.Element == A? {
return xs |> mapOptional(id)
}
public func mapOptional<S: Sequence, A>(_ f: @escaping (S.Element) -> A?) -> (S) -> [A] {
return { xs in
xs.compactMap(f)
}
}
// MARK: - Functor
extension Sequence {
public static func <¢> <A>(f: (Element) -> A, xs: Self) -> [A] {
return xs.map(f)
}
}
public func map<S: Sequence, A>(_ f: @escaping (S.Element) -> A) -> (S) -> [A] {
return { xs in
return f <¢> xs
}
}
// MARK: - Apply
extension Sequence {
public func apply<S: Sequence, A>(_ fs: S) -> [A] where S.Element == ((Element) -> A) {
// return fs.flatMap(self.map) // https://bugs.swift.org/browse/SR-5251
return fs.flatMap { f in self.map { x in f(x) } }
}
public static func <*> <S: Sequence, A>(fs: S, xs: Self) -> [A] where S.Element == ((Element) -> A) {
// return xs.apply(fs) // https://bugs.swift.org/browse/SR-5251
return fs.flatMap { f in xs.map { x in f(x) } }
}
}
public func apply<S: Sequence, T: Sequence, A>(_ fs: S) -> (T) -> [A] where S.Element == ((T.Element) -> A) {
return { xs in
// fs <*> xs // https://bugs.swift.org/browse/SR-5251
fs.flatMap { f in xs.map { x in f(x) } }
}
}
// MARK: - Bind/Monad
public func flatMap<S: Sequence, A>(_ f: @escaping (S.Element) -> [A]) -> (S) -> [A] {
return { xs in
xs.flatMap(f)
}
}
// MARK: - Monoid
extension Sequence where Element: Monoid {
public func concat() -> Element {
return Prelude.concat(self)
}
}
public func concat<S: Sequence>(_ xs: S) -> S.Element where S.Element: Monoid {
return xs.reduce(.empty, <>)
}
// MARK: - Foldable/Sequence
extension Sequence {
public func foldMap<M: Monoid>(_ f: @escaping (Element) -> M) -> M {
return self.reduce(M.empty) { m, x in m <> f(x) }
}
}
public func foldMap<S: Sequence, M: Monoid>(_ f: @escaping (S.Element) -> M) -> (S) -> M {
return { xs in
xs.foldMap(f)
}
}
// MARK: - Point-free Standard Library
public func contains<S: Sequence>(_ x: S.Element) -> (S) -> Bool where S.Element: Equatable {
return { xs in
xs.contains(x)
}
}
public func contains<S: Sequence>(where p: @escaping (S.Element) -> Bool) -> (S) -> Bool {
return { xs in
xs.contains(where: p)
}
}
public func filter<S: Sequence>(_ p: @escaping (S.Element) -> Bool) -> (S) -> [S.Element] {
return { xs in
xs.filter(p)
}
}
public func flatMap<S: Sequence, T: Sequence>(_ f: @escaping (S.Element) -> T) -> (S) -> [T.Element] {
return { xs in
xs.flatMap(f)
}
}
public func forEach<S: Sequence>(_ f: @escaping (S.Element) -> ()) -> (S) -> () {
return { xs in
xs.forEach(f)
}
}
public func map<A, S: Sequence>(_ f: @escaping (S.Element) -> A) -> (S) -> [A] {
return { xs in
xs.map(f)
}
}
public func reduce<A, S: Sequence>(_ f: @escaping (A, S.Element) -> A) -> (A) -> (S) -> A {
return { a in
{ xs in
xs.reduce(a, f)
}
}
}
public func sorted<S: Sequence>(_ xs: S) -> [S.Element] where S.Element: Comparable {
return xs.sorted()
}
public func sorted<S: Sequence>(by f: @escaping (S.Element, S.Element) -> Bool) -> (S) -> [S.Element] {
return { xs in
xs.sorted(by: f)
}
}
public func zipWith<S: Sequence, T: Sequence, A>(_ f: @escaping (S.Element, T.Element) -> A)
-> (S)
-> (T)
-> [A] {
return { xs in
return { ys in
return zip(xs, ys).map { f($0.0, $0.1) }
}
}
}
public func intersperse<A>(_ a: A) -> ([A]) -> [A] {
return { xs in
var result = [A]()
for x in xs.dropLast() {
result.append(x)
result.append(a)
}
xs.last.do { result.append($0) }
return result
}
}