在“一侧”调用函数Bifunctor 的值取决于布尔值

发布于 2024-10-01 10:24:30 字数 1037 浏览 2 评论 0原文

如果我有一个 Bifunctor[A,A] bf 实例,则函数 f : A => A 和 Booleanp

def calc[A, F[_,_]: Bifunctor](p: Boolean, bf: F[A, A], f: A => A): F[A, A] = {
  val BF = implicitly[Bifunctor[F]]
  BF.bimap(bf, (a : A) => if (p) f(a) else a, (a : A) => if (!p) f(a) else a)
}

我怎样才能更简洁(和表达)地表达它?基本上,我试图在依赖于某些谓词的双函子(例如Tuple2)一侧调用函数。如果谓词为 true,我想映射 LHS,如果为 false,则映射 RHS

val t2 = (1, 2)
def add4 = (_ : Int) + 4
calc(true, t2, add4) //should be (5,2)
calc(false, t2, add4) //should be (1,6)


Given that I want to use tuples (as opposed to the more general Bifunctor),I seem to be able to use arrows as follows:

def calc[A](p: Boolean, bf: (A, A), f: A => A): (A, A) 
  = (if (p) f.first[A] else f.second[A]) apply bf

If I have an instance of Bifunctor[A,A] bf, a function f : A => A and a Boolean value p:

def calc[A, F[_,_]: Bifunctor](p: Boolean, bf: F[A, A], f: A => A): F[A, A] = {
  val BF = implicitly[Bifunctor[F]]
  BF.bimap(bf, (a : A) => if (p) f(a) else a, (a : A) => if (!p) f(a) else a)
}

How can I put this more concisely (and expressively)? Basically I am trying to invoke a function on a side of a bifunctor (e.g. a Tuple2) dependent on some predicate. If the predicate is true, I want to map the LHS and the RHS if it's false

val t2 = (1, 2)
def add4 = (_ : Int) + 4
calc(true, t2, add4) //should be (5,2)
calc(false, t2, add4) //should be (1,6)


Given that I want to use tuples (as opposed to the more general Bifunctor),I seem to be able to use arrows as follows:

def calc[A](p: Boolean, bf: (A, A), f: A => A): (A, A) 
  = (if (p) f.first[A] else f.second[A]) apply bf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何处潇湘 2024-10-08 10:24:30

不是那么好:

def calc[A, F[_,_]:Bifunctor](p: Boolean, bf: F[A, A], f: A => A): F[A, A] =
   (if (p) (bf :-> (_: A => A)) else ((_:A => A) <-: bf))(f)

更好一点:

def cond[A:Zero](b: Boolean, a: A) = if (b) a else mzero

def calc[A, F[_,_]:Bifunctor](p: Boolean, bf: F[A, A], f: Endo[A]): F[A, A] =
  cond(p, f) <-: bf :-> cond(!p, f)

一些 Haskell,只是为了语言羡慕:

calc p = if p then first else second

Not all that much nicer:

def calc[A, F[_,_]:Bifunctor](p: Boolean, bf: F[A, A], f: A => A): F[A, A] =
   (if (p) (bf :-> (_: A => A)) else ((_:A => A) <-: bf))(f)

A little nicer:

def cond[A:Zero](b: Boolean, a: A) = if (b) a else mzero

def calc[A, F[_,_]:Bifunctor](p: Boolean, bf: F[A, A], f: Endo[A]): F[A, A] =
  cond(p, f) <-: bf :-> cond(!p, f)

Some Haskell, just for the language envy:

calc p = if p then first else second
不离久伴 2024-10-08 10:24:30

这种变化是否Apocalisp 的解决方案有效吗?

def calc[A, F[_,_]:Bifunctor](p: Boolean, bf: F[A, A], f: A => A): F[A, A] =
   (if (p) ((_: F[A,A]) :-> f) else (f <-: (_: F[A,A])))(bf)

注意:我没有使用 scalaz 对此进行测试。

Does this variation on Apocalisp's solution work?

def calc[A, F[_,_]:Bifunctor](p: Boolean, bf: F[A, A], f: A => A): F[A, A] =
   (if (p) ((_: F[A,A]) :-> f) else (f <-: (_: F[A,A])))(bf)

Note: I didn't test this with scalaz.

小姐丶请自重 2024-10-08 10:24:30

编辑:修复为返回 (A,A) 而不是 A

也许我错过了一些东西,但这不是临时变量的用途吗?使用常规 Scala 元组:

Some(bf).map(x => if (p) x.copy(_1 = f(x._1)) else x.copy(_2 = f(x._2))).get

{ val (l,r) = bf; if (p) (f(l),r) else (l,f(r)) }

Edit: fixed to return (A,A) instead of A

Maybe I'm missing something, but isn't this what temporary variables are for? With a regular Scala tuple:

Some(bf).map(x => if (p) x.copy(_1 = f(x._1)) else x.copy(_2 = f(x._2))).get

or

{ val (l,r) = bf; if (p) (f(l),r) else (l,f(r)) }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文