簡體   English   中英

在Scala中具有功能的半群

[英]Semigroup with function in Scala

我正在嘗試將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 {
    // ???
  }
}

除了由@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)
      }
  }

這是可以回答您特定問題的代碼。

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的了解還不夠多。 找出答案后,我將對其進行更新,否則其他人可以跟進並編輯此答案/發布更好的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM