Skip to content

Commit 01ebe4e

Browse files
committed
dynaml-pipes: Improvements to pipes api
- DataPipe2 3 and 4 are now more compatible with DataPipe in composition operations
1 parent b2b6ef4 commit 01ebe4e

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

dynaml-pipes/src/main/scala-2.11/io/github/mandar2812/dynaml/pipes/DataPipe.scala

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ package io.github.mandar2812.dynaml.pipes
2020

2121
import scalaxy.streams.optimize
2222

23+
trait DataPipeConvertible[-Source, +Destination] {
24+
def toPipe: (Source) => Destination
25+
}
26+
27+
2328
/**
2429
* Top level trait representing an
2530
* abstract pipe that defines a transformation
2631
* between two data types, i.e. [[Source]] and [[Destination]]
2732
* @author mandar2812 on 18/11/15.
2833
*
2934
* */
30-
trait DataPipe[-Source, +Destination] extends Serializable {
35+
trait DataPipe[-Source, +Destination] extends DataPipeConvertible[Source, Destination] with Serializable {
3136

3237
self =>
3338

@@ -47,11 +52,11 @@ trait DataPipe[-Source, +Destination] extends Serializable {
4752
* [[Source]] -> [[Further]]
4853
*
4954
* */
50-
def >[Further](that: DataPipe[Destination, Further]):
51-
DataPipe[Source, Further] = {
52-
val runFunc = (d: Source) => that.run(self.run(d))
53-
DataPipe(runFunc)
54-
}
55+
def >[Further](that: DataPipeConvertible[Destination, Further]) =
56+
DataPipe((d: Source) => that.toPipe(self.run(d)))
57+
58+
/*def >[Result1, Result2](that: BifurcationPipe[Destination, Result1, Result2])
59+
: BifurcationPipe[Source, Result1, Result2] = DataPipe((x: Source) => that.run(self.run(x)))*/
5560

5661
/**
5762
* Represents the composition of two
@@ -72,6 +77,8 @@ trait DataPipe[-Source, +Destination] extends Serializable {
7277
def >-<[OtherSource, OtherDestination](that: DataPipe[OtherSource, OtherDestination])
7378
:DataPipe2[Source, OtherSource, (Destination, OtherDestination)] =
7479
DataPipe2((d1: Source, d2: OtherSource) => (self(d1), that(d2)))
80+
81+
override def toPipe: Source => Destination = self.run _
7582
}
7683

7784
object DataPipe {
@@ -87,8 +94,7 @@ object DataPipe {
8794
}
8895
}
8996

90-
def apply[S1, D1, S2, D2](pipe1: DataPipe[S1, D1],
91-
pipe2: DataPipe[S2, D2]): ParallelPipe[S1, D1, S2, D2] =
97+
def apply[S1, D1, S2, D2](pipe1: DataPipe[S1, D1], pipe2: DataPipe[S2, D2]): ParallelPipe[S1, D1, S2, D2] =
9298
ParallelPipe(pipe1.run, pipe2.run)
9399

94100
def apply[S, D1, D2](func: (S) => (D1, D2)):
@@ -106,8 +112,6 @@ object DataPipe {
106112
}
107113
}
108114

109-
110-
111115
trait ParallelPipe[-Source1, +Result1, -Source2, +Result2]
112116
extends DataPipe[(Source1, Source2), (Result1, Result2)] {
113117

@@ -133,6 +137,14 @@ object ParallelPipe {
133137
trait BifurcationPipe[-Source, +Result1, +Result2]
134138
extends DataPipe[Source, (Result1, Result2)] {
135139

140+
self =>
141+
142+
def >[FinalResult](other: DataPipe2[Result1, Result2, FinalResult]): DataPipe[Source, FinalResult] =
143+
DataPipe((input: Source) => {
144+
val (x, y) = self.run(input)
145+
other.run(x, y)
146+
})
147+
136148
}
137149

138150
trait SideEffectPipe[I] extends DataPipe[I, Unit] {
@@ -141,12 +153,12 @@ trait SideEffectPipe[I] extends DataPipe[I, Unit] {
141153

142154
object BifurcationPipe {
143155

144-
def apply[Source,
145-
Destination1,
146-
Destination2](pipe1: DataPipe[Source, Destination1],
147-
pipe2: DataPipe[Source, Destination2]):
148-
BifurcationPipe[Source, Destination1, Destination2] = {
156+
def apply[Source, Destination1, Destination2](f: (Source) => (Destination1, Destination2)) = DataPipe(f)
149157

158+
def apply[Source, Destination1, Destination2](
159+
pipe1: DataPipe[Source, Destination1],
160+
pipe2: DataPipe[Source, Destination2]):
161+
BifurcationPipe[Source, Destination1, Destination2] = {
150162
DataPipe((x: Source) => (pipe1.run(x), pipe2.run(x)))
151163
}
152164
}

dynaml-pipes/src/main/scala-2.11/io/github/mandar2812/dynaml/pipes/DataPipe2.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ package io.github.mandar2812.dynaml.pipes
2323
*
2424
* Data Pipes representing functions of 2 arguments
2525
*/
26-
trait DataPipe2[-Source1, -Source2, +Result] extends Serializable {
26+
trait DataPipe2[-Source1, -Source2, +Result] extends
27+
DataPipeConvertible[(Source1, Source2), Result] with
28+
Serializable {
29+
2730
self =>
2831

2932
def run(data1: Source1, data2: Source2): Result
@@ -33,6 +36,7 @@ trait DataPipe2[-Source1, -Source2, +Result] extends Serializable {
3336
def >[Result2](otherPipe: DataPipe[Result, Result2]): DataPipe2[Source1, Source2, Result2] =
3437
DataPipe2((d1: Source1, d2:Source2) => otherPipe.run(self.run(d1, d2)))
3538

39+
override def toPipe: ((Source1, Source2)) => Result = (x: (Source1, Source2)) => self.run(x._1, x._2)
3640
}
3741

3842
object DataPipe2 {

dynaml-pipes/src/main/scala-2.11/io/github/mandar2812/dynaml/pipes/DataPipe3.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ package io.github.mandar2812.dynaml.pipes
2323
*
2424
* Data Pipes representing functions of 3 arguments
2525
*/
26-
trait DataPipe3[-Source1, -Source2, -Source3, +Result] extends Serializable {
26+
trait DataPipe3[-Source1, -Source2, -Source3, +Result] extends
27+
DataPipeConvertible[(Source1, Source2, Source3), Result] with
28+
Serializable {
2729
self =>
2830

2931
def run(data1: Source1, data2: Source2, data3: Source3): Result
@@ -33,6 +35,8 @@ trait DataPipe3[-Source1, -Source2, -Source3, +Result] extends Serializable {
3335
def >[Result2](otherPipe: DataPipe[Result, Result2]): DataPipe3[Source1, Source2, Source3, Result2] =
3436
DataPipe3((d1: Source1, d2:Source2, d3: Source3) => otherPipe.run(self.run(d1, d2, d3)))
3537

38+
override def toPipe: ((Source1, Source2, Source3)) => Result =
39+
(x: (Source1, Source2, Source3)) => self.run(x._1, x._2, x._3)
3640
}
3741

3842
object DataPipe3 {

dynaml-pipes/src/main/scala-2.11/io/github/mandar2812/dynaml/pipes/DataPipe4.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ package io.github.mandar2812.dynaml.pipes
2323
*
2424
* Data Pipes representing functions of 4 arguments
2525
*/
26-
trait DataPipe4[-Source1, -Source2, -Source3, -Source4, +Result] extends Serializable {
26+
trait DataPipe4[-Source1, -Source2, -Source3, -Source4, +Result] extends
27+
DataPipeConvertible[(Source1, Source2, Source3, Source4), Result] with
28+
Serializable {
2729
self =>
2830

2931
def run(data1: Source1, data2: Source2, data3: Source3, data4: Source4): Result
@@ -33,6 +35,9 @@ trait DataPipe4[-Source1, -Source2, -Source3, -Source4, +Result] extends Seriali
3335
def >[Result2](otherPipe: DataPipe[Result, Result2]): DataPipe4[Source1, Source2, Source3, Source4, Result2] =
3436
DataPipe4((d1: Source1, d2:Source2, d3: Source3, d4: Source4) => otherPipe.run(self.run(d1, d2, d3, d4)))
3537

38+
override def toPipe: ((Source1, Source2, Source3, Source4)) => Result =
39+
(x: (Source1, Source2, Source3, Source4)) => self.run(x._1, x._2, x._3, x._4)
40+
3641
}
3742

3843
object DataPipe4 {

0 commit comments

Comments
 (0)