Last active
March 29, 2017 16:24
-
-
Save SRGOM/a41c07c1a7897064e3847b6ae78274d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Result[A] = Either[ Seq[ Error ], A ] //Type of Error is immaterial | |
//If I have a case class Border that takes Width and Color | |
case class Border( width: SizeUnit, color: Color ) | |
//then what I will invoke fn2 below as such: | |
//This function composese results. | |
//Just like apply of Border would take | |
//width and color and return Border | |
//this would take a Result(width), Result(color) | |
//and return Result(border) | |
def composeResultn2[ A, B, R ]( | |
resa: Result[ A ], | |
resb: Result[ B ] | |
f: (A,B) => R | |
) : Result[ R ] = { | |
( resa, resb ) match{ | |
case( Right( a ), Right( b ) ) => Right( f( a, b ) ) | |
case _ => Left( | |
Seq( a.left, b.left ) | |
.map( _.toOption ) | |
.flatten.flatten | |
) | |
} | |
//Invocation of composeResult2 | |
composeResult2( | |
Right( SizeUnit( 3 ) ), | |
Left( Error( "SomeError" ) ), | |
Border.apply | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment