我正在尝试将Haskell Semigroup转换为Scala。Haskell代码工作正常,但我不能用Scala编写它
Haskell:
import Data.Semigroup
newtype Combine a b = Combine { unCombine :: (a -> b) }
instance Semigroup b => Semigroup (Combine a b) where
Combine f <> Combine g = Combine (f <> g)
f = Combine $ \n -> Sum (n + 1)
g = Combine $ \n -> Sum (n - 1)
print (unCombine (f <> g) $ 0) -- Sum 0
print (unCombine (f <> g) $ 10) -- Sum 20Scala代码
import cats.Semigroup
import cats.instances.all._
trait Combine[A, B] {
def unCombine(a: A): B
}
val f = new Combine[Int, Int] {
override def unCombine(n: Int): Int = n + 1
}
val g = new Combine[Int, Int] {
override def unCombine(n: Int): Int = n - 1
}
implicit val mySemigroup: Semigroup[Combine[Int, Int]] = new Semigroup[Combine[Int, Int]] {
def combine(x: Combine[Int, Int], y: Combine[Int, Int]): Combine[Int, Int] = (x,y) match {
// ???
}
}发布于 2018-09-14 17:46:05
除了@KartikSabharwal的回答之外,因为Semigroup和Combine都是函数接口,因为Scala2.12可以定义如下的具体情况:
implicit val mySemigroup: Semigroup[Combine[Int, Int]] =
(x, y) => a => x.unCombine(a) + y.unCombine(a)在Scala 2.12中,@KartikSabharwal提到的通用案例如下所示:
// Don't forget to NOT import `cats.instances.all._` together with this import
import cats.implicits._
implicit def combineSemigroup[A, B](
implicit ev: Semigroup[B]
): Semigroup[Combine[A, B]] =
(x, y) => a => x.unCombine(a) combine y.unCombine(a)在Scala2.11中是这样的:
import cats.implicits._
implicit def combineSemigroup[A, B](
implicit ev: Semigroup[B]
): Semigroup[Combine[A, B]] =
new Semigroup[Combine[A, B]] {
override def combine(x: Combine[A, B], y: Combine[A, B]): Combine[A, B] =
new Combine[A, B] {
override def unCombine(a: A): B = x.unCombine(a) combine y.unCombine(a)
}
}发布于 2018-09-14 17:17:07
下面的代码回答了你的具体问题。
import cats.Semigroup
import cats.instances.all._
object Main extends App {
trait Combine[A, B] {
def unCombine(a: A): B
}
override def main(args: Array[String]): Unit = {
implicit val mySemigroup: Semigroup[Combine[Int, Int]] =
new Semigroup[Combine[Int, Int]] {
def combine(x: Combine[Int, Int], y: Combine[Int, Int]): Combine[Int, Int] =
new Combine[Int, Int] {
override def unCombine(n: Int): Int =
Semigroup[Int].combine(x.unCombine(n), y.unCombine(n))
}
}
val f = new Combine[Int, Int] {
override def unCombine(n: Int): Int = n + 1
}
val g = new Combine[Int, Int] {
override def unCombine(n: Int): Int = n - 1
}
val example = Semigroup[Combine[Int, Int]].combine(f, g).unCombine(10)
printf("%d\n", example)
}
}理想情况下,我希望在精神上复制Haskell代码,并实现某种形式的代码。
// 'a' can be any type
// Semigroup[b] must exist
implicit val mySemigroup: Semigroup[Combine[a, b]] =
def combine(x: Combine[a, b], y: Combine[a, b]): Combine[a, b] =
new Combine[a, b] {
override def unCombine(n: a): b =
Semigroup[b].combine(x.unCombine(n), y.unCombine(n))
}但我不知道足够的Scala来完成它。当我想出答案时,我会更新它,或者其他人可以来编辑这个答案/发布一个更好的答案。
https://stackoverflow.com/questions/52333274
复制相似问题