/
Yield.scala
213 lines (177 loc) · 8.68 KB
/
Yield.scala
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.thoughtworks.dsl.keywords
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.Keyword
import com.thoughtworks.dsl.keywords.Yield.From
import com.thoughtworks.enableMembersIf
import scala.collection._
import scala.collection.generic.CanBuildFrom
import scala.concurrent.Future
import scala.language.implicitConversions
import scala.language.higherKinds
/**
* @author 杨博 (Yang Bo)
* @example This `Yield` keyword must be put inside a function that returns `Seq[Element]` or `Seq[Element] !! ...`,
* or it will not compile.
*
* {{{
* "def f(): Int = !Yield(1)" shouldNot compile
* }}}
*
* @example [[Yield]] keywords can be used together with other keywords.
* {{{
* def gccFlagBuilder(sourceFile: String, includes: String*): Stream[String] = {
* !Yield("gcc")
* !Yield("-c")
* !Yield(sourceFile)
* val include = !Each(includes)
* !Yield("-I")
* !Yield(include)
* !Continue
* }
*
* gccFlagBuilder("main.c", "lib1/include", "lib2/include") should be(Stream("gcc", "-c", "main.c", "-I", "lib1/include", "-I", "lib2/include"))
* }}}
* @see [[comprehension]] if you want to use traditional `for` comprehension instead of !-notation.
*/
final case class Yield[Element](element: Element) extends AnyVal with Keyword[Yield[Element], Unit]
private[keywords] trait LowPriorityYield2 {
def apply[A](elements: A*) = {
From(elements)
}
implicit def iteratorYieldFromDsl[A, FromCollection <: TraversableOnce[A]]
: Dsl[From[FromCollection], Iterator[A], Unit] =
new Dsl[From[FromCollection], Iterator[A], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => Iterator[A]): Iterator[A] = {
keyword.elements.toIterator ++ generateTail(())
}
}
implicit def iteratorYieldDsl[A, B >: A]: Dsl[Yield[A], Iterator[B], Unit] =
new Dsl[Yield[A], Iterator[B], Unit] {
def cpsApply(keyword: Yield[A], generateTail: Unit => Iterator[B]): Iterator[B] = {
Iterator.single(keyword.element) ++ generateTail(())
}
}
}
private[keywords] object YieldScalaVersions {
@enableMembersIf(scala.util.Properties.versionNumberString.matches("""^2\.1(1|2)\..*$"""))
object Scala211Or212 {
trait LowPriorityYield1 extends LowPriorityYield2 {
implicit def seqViewYieldFromDsl[A, FromCollection <: Traversable[A], Coll1, Coll2](
implicit canBuildFrom: CanBuildFrom[SeqView[A, Coll1], A, SeqView[A, Coll2]])
: Dsl[From[FromCollection], SeqView[A, Coll1], Unit] =
new Dsl[From[FromCollection], SeqView[A, Coll1], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => SeqView[A, Coll1]): SeqView[A, Coll1] = {
(keyword.elements.toIterable ++: generateTail(()))(canBuildFrom).asInstanceOf[SeqView[A, Coll1]]
}
}
implicit def seqViewYieldDsl[A, B >: A, Coll1, Coll2](
implicit canBuildFrom: CanBuildFrom[scala.collection.SeqView[B, Coll1], B, SeqView[B, Coll2]])
: Dsl[Yield[A], SeqView[B, Coll1], Unit] =
new Dsl[Yield[A], SeqView[B, Coll1], Unit] {
def cpsApply(keyword: Yield[A], generateTail: Unit => SeqView[B, Coll1]): SeqView[B, Coll1] = {
(keyword.element +: generateTail(()))(canBuildFrom).asInstanceOf[SeqView[B, Coll1]]
}
}
}
trait LowPriorityYield0 extends LowPriorityYield1 {
implicit def seqYieldFromDsl[A, FromCollection <: Iterable[A], Collection[X] <: SeqLike[X, Collection[X]]](
implicit canBuildFrom: CanBuildFrom[Collection[A], A, Collection[A]])
: Dsl[From[FromCollection], Collection[A], Unit] =
new Dsl[From[FromCollection], Collection[A], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => Collection[A]): Collection[A] = {
keyword.elements ++: generateTail(())
}
}
implicit def seqYieldDsl[A, B >: A, Collection[X] <: SeqLike[X, Collection[X]]](
implicit canBuildFrom: CanBuildFrom[Collection[B], B, Collection[B]]): Dsl[Yield[A], Collection[B], Unit] =
new Dsl[Yield[A], Collection[B], Unit] {
def cpsApply(keyword: Yield[A], generateTail: Unit => Collection[B]): Collection[B] = {
keyword.element +: generateTail(())
}
}
}
}
@enableMembersIf(scala.util.Properties.versionNumberString.matches("""^2\.13\..*$"""))
object Scala213 {
trait LowPriorityYield1 extends LowPriorityYield2 {
implicit def viewYieldFromDsl[A, FromCollection <: View.SomeIterableOps[A]]
: Dsl[From[FromCollection], View[A], Unit] =
new Dsl[From[FromCollection], View[A], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => View[A]): View[A] = {
new View.Concat(keyword.elements, generateTail(()))
}
}
implicit def seqViewYieldDsl[A, B >: A]: Dsl[Yield[A], SeqView[B], Unit] =
new Dsl[Yield[A], SeqView[B], Unit] {
def cpsApply(keyword: Yield[A], generateTail: Unit => SeqView[B]): SeqView[B] = {
generateTail(()).prepended(keyword.element)
}
}
implicit def indexedSeqViewYieldDsl[A, B >: A]: Dsl[Yield[A], IndexedSeqView[B], Unit] =
new Dsl[Yield[A], IndexedSeqView[B], Unit] {
def cpsApply(keyword: Yield[A], generateTail: Unit => IndexedSeqView[B]): IndexedSeqView[B] = {
generateTail(()).prepended(keyword.element)
}
}
implicit def viewYieldDsl[A, B >: A]: Dsl[Yield[A], View[B], Unit] =
new Dsl[Yield[A], View[B], Unit] {
def cpsApply(keyword: Yield[A], generateTail: Unit => View[B]): View[B] = {
new View.Concat[B](new View.Single(keyword.element), generateTail(()))
}
}
}
trait LowPriorityYield0 extends LowPriorityYield1 {
implicit def seqYieldFromDsl[A,
FromCollection <: View.SomeIterableOps[A],
Collection[X] <: SeqOps[X, Collection, Collection[X]]]
: Dsl[From[FromCollection], Collection[A], Unit] =
new Dsl[From[FromCollection], Collection[A], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => Collection[A]): Collection[A] = {
keyword.elements.toIterable ++: generateTail(())
}
}
implicit def seqYieldDsl[A, B >: A, Collection[+X] <: SeqOps[X, Collection, Collection[X]]]
: Dsl[Yield[A], Collection[B], Unit] =
new Dsl[Yield[A], Collection[B], Unit] {
def cpsApply(keyword: Yield[A], generateTail: Unit => Collection[B]): Collection[B] = {
keyword.element +: generateTail(())
}
}
}
}
}
import YieldScalaVersions.Scala211Or212._
import YieldScalaVersions.Scala213._
object Yield extends LowPriorityYield0 {
final case class From[FromCollection <: TraversableOnce[_]](elements: FromCollection)
extends AnyVal
with Keyword[From[FromCollection], Unit]
implicit def implicitYieldFrom[FromCollection <: TraversableOnce[_]](elements: FromCollection): From[FromCollection] =
From(elements)
implicit def streamYieldFromDsl[A, FromCollection <: Iterable[A]]: Dsl[From[FromCollection], Stream[A], Unit] =
new Dsl[From[FromCollection], Stream[A], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => Stream[A]): Stream[A] = {
keyword.elements.toStream.append(generateTail(()))
}
}
implicit def futureStreamYieldFromDsl[A, FromCollection <: Iterable[A]]
: Dsl[From[FromCollection], Stream[Future[A]], Unit] =
new Dsl[From[FromCollection], Stream[Future[A]], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => Stream[Future[A]]): Stream[Future[A]] = {
keyword.elements.toStream.map(Future.successful).append(generateTail(()))
}
}
implicit def implicitYield[Element](element: Element): Yield[Element] = Yield[Element](element)
implicit def streamYieldDsl[Element, That >: Element]: Dsl[Yield[Element], Stream[That], Unit] =
new Dsl[Yield[Element], Stream[That], Unit] {
def cpsApply(keyword: Yield[Element], generateTail: Unit => Stream[That]): Stream[That] = {
new Stream.Cons(keyword.element, generateTail(()))
}
}
implicit def futureStreamYieldDsl[Element, That >: Element]: Dsl[Yield[Element], Stream[Future[That]], Unit] =
new Dsl[Yield[Element], Stream[Future[That]], Unit] {
def cpsApply(keyword: Yield[Element], generateTail: Unit => Stream[Future[That]]): Stream[Future[That]] = {
new Stream.Cons(Future.successful(keyword.element), generateTail(()))
}
}
}