user2066049
user2066049

Reputation: 1371

Higher order functional approach to short circuit the execution and execute block of code if disjunction has right value

I have following def.

def withAuthorized[T,U](t:T)(func: T => U) : U = func(t)

Usage of it is

 withAuthorized(methodReturningDisjunction){ res : \/[Throwable,Boolean] => 
    res match{
      case \/-(r) => { block of code }
      case -\/(e) => e.left 
    }

where methodReturningDisjunction returns \/[Throwable,Boolean]

I want to abstract out res pattern matching logic into withAuthorized method such that it'll accept block of code and execute only if first def (methodReturningDisjunction) returns right side of disjunction. I am wondering what modifications will have to made to withAuthorized to make it function that way?

Upvotes: 1

Views: 101

Answers (1)

Shadowlands
Shadowlands

Reputation: 15074

What you are looking for is the map method on \/:

def withAuthorized[E,T,U](res:\/[E,T])(func: T => U) : \/[E,U] = res.map(func)

Upvotes: 2

Related Questions