1
1
object Test extends App {
2
2
3
3
implicit object O {
4
- def em ( this x : Int ): Boolean = x > 0
4
+ def ( x : Int ) em : Boolean = x > 0
5
5
}
6
6
7
7
assert(1 .em == O .em(1 ))
8
8
9
9
case class Circle (x : Double , y : Double , radius : Double )
10
10
11
11
implicit object CircleOps {
12
- def circumference ( this c : Circle ): Double = c.radius * math.Pi * 2
12
+ def ( c : Circle ) circumference : Double = c.radius * math.Pi * 2
13
13
}
14
14
15
15
val circle = new Circle (1 , 1 , 2.0 )
16
16
17
17
assert(circle.circumference == CircleOps .circumference(circle))
18
18
19
19
implicit object StringOps {
20
- def longestStrings ( this xs : Seq [String ]): Seq [String ] = {
20
+ def ( xs : Seq [String ]) longestStrings : Seq [String ] = {
21
21
val maxLength = xs.map(_.length).max
22
22
xs.filter(_.length == maxLength)
23
23
}
@@ -26,28 +26,28 @@ object Test extends App {
26
26
assert(names.longestStrings == List (" hello" , " world" ))
27
27
28
28
implicit object SeqOps {
29
- def second [ T ]( this xs : Seq [T ]) = xs.tail.head
29
+ def ( xs : Seq [T ]) second[ T ] = xs.tail.head
30
30
}
31
31
32
32
assert(names.longestStrings.second == " world" )
33
33
34
34
implicit object ListListOps {
35
- def flattened [ T ]( this xs : List [List [T ]]) = xs.foldLeft[List [T ]](Nil )(_ ++ _)
35
+ def ( xs : List [List [T ]]) flattened[ T ] = xs.foldLeft[List [T ]](Nil )(_ ++ _)
36
36
}
37
37
38
38
assert(List (names, List (" !" )).flattened == names :+ " !" )
39
39
assert(Nil .flattened == Nil )
40
40
41
41
trait SemiGroup [T ] {
42
- def combine ( this x : T )(y : T ): T
42
+ def ( x : T ) combine (y : T ): T
43
43
}
44
44
trait Monoid [T ] extends SemiGroup [T ] {
45
45
def unit : T
46
46
}
47
47
48
48
// An instance declaration:
49
49
implicit object StringMonoid extends Monoid [String ] {
50
- def combine ( this x : String )(y : String ): String = x.concat(y)
50
+ def ( x : String ) combine (y : String ): String = x.concat(y)
51
51
def unit : String = " "
52
52
}
53
53
@@ -58,20 +58,20 @@ object Test extends App {
58
58
println(sum(names))
59
59
60
60
trait Ord [T ] {
61
- def compareTo ( this x : T )(y : T ): Int
62
- def < ( this x : T )(y : T ) = x.compareTo(y) < 0
63
- def > ( this x : T )(y : T ) = x.compareTo(y) > 0
61
+ def ( x : T ) compareTo (y : T ): Int
62
+ def ( x : T ) < (y : T ) = x.compareTo(y) < 0
63
+ def ( x : T ) > (y : T ) = x.compareTo(y) > 0
64
64
val minimum : T
65
65
}
66
66
67
67
implicit object IntOrd extends Ord [Int ] {
68
- def compareTo ( this x : Int )(y : Int ) =
68
+ def ( x : Int ) compareTo (y : Int ) =
69
69
if (x < y) - 1 else if (x > y) + 1 else 0
70
70
val minimum = Int .MinValue
71
71
}
72
72
73
73
class ListOrd [T : Ord ] extends Ord [List [T ]] {
74
- def compareTo ( this xs : List [T ])(ys : List [T ]): Int = (xs, ys) match {
74
+ def ( xs : List [T ]) compareTo (ys : List [T ]): Int = (xs, ys) match {
75
75
case (Nil , Nil ) => 0
76
76
case (Nil , _) => - 1
77
77
case (_, Nil ) => + 1
@@ -93,25 +93,25 @@ object Test extends App {
93
93
println(max(List (1 , 2 , 3 ), List (2 )))
94
94
95
95
trait Functor [F [_]] {
96
- def map [ A , B ]( this x : F [A ])(f : A => B ): F [B ]
96
+ def ( x : F [A ]) map [ A , B ] (f : A => B ): F [B ]
97
97
}
98
98
99
99
trait Monad [F [_]] extends Functor [F ] {
100
- def flatMap [ A , B ]( this x : F [A ])(f : A => F [B ]): F [B ]
101
- def map [ A , B ]( this x : F [A ])(f : A => B ) = x.flatMap(f `andThen` pure)
100
+ def ( x : F [A ]) flatMap [ A , B ] (f : A => F [B ]): F [B ]
101
+ def ( x : F [A ]) map [ A , B ] (f : A => B ) = x.flatMap(f `andThen` pure)
102
102
103
103
def pure [A ](x : A ): F [A ]
104
104
}
105
105
106
106
implicit object ListMonad extends Monad [List ] {
107
- def flatMap [ A , B ]( this xs : List [A ])(f : A => List [B ]): List [B ] =
107
+ def ( xs : List [A ]) flatMap [ A , B ] (f : A => List [B ]): List [B ] =
108
108
xs.flatMap(f)
109
109
def pure [A ](x : A ): List [A ] =
110
110
List (x)
111
111
}
112
112
113
113
class ReaderMonad [Ctx ] extends Monad [[X ] => Ctx => X ] {
114
- def flatMap [ A , B ]( this r : Ctx => A )(f : A => Ctx => B ): Ctx => B =
114
+ def ( r : Ctx => A ) flatMap [ A , B ] (f : A => Ctx => B ): Ctx => B =
115
115
ctx => f(r(ctx))(ctx)
116
116
def pure [A ](x : A ): Ctx => A =
117
117
ctx => x
0 commit comments