gpt4 book ai didi

scala - 如何为类型 R[Out] { type In } 实现单子(monad)接口(interface)(map、flatMap)?

转载 作者:行者123 更新时间:2023-12-01 23:46:10 24 4
gpt4 key购买 nike

我想做的是实现一个读取器 monad R[Out] { type In } ,它读取类型 Out 的一些值(类型参数,或泛型类型) 来自抽象输入In(抽象类型成员)。它通常可以被视为 R[In, Out]

这是我目前所拥有的:

trait R[+Out] { self =>
type In

def apply(in: In): Out

def map[Out2](f: Out => Out2): R[Out2] = new R[Out2] {
type In = self.In
def apply(in: In): Out2 = f(self(in))
}

def flatMap[Out2](f: Out => R[Out2] { type In = self.In }): R[Out2] =
new R[Out2] {
type In = self.In
def apply(in: In): Out2 = f(self(in))(in)
}
}

def foo[I, O](o: O): R[O] = new R[O] {
type In = I
def apply(in: I): O = o
}

//
// compiles
//
val a: R[String] = foo[Int, String]("b").map(_ + "a")

// does not compile
//
// <console>:45: error: type mismatch;
// found : R[String]
// required: R[String]{type In = a.In}
//
val b: R[String] = a.flatMap(x => foo[Int, String](x))

我应该怎么做才能让它发挥作用?有可能吗?

最佳答案

问题似乎是您的一些方法,如 foo 返回 R[O],而不是 R[O] { type In = I } 。它可以编译,但是它的 apply 方法被破坏了,因为我们不知道内部类型别名是 Int,而是编译器看到了 f。在 中,所以它不知道它应该允许传递一个 Int:

scala> val f = foo[Int, String]("2")
f: R[String] = $anon$1@3cbccfc1

scala> f(2)
<console>:11: error: type mismatch;
found : Int(2)
required: f.In
f(2)
^

您可以通过修复返回类型来解决此问题:

def foo[I, O](o: O): R[O] { type In = I } = new R[O] {
type In = I
def apply(in: I): O = o
}

scala> val f = foo[Int, String]("2")
f: R[String]{type In = Int} = $anon$1@d9f9598

scala> f(2)
res20: String = 2 // It works!

同样,您的 mapflatMap 返回 R[Out2] 而不是 R[Out2] { type In = self.在 中。遗漏细化 { type In = self.In } 类似于在 R[I, O] 中遗漏类型参数 I .没有它,编译器就不知道 R[String] 有一个 type In = Int,即使它从代码中看起来非常明显。您还可以将其与向上转换返回值进行比较,在这种情况下您会丢失一些类型信息,否则这些信息可以通过使用更准确的类型来保证。

综合起来:

trait R[+Out] { self =>
type In

def apply(in: In): Out

def map[Out2](f: Out => Out2): R[Out2] { type In = self.In } = new R[Out2] {
type In = self.In
def apply(in: In): Out2 = f(self(in))
}

def flatMap[Out2](f: Out => R[Out2] { type In = self.In }): R[Out2] { type In = self.In } =
new R[Out2] {
type In = self.In
def apply(in: In): Out2 = f(self(in))(in)
}
}

def foo[I, O](o: O): R[O] { type In = I } = new R[O] {
type In = I
def apply(in: I): O = o
}

// At this point I just started relying on type inference to avoid
// continuing this notation.

scala> val a = foo[Int, String]("b").map(_ + "a")
a: R[String]{type In = Int} = R$$anon$1@4be408f0

scala> val b = a.flatMap(x => foo[Int, String](x))
b: R[String]{type In = a.In} = R$$anon$2@19fa8e56

关于scala - 如何为类型 R[Out] { type In } 实现单子(monad)接口(interface)(map、flatMap)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29156156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com