English 中文(简体)
Haskell s 插入 地图
原标题:Scala equivalent of Haskell s insertWith for Maps

我期望完成把言词列入“努力”的简单任务。 我发现的最容易的方法是使用地图来跟踪字频率。 此前,我曾与Haskell一起使用其<代码>Map的功能insertWith,该功能具有解决重大碰撞的功能,同时具有关键和价值。 但是,在Schala的图书馆中,我可以发现任何类似之处;只有附加功能(+<>/code>),假定在重新插入钥匙时超出以前的数值。 不过,为了我的目的,我不想夸大以前的价值,而是想给它增加一席。

显然,我可以写一个功能,检查已经存在的关键,说明其价值,增加1,并重新加入其中,但似乎奇怪的是,这种职能就包括了。 我失踪了吗? 这样做有什么办法?

最佳回答

使用具有违约价值的地图,然后更新+=

import scala.collection.mutable

val count = mutable.Map[String, Int]().withDefaultValue(0)
count("abc") += 1
println(count("abc"))
问题回答

如果该模块有说明,那么为什么不使用分单元

import Data.List.Split

let mywords = "he is a good good boy"
length $ nub $ splitOn " " mywords
5

如果你想要坚持Schala的不可改变的风格,你就可以用不可改变的语种创建自己的班子:

class CountMap protected(val counts: Map[String, Int]){
    def +(str: String) = new CountMap(counts + (str -> (counts(str) + 1)))
    def apply(str: String) = counts(str)
}

object CountMap {
    def apply(counts: Map[String, Int] = Map[String, Int]()) = new CountMap(counts.withDefaultValue(0))
}

之后,你可以使用:

val added = CountMap() + "hello" + "hello" + "world" + "foo" + "bar"
added("hello")
>>2
added("qux")
>>0

您也可添加<条码> 适用 随附物体上的有效载荷,以便你能够直接输入一系列字句,甚至一句:

object CountMap {
    def apply(counts: Map[String, Int] = Map[String, Int]()): CountMap = new CountMap(counts.withDefaultValue(0))
    def apply(words: Seq[String]): CountMap = CountMap(words.groupBy(w => w).map { case(word, group) => word -> group.length })
    def apply(sentence: String): CountMap = CountMap(sentence.split(" "))
}

那么,你会更容易:

CountMap(Seq("hello", "hello", "world", "world", "foo", "bar"))

或:

CountMap("hello hello world world foo bar")

updatedWith :

counters.updatedWith(key):
  case Some(old) => Some(old + new)
  case _ => Some(new)




相关问题
Which name for a "smart" dictionary (hashtable)?

I m looking for a good name for a custom dictionary which automatically initializes the value for a requested key if it doesn t exist, using a delegate. The indexer implementation should help ...

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

Python dictionary simple way to add a new key value pair

Say you have, foo = bar d = { a-key : a-value } And you want d = { a-key : a-value , foo : bar } e = { foo :foo} I know you can do, d[ foo ] = foo #Either of the following for e e = { foo :foo}...

Storing ints in a Dictionary

As I understand, in Objective-C you can only put Objects into dictionaries. So if I was to create a dictionary, it would have to have all objects. This means I need to put my ints in as NSNumber, ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...