Skip to content

Instantly share code, notes, and snippets.

@SRGOM
Last active March 29, 2017 16:24
Show Gist options
  • Save SRGOM/a41c07c1a7897064e3847b6ae78274d9 to your computer and use it in GitHub Desktop.
Save SRGOM/a41c07c1a7897064e3847b6ae78274d9 to your computer and use it in GitHub Desktop.
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