Cats: Add `ifF` on Functor

Created on 8 Sep 2019  路  10Comments  路  Source: typelevel/cats

Signature:

//trait Functor[F[_]]
def ifF[A](fb: F[Boolean])(ifTrue: A, ifFalse: A): F[A]

Similar to ifM from Monad, but doesn't flatten.

Will be closed by #3058

Most helpful comment

That reads like "if and only if" :smile:
Should the branches be by-name though?

All 10 comments

Shouldn't it go to the IfFunctorOps like it was done for Apply and FlatMap?

I think it could be either of these places. Both in the type class with a default implementation and in the syntax. Probably only in the syntax until we drop 2.11 support though.

That reads like "if and only if" :smile:
Should the branches be by-name though?

For laziness, they probably should, yeah.

Good point about the name, but it matches ifA and ifM.

Implementation in terms of map is ok?

def ifF[A](fb: F[Boolean])(ifTrue: A, ifFalse: A): F[A] = map(fb)(x => if(x) ifTrue else ifFalse)

I think so, everything in Functor will use map :D

What about comment of @joroKr21 regarding by-name parameters? I have hit a problem with the as once, by putting there quite heavy computation that was ran despite of the failed flow. I know it is probably corner case, but still, there is possibility to encounter it.

I agree these should be by-name, or in Eval. We can probably add the latter in the future if it seems like a nice thing to have.

I can see @vasiliybondarenko is working on this!

Could anybody review my solution?

Merged to master in #3058!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

non picture non  路  3Comments

adelbertc picture adelbertc  路  5Comments

chuwy picture chuwy  路  4Comments

SimY4 picture SimY4  路  4Comments

diesalbla picture diesalbla  路  4Comments