/
sybclass.scala
141 lines (123 loc) · 3.46 KB
/
sybclass.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
/*
* Copyright (c) 2012-13 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package shapeless.examples
import shapeless._
import poly._
/*
* Examples of Scrap Your Boilerplate in action
*
* @author Miles Sabin
*/
object SybClassExamples {
// Example taken from the original SYB paper:
// "Scrap your boilerplate: a practical approach to generic programming", Ralf Laemmel, Simon Peyton Jones
// http://research.microsoft.com/en-us/um/people/simonpj/papers/hmap/
case class Company(depts : List[Dept])
sealed trait Subunit
case class Dept(name : Name, manager : Manager, subunits : List[Subunit]) extends Subunit
case class Employee(person : Person, salary : Salary) extends Subunit
case class Person(name : Name, address : Address)
case class Salary(salary : Int)
type Manager = Employee
type Name = String
type Address = String
def paradise : Unit = {
val beforeRaise =
Company(
List(
Dept("Research",
Employee(Person("Ralf", "Amsterdam"), Salary(8000)),
List(
Employee(Person("Joost", "Amsterdam"), Salary(1000)),
Employee(Person("Marlow", "Cambridge"), Salary(2000))
)
),
Dept("Strategy",
Employee(Person("Blair", "London"), Salary(100000)),
List()
)
)
)
// Compute a new company structure with all salaries increased by 10%
object raise extends ->((i: Int) => i*110/100)
val afterRaise = everywhere(raise)(beforeRaise)
println(afterRaise)
val expected =
Company(
List(
Dept("Research",
Employee(Person("Ralf", "Amsterdam"), Salary(8800)),
List(
Employee(Person("Joost", "Amsterdam"), Salary(1100)),
Employee(Person("Marlow", "Cambridge"),Salary(2200))
)
),
Dept("Strategy",
Employee(Person("Blair", "London"),Salary(110000)),
List()
)
)
)
assert(afterRaise == expected)
}
sealed trait Tree[T]
case class Leaf[T](t: T) extends Tree[T]
case class Node[T](left: Tree[T], right: Tree[T]) extends Tree[T]
def recursion : Unit = {
val tree: Tree[Int] =
Node(
Node(
Node(
Leaf(1),
Node(
Leaf(2),
Leaf(3)
)
),
Leaf(4)
),
Node(
Leaf(5),
Leaf(6)
)
)
object inc extends ->((i: Int) => i+1)
val result = everywhere(inc)(tree)
println(result)
val expected: Tree[Int] =
Node(
Node(
Node(
Leaf(2),
Node(
Leaf(3),
Leaf(4)
)
),
Leaf(5)
),
Node(
Leaf(6),
Leaf(7)
)
)
assert(expected == result)
}
def main(args: Array[String]) {
paradise
recursion
}
}