在Scala中具有功能的半群

时间:2018-09-14 13:42:52

标签: scala haskell functional-programming typeclass semigroup

我正在尝试将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 20

Scala代码

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 {
    // ???
  }
}

1 个答案:

答案 0 :(得分:3)

除了@KartikSabharwal的回答之外,由于SemigroupCombine都是功能接口,因此自Scala 2.12开始,您可以定义如下特定情况:

implicit val mySemigroup: Semigroup[Combine[Int, Int]] =
  (x, y) => a => x.unCombine(a) + y.unCombine(a)

@KartikSabharwal提到的通用案例在Scala 2.12中看起来像这样:

// 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)

在Scala 2.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)
      }
  }