0
# Scala Extensions and Partial Functions
1
2
Scala-specific extensions that enable partial function support for more idiomatic Scala programming with automatic conversion between partial and total functions.
3
4
## Extension Import
5
6
```scala
7
import org.apache.flink.streaming.api.scala.extensions._
8
```
9
10
## Partial Function Support
11
12
```scala { .api }
13
class OnDataStream[T] {
14
def mapWith[R: TypeInformation](fun: PartialFunction[T, R]): DataStream[R]
15
def flatMapWith[R: TypeInformation](fun: PartialFunction[T, TraversableOnce[R]]): DataStream[R]
16
def filterWith(fun: PartialFunction[T, Boolean]): DataStream[T]
17
def keyingBy[K: TypeInformation](fun: PartialFunction[T, K]): KeyedStream[T, K]
18
}
19
20
class OnKeyedStream[T, K] {
21
def mapWith[R: TypeInformation](fun: PartialFunction[T, R]): DataStream[R]
22
def flatMapWith[R: TypeInformation](fun: PartialFunction[T, TraversableOnce[R]]): DataStream[R]
23
def filterWith(fun: PartialFunction[T, Boolean]): DataStream[T]
24
def reduceWith(fun: PartialFunction[(T, T), T]): DataStream[T]
25
}
26
```
27
28
## Usage Examples
29
30
```scala
31
import org.apache.flink.streaming.api.scala._
32
import org.apache.flink.streaming.api.scala.extensions._
33
34
val env = StreamExecutionEnvironment.getExecutionEnvironment
35
val events = env.fromElements("start", "data", "end", "error")
36
37
// Partial function mapping
38
val mapped = events.mapWith {
39
case "start" => "BEGIN"
40
case "end" => "FINISH"
41
case data => s"PROCESS: $data"
42
}
43
44
// Partial function filtering
45
val filtered = events.filterWith {
46
case "error" => false
47
case _ => true
48
}
49
```
50
51
Scala extensions provide idiomatic Scala programming patterns while maintaining Flink's type safety and performance characteristics.