如果析取具有正确的值,则高阶函数方法会短路执行和执行代码块
Higher order functional approach to short circuit the execution and execute block of code if disjunction has right value
我关注def
。
def withAuthorized[T,U](t:T)(func: T => U) : U = func(t)
它的用法是
withAuthorized(methodReturningDisjunction){ res : \/[Throwable,Boolean] =>
res match{
case \/-(r) => { block of code }
case -\/(e) => e.left
}
其中 methodReturningDisjunction
returns \/[Throwable,Boolean]
我想将 res
模式匹配逻辑抽象到 withAuthorized
方法中,这样它将接受代码块并仅在第一个 def
时执行(methodReturningDisjunction
) returns right
disjunction
一侧。我想知道必须对 withAuthorized
进行哪些修改才能使其以这种方式运行?
你要找的是 \/
:
上的 map 方法
def withAuthorized[E,T,U](res:\/[E,T])(func: T => U) : \/[E,U] = res.map(func)
我关注def
。
def withAuthorized[T,U](t:T)(func: T => U) : U = func(t)
它的用法是
withAuthorized(methodReturningDisjunction){ res : \/[Throwable,Boolean] =>
res match{
case \/-(r) => { block of code }
case -\/(e) => e.left
}
其中 methodReturningDisjunction
returns \/[Throwable,Boolean]
我想将 res
模式匹配逻辑抽象到 withAuthorized
方法中,这样它将接受代码块并仅在第一个 def
时执行(methodReturningDisjunction
) returns right
disjunction
一侧。我想知道必须对 withAuthorized
进行哪些修改才能使其以这种方式运行?
你要找的是 \/
:
def withAuthorized[E,T,U](res:\/[E,T])(func: T => U) : \/[E,U] = res.map(func)