-
Notifications
You must be signed in to change notification settings - Fork 29
/
Each.scala
36 lines (28 loc) · 1.16 KB
/
Each.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
package com.thoughtworks.dsl.keywords
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.Keyword
import scala.collection.GenTraversableOnce
import scala.collection.generic.CanBuildFrom
import scala.language.implicitConversions
/**
* @author 杨博 (Yang Bo)
*/
final case class Each[Element](elements: Traversable[Element]) extends Keyword[Each[Element], Element]
object Each {
implicit def implicitEach[Element](elements: Traversable[Element]): Each[Element] = Each[Element](elements)
implicit def eachDsl[Element, That, B](
implicit thatIsTraversableOnce: (Element => That) <:< (Element => GenTraversableOnce[B]),
bf: CanBuildFrom[Nothing, B, That]
): Dsl[Each[Element], That, Element] =
new Dsl[Each[Element], That, Element] {
def interpret(keyword: Each[Element], handler: Element => That): That = {
keyword.elements.flatMap(handler)(collection.breakOut(bf))
}
}
implicit def foreachDsl[Element]: Dsl[Each[Element], Unit, Element] =
new Dsl[Each[Element], Unit, Element] {
def interpret(keyword: Each[Element], handler: Element => Unit): Unit = {
keyword.elements.foreach(handler)
}
}
}