Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. type Result[A] = Either[ Seq[ Error ], A ] //Type of Error is immaterial
  2.  
  3. //If I have a case class Border that takes Width and Color
  4. case class Border( width: SizeUnit, color: Color )
  5. //then what I will invoke fn2 below as such:
  6.  
  7.  
  8. //This function composese results.
  9. //Just like apply of Border would take
  10. //width and color and return Border
  11. //this would take a Result(width), Result(color)
  12. //and return Result(border)
  13. def composeResultn2[ A, B, R ](
  14. resa: Result[ A ],
  15. resb: Result[ B ]
  16. f: (A,B) => R
  17. ) : Result[ R ] = {
  18. ( resa, resb ) match{
  19. case( Right( a ), Right( b ) ) => Right( f( a, b ) )
  20. case _ => Left(
  21. Seq( a.left, b.left )
  22. .map( _.toOption )
  23. .flatten.flatten
  24. )
  25. }
  26.  
  27. //Invocation of composeResult2
  28. composeResult2(
  29. Right( SizeUnit( 3 ) ),
  30. Left( Error( "SomeError" ) ),
  31. Border.apply
  32. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement