1
1
package com .thoughtworks
2
2
3
+ private [thoughtworks] sealed trait LowPrirorityExtractor {
4
+
5
+ sealed trait SeqExtractor [- A , + B ] {
6
+ def unapplySeq (a : A ): Option [Seq [B ]]
7
+ }
8
+
9
+ implicit final class OptionFunctionToSeqExtractor [- A , + B ] private [LowPrirorityExtractor ](underlying : A => Option [Seq [B ]]) {
10
+ def extractSeq = new SeqExtractor [A , B ] {
11
+ def unapplySeq (a : A ) = underlying(a)
12
+ }
13
+ }
14
+
15
+ implicit final class OptionFunctionToExtractor [- A , + B ] private [LowPrirorityExtractor ](underlying : A => Option [B ]) {
16
+ def extract = new Extractor [A , B ] {
17
+ def unapply (a : A ) = underlying(a)
18
+ }
19
+ }
20
+
21
+ }
22
+
23
+ /**
24
+ * A pattern that can be used in `match` / `case` expressions.
25
+ */
26
+ sealed trait Extractor [- A , + B ] {
27
+ def unapply (a : A ): Option [B ]
28
+ }
29
+
3
30
/**
4
31
* Utilities to convert between `A => Option[B]`, `PartialFunction[A, B]` and [[Extractor ]].
5
32
*
@@ -37,47 +64,24 @@ package com.thoughtworks
37
64
}}}
38
65
*
39
66
*/
40
- object Extractor {
41
-
42
- sealed trait SeqExtractor [- A , + B ] {
43
- def unapplySeq (a : A ): Option [Seq [B ]]
44
- }
67
+ object Extractor extends LowPrirorityExtractor {
45
68
46
69
implicit final class PartialFunctionToSeqExtractor [- A , + B ] private [Extractor ](underlying : PartialFunction [A , Seq [B ]]) {
47
70
def extractSeq = new SeqExtractor [A , B ] {
48
71
def unapplySeq (a : A ) = underlying.lift(a)
49
72
}
50
73
}
51
74
52
- implicit final class OptionFunctionToSeqExtractor [- A , + B ] private [Extractor ](underlying : A => Option [Seq [B ]]) {
53
- def extractSeq = new SeqExtractor [A , B ] {
54
- def unapplySeq (a : A ) = underlying(a)
55
- }
56
- }
57
-
58
75
implicit final class PartialFunctionToExtractor [- A , + B ] private [Extractor ](underlying : PartialFunction [A , B ]) {
59
76
def extract = new Extractor [A , B ] {
60
77
def unapply (a : A ) = underlying.lift(a)
61
78
}
62
79
}
63
80
64
- implicit final class OptionFunctionToExtractor [- A , + B ] private [Extractor ](underlying : A => Option [B ]) {
65
- def extract = new Extractor [A , B ] {
66
- def unapply (a : A ) = underlying(a)
67
- }
68
- }
69
-
70
81
implicit final class OptionFunctionToPartialFunction [- A , + B ] private [Extractor ](underlying : A => Option [B ]) {
71
82
def unlift : PartialFunction [A , B ] = {
72
83
case underlying.extract(b) => b
73
84
}
74
85
}
75
86
76
- }
77
-
78
- /**
79
- * A pattern that can be used in `match` / `case` expressions.
80
- */
81
- sealed trait Extractor [- A , + B ] {
82
- def unapply (a : A ): Option [B ]
83
- }
87
+ }
0 commit comments