Scala 中 hashmap forall() 方法的示例?

发布于 2024-11-07 23:32:23 字数 318 浏览 0 评论 0原文

有人可以举例说明如何使用 HashMap forall() 方法吗?我发现 Scala 文档难以理解。

我想要的是这样的:

val myMap = HashMap[Int, Int](1 -> 10, 2 -> 20) 
val areAllValuesTenTimesTheKey = myMap.forall((k, v) => k * 10 == v)

但这给出了:

error: wrong number of parameters; expected = 1

Can someone please give an example of how to use the HashMap forall() method? I find the Scala docs to be impenetrable.

What I want is something like this:

val myMap = HashMap[Int, Int](1 -> 10, 2 -> 20) 
val areAllValuesTenTimesTheKey = myMap.forall((k, v) => k * 10 == v)

but this gives:

error: wrong number of parameters; expected = 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一笑 2024-11-14 23:32:23

问题是forall

val myMap = HashMap[Int, Int](1 -> 10, 2 -> 20) 
val areAllValuesTenTimesTheKey = myMap.forall { case (k, v) => k * 10 == v }

想要一个接受单个 Tuple2 而不是两个参数的函数。 (当我们使用 forall 时,我们将 Map[A,B] 视为 Iterable[(A,B)]。)使用 case 语句是一个很好的解决方法;它实际上是在这里使用模式匹配来分解 Tuple2 并给出各个部分的名称。

如果你不想使用模式匹配,你也可以写,

val areAllValuesTenTimesTheKey = myMap.forall(p => p._1 * 10 == p._2 }

但我认为这没什么帮助。

You need instead

val myMap = HashMap[Int, Int](1 -> 10, 2 -> 20) 
val areAllValuesTenTimesTheKey = myMap.forall { case (k, v) => k * 10 == v }

The problem is that forall wants a function that takes a single Tuple2, rather than two arguments. (We're thinking of a Map[A,B] as an Iterable[(A,B)] when we use forall.) Using a case statement is a nice workaround; it's really using pattern matching here to break apart the Tuple2 and give the parts names.

If you don't want to use pattern matching, you could have also written

val areAllValuesTenTimesTheKey = myMap.forall(p => p._1 * 10 == p._2 }

but I think that's less helpful.

忆悲凉 2024-11-14 23:32:23

forall 传递单个 (Int, Int) 元组(而不是多个参数)。考虑这个(它明确地显示单个元组值被分解):

val areAllValuesTenTimesTheKey = myMap.forall(t => t match { case (k, v) => k * 10 == v })

或者,简写(实际上传递一个 PartialFunction):(

val areAllValuesTenTimesTheKey = myMap.forall {case (k, v) => k * 10 == v}

它们都分解接收的元组。)

此外,该函数可以被“tupled”ed:

val myMap = Map((1,10), (2,20))
val fn = (k: Int, v: Int) => k * 10 == v
val tupled_fn = fn.tupled
val areAllValuesTenTimesTheKey = myMap.forall(tupled_fn)

myMap: scala.collection.immutable.Map[Int,Int] = Map((1,10), (2,20))
fn: (Int, Int) => Boolean =          // takes in two parameters
tupled_fn: ((Int, Int)) => Boolean = // note that it now takes in a single Tuple
areAllValuesTenTimesTheKey: Boolean = true

Happy编码。

forall is passed a single (Int, Int) Tuple (as opposed to multiple parameters). Consider this (which explicitly shows a single tuple value is decomposed):

val areAllValuesTenTimesTheKey = myMap.forall(t => t match { case (k, v) => k * 10 == v })

Or, the short-hand (which actually passes a PartialFunction):

val areAllValuesTenTimesTheKey = myMap.forall {case (k, v) => k * 10 == v}

(These both decompose the tuple take in.)

Additionally, the function can be "tupled"ed:

val myMap = Map((1,10), (2,20))
val fn = (k: Int, v: Int) => k * 10 == v
val tupled_fn = fn.tupled
val areAllValuesTenTimesTheKey = myMap.forall(tupled_fn)

myMap: scala.collection.immutable.Map[Int,Int] = Map((1,10), (2,20))
fn: (Int, Int) => Boolean =          // takes in two parameters
tupled_fn: ((Int, Int)) => Boolean = // note that it now takes in a single Tuple
areAllValuesTenTimesTheKey: Boolean = true

Happy coding.

如何视而不见 2024-11-14 23:32:23

您的代码的问题在于,您为 forall 方法提供了一个函数,该函数接受 2 个参数并返回 Boolean,或者换句话说 (Int, Int) = >布尔值。如果您查看文档,那么您会发现这个签名:

def forall (p: ((A, B)) => Boolean): Boolean

在本例中,forall 方法需要 Tuple2[A, B] =>; Boolean,所以它也可以这样写:

def forall (p: Tuple2[A, B] => Boolean): Boolean

为了修复您的示例,您可以调用 forall 并为其提供接受 1 个元组参数的函数:

myMap.forall(keyVal => keyVal._1 * 10 == keyVal._2)

或者您使模式匹配并提取键和值:

myMap.forall {case (k, v) => k * 10 == v}

在本例中,您将 PartialFunction[(Int, Int), Boolean] 赋予 forall 方法

The problem with your code, is that you give forall method a function, that accepts 2 arguments and returns Boolean, or in other words (Int, Int) => Boolean. If you will look in the documentation, then you will find this signature:

def forall (p: ((A, B)) => Boolean): Boolean

in this case forall method expects Tuple2[A, B] => Boolean, so it also can be written like this:

def forall (p: Tuple2[A, B] => Boolean): Boolean

In order to fix your example you can either call forall and give it function, that accepts 1 tuple argument:

myMap.forall(keyVal => keyVal._1 * 10 == keyVal._2)

or you make patterns match and extract key and value:

myMap.forall {case (k, v) => k * 10 == v}

In this case you are giving PartialFunction[(Int, Int), Boolean] to the forall method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文