-
Notifications
You must be signed in to change notification settings - Fork 0
/
row-convert.wok
38 lines (32 loc) · 937 Bytes
/
row-convert.wok
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
#!/usr/bin/wok -f
/** row-convert.wok
*
* Usage:
* java -jar wok-0.1.0.jar -f row-convert.wok -v@rawstr expr=row.size input > output
*
* Option:
* -v@rawstr expr: The scala code to be applied to all of the rows. `row` represents the value of a row and the type
of which is `List[String]`. The type of the return value of this should be `List[Any]`.
*/
FS = '\t'
FQ = Quote.Min
OFS = '\t'
OFQ = Quote.All
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox
def source = s"""
new Function1[List[String], List[Any]] {
def apply(row: List[String]): List[Any] = {
${expr}
}
}
"""
val evaluator = {
val toolbox = currentMirror.mkToolBox()
val tree = toolbox.parse(source)
toolbox.eval(tree).asInstanceOf[Function1[List[String], List[Any]]]
}
In { _
.map (evaluator apply _)
.foreach (row => println(row: _*))
}