scala - scala.collection.Map[Int, T] 的仿函数

标签 scala dictionary functor

我正在尝试查找是否可以为 Map 类型创建 Functor 的信息。 文档包含 ListOption 的信息,但不适用于我的情况。

你能告诉我是否可以创建一个 Functor[Map[Int, T]]

下面我将附上 List 的类似仿函数的实现。

trait Functor[F[_]]:
    def map[A, B](list: F[A])(f: A => B): F[B]

given Functor[List] with
    def map[A, B](list: List[A])(f: A => B): List[B] = ???

最佳答案

扩展 Luis 和 Andrey 的答案,使用 Scala 2 和 scala-cats 尝试使用类型别名

import cats.Functor

type MapInt[T] = Map[Int, T]
Functor[MapInt].map(Map(1 -> "woo"))(a => a + "hoo") 
// : MapInt[String] = Map(1 -> "woohoo")

或使用 Scala 2 类型的 lambda "atrocity"

Functor[({type MapInt[T]=Map[Int, T]})#MapInt].map(Map(1 -> "woo"))(a => a + "hoo")
// : MapInt[String] = Map(1 -> "woohoo")

或使用 kind-projector

Functor[Map[Int, *]].map(Map(1 -> "woo"))(a => a + "hoo")
// : MapInt[String] = Map(1 -> "woohoo")

scastie


关于 Map 对于 Functor 的“种类”错误,尝试使用 REPL 来探索这个想法(如果你从 sbt console 开始应该使用 build.sbt 的所有依赖项加载它):

scala> import cats.Functor
import cats.Functor

scala> :kind -v Functor
cats.Functor's kind is X[F[A]]
(* -> *) -> *
This is a type constructor that takes type constructor(s): a higher-kinded type.

scala> :kind -v Map
Map's kind is F[A1,+A2]
* -> * -(+)-> *
This is a type constructor: a 1st-order-kinded type.

scala> type MapInt[T] = Map[Int, T]
type MapInt

scala> :kind -v MapInt
MapInt's kind is F[A]
* -> *
This is a type constructor: a 1st-order-kinded type.

注意 Functor 如何具有高阶种类

(* -> *) -> *
\______/
   |
required shape of type argument to Functor

这意味着它需要一个一阶类型的类型构造函数,特别是一个接受单个类型参数的类型构造函数

* -> *
|
only one type argument expected

现在 Map 类型构造函数本身确实具有 Functor 所期望的一阶类型,但是它是错误的 arity 因为它需要两个输入参数而不是一个

1st arg to Map
|
* -> * --> *
     |
   2nd arg to Map

因此我们需要一个类型 lambda 来将 Map 的第一个类型参数固定为 Int,同时保持第二个类型参数空闲,这会将其转换为正确类型的类型构造函数和 Functor

的数量
scala> :kind -v MapInt
MapInt's kind is F[A]
* -> *

这是一个高阶类型构造函数的示例,它采用另一个二元一阶类型构造函数作为其类型参数

trait Foo[F[A, B]]

让我们检查一下 Map 现在是否适合而无需使用类型 lambda

scala> trait Foo[F[A, B]]
trait Foo

scala> :kind -v Foo
Foo's kind is X[F[A1,A2]]
(* -> * -> *) -> *
This is a type constructor that takes type constructor(s): a higher-kinded type.

scala> new Foo[Map] {}
val res2: Foo[Map] = $anon$1@589af27e

关于scala - scala.collection.Map[Int, T] 的仿函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67460120/

相关文章:

c++ - 为指针后面的对象创建比较器的通用方法

python - 如何比较两个具有不同键但相似值的字典并删除重复项

haskell - Functor/Applicative 可以绑定(bind)到一种特定的类型或结构吗?

haskell - 在具有多个类型类的自定义数据类型上使用仿函数/应用程序?

eclipse - 使用 maven-scala-plugin 构建 jar

python - 如何从给定键列表的嵌套字典中检索值?

c++ - C++中如何从map的迭代器中调用类成员函数?

scala - 在Scala中将null转换为Int和Double

java - 将变量声明为类级常量是否比局部变量更能提高性能?

scala - Scala Slick 3.0 中的随机行