Skip to content
Newer
Older
100644 112 lines (101 sloc) 4.53 KB
6749e5d @odersky *** empty log message ***
odersky authored
1 /* __ *\
2 ** ________ ___ / / ___ Scala API **
2d11a5b Updated copyright notices to 2011
Antonio Cunei authored
3 ** / __/ __// _ | / / / _ | (c) 2002-2011, LAMP/EPFL **
a874f35 minor changes
michelou authored
4 ** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
6749e5d @odersky *** empty log message ***
odersky authored
5 ** /____/\___/_/ |_/____/_/ | | **
6 ** |/ **
7 \* */
8
91a9821 Rearranged header and updated copyright message
mihaylov authored
9
10
a874f35 minor changes
michelou authored
11 package scala
4cd3c13 - syntax fix: added the required "abstract" fla...
schinz authored
12
cb1c0cf @odersky lost of documentation and some small adjustment...
odersky authored
13 /** A partial function of type `PartialFunction[A, B]` is a
8388f49 @odersky allowed $super variables in doc comment; some m...
odersky authored
14 * unary function where the domain does not necessarily include all values of type
cb1c0cf @odersky lost of documentation and some small adjustment...
odersky authored
15 * `A`. The function `isDefinedAt` allows to
8388f49 @odersky allowed $super variables in doc comment; some m...
odersky authored
16 * test dynamically if a value is in the domain of the function.
c1bcad8 Added some comments and removed inconsistencies.
Matthias Zenger authored
17 *
8ac3654 Added more comments, removed StructuralEquality...
Matthias Zenger authored
18 * @author Martin Odersky
19 * @version 1.0, 16/07/2003
c1bcad8 Added some comments and removed inconsistencies.
Matthias Zenger authored
20 */
8388f49 @odersky allowed $super variables in doc comment; some m...
odersky authored
21 trait PartialFunction[-A, +B] extends (A => B) {
c1bcad8 Added some comments and removed inconsistencies.
Matthias Zenger authored
22
8388f49 @odersky allowed $super variables in doc comment; some m...
odersky authored
23 /** Checks if a value is contained in the function's domain.
29d431c Reverted r19466, as the decision is now that
Antonio Cunei authored
24 *
25 * @param x the value to test
cb1c0cf @odersky lost of documentation and some small adjustment...
odersky authored
26 * @return `true`, iff `x` is in the domain of this function, `false` otherwise.
29d431c Reverted r19466, as the decision is now that
Antonio Cunei authored
27 */
28 def isDefinedAt(x: A): Boolean
29
8388f49 @odersky allowed $super variables in doc comment; some m...
odersky authored
30 /** Composes this partial function with a fallback partial function which gets applied where this partial function
31 * is not defined.
32 *
33 * @param that the fallback function
34 * @tparam A1 the argument type of the fallback function
35 * @tparam B1 the result type of the fallback function
36 * @return a partial function which has as domain the union of the domains
37 * of this partial function and `that`. The resulting partial function
38 * takes `x` to `this(x)` where `this` is defined, and to `that(x)` where it is not.
39 */
3e1241c Reverted over-zealous replacement of 'PartialFu...
Antonio Cunei authored
40 def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]) : PartialFunction[A1, B1] =
8062384 Revamped scala-doc.
Sean McDirmid authored
41 new PartialFunction[A1, B1] {
29d431c Reverted r19466, as the decision is now that
Antonio Cunei authored
42 def isDefinedAt(x: A1): Boolean =
4d92915 @odersky Fixed parsing problem for closures
odersky authored
43 PartialFunction.this.isDefinedAt(x) || that.isDefinedAt(x)
44 def apply(x: A1): B1 =
45 if (PartialFunction.this.isDefinedAt(x)) PartialFunction.this.apply(x)
46 else that.apply(x)
47 }
48
8388f49 @odersky allowed $super variables in doc comment; some m...
odersky authored
49 /** Composes this partial function with a transformation function that gets applied
50 * to results of this partial function.
51 * @param k the transformation function
52 * @tparam C the result type of the transformation function.
53 * @return a partial function with the same domain as this partial function, which maps
54 * arguments `x` to `k(this(x))`.
55 */
3e1241c Reverted over-zealous replacement of 'PartialFu...
Antonio Cunei authored
56 override def andThen[C](k: B => C) : PartialFunction[A, C] = new PartialFunction[A, C] {
29d431c Reverted r19466, as the decision is now that
Antonio Cunei authored
57 def isDefinedAt(x: A): Boolean = PartialFunction.this.isDefinedAt(x)
4d92915 @odersky Fixed parsing problem for closures
odersky authored
58 def apply(x: A): C = k(PartialFunction.this.apply(x))
59 }
147ecff @odersky Added lift method to PartialFunction
odersky authored
60
8388f49 @odersky allowed $super variables in doc comment; some m...
odersky authored
61 /** Turns this partial function into an plain function returning an `Option` result.
fd1ca1e @paulp Moved unlift to the Function companion object, ...
paulp authored
62 * @see Function.unlift
8388f49 @odersky allowed $super variables in doc comment; some m...
odersky authored
63 * @return a function that takes an argument `x` to `Some(this(x))` if `this`
64 * is defined for `x`, and to `None` otherwise.
65 */
aab959b @paulp Proposed implementation of 'unlift' on Function...
paulp authored
66 def lift: A => Option[B] = new (A => Option[B]) {
67 def apply(x: A): Option[B] = if (isDefinedAt(x)) Some(PartialFunction.this.apply(x)) else None
68 }
6749e5d @odersky *** empty log message ***
odersky authored
69 }
4d92915 @odersky Fixed parsing problem for closures
odersky authored
70
397c202 @paulp A couple super useful partial function methods ...
paulp authored
71 /** A few handy operations which leverage the extra bit of information
72 * available in partial functions. Examples:
73 *
74 * <pre>
75 * import PartialFunction._
76 *
fe33490 @paulp Large Path/File/Directory checkin.
paulp authored
77 * def strangeConditional(other: Any): Boolean = cond(other) {
397c202 @paulp A couple super useful partial function methods ...
paulp authored
78 * case x: String if x == "abc" || x == "def" => true
79 * case x: Int => true
80 * }
fe33490 @paulp Large Path/File/Directory checkin.
paulp authored
81 * def onlyInt(v: Any): Option[Int] = condOpt(v) { case x: Int => x }
397c202 @paulp A couple super useful partial function methods ...
paulp authored
82 * </pre>
83 *
84 * @author Paul Phillips
85 * @since 2.8
86 */
87 object PartialFunction
88 {
89 /** Creates a Boolean test based on a value and a partial function.
90 * It behaves like a 'match' statement with an implied 'case _ => false'
91 * following the supplied cases.
92 *
93 * @param x the value to test
94 * @param pf the partial function
cb1c0cf @odersky lost of documentation and some small adjustment...
odersky authored
95 * @return true, iff `x` is in the domain of `pf` and `pf(x) == true`.
397c202 @paulp A couple super useful partial function methods ...
paulp authored
96 */
3e1241c Reverted over-zealous replacement of 'PartialFu...
Antonio Cunei authored
97 def cond[T](x: T)(pf: PartialFunction[T, Boolean]): Boolean =
397c202 @paulp A couple super useful partial function methods ...
paulp authored
98 (pf isDefinedAt x) && pf(x)
99
361a7a4 @paulp Took full advantage of the new =>? alias for th...
paulp authored
100 /** Transforms a PartialFunction[T, U] `pf' into Function1[T, Option[U]] `f'
397c202 @paulp A couple super useful partial function methods ...
paulp authored
101 * whose result is Some(x) if the argument is in pf's domain and None otherwise,
102 * and applies it to the value `x'. In effect, it is a 'match' statement
103 * which wraps all case results in Some(_) and adds 'case _ => None' to the end.
104 *
105 * @param x the value to test
361a7a4 @paulp Took full advantage of the new =>? alias for th...
paulp authored
106 * @param pf the PartialFunction[T, U]
cb1c0cf @odersky lost of documentation and some small adjustment...
odersky authored
107 * @return `Some(pf(x))` if `pf isDefinedAt x`, `None` otherwise.
397c202 @paulp A couple super useful partial function methods ...
paulp authored
108 */
3e1241c Reverted over-zealous replacement of 'PartialFu...
Antonio Cunei authored
109 def condOpt[T,U](x: T)(pf: PartialFunction[T, U]): Option[U] =
397c202 @paulp A couple super useful partial function methods ...
paulp authored
110 if (pf isDefinedAt x) Some(pf(x)) else None
c1f5cbd [no content change] Fixed all SVN properties: m...
Gilles Dubochet authored
111 }
Something went wrong with that request. Please try again.