Scala - 如何定义地图,其中值取决于密钥?

时间:2015-02-18 12:21:30

标签: scala dictionary

有没有办法定义Map,其中Map值取决于其键,如

Map(key -> f(key), key2 -> f(key2), ...).

4 个答案:

答案 0 :(得分:10)

你看错了......

Map[K,V]也是Partialfunction[K,V]的一个实例。因此,将您使用Map类型(vals,方法参数等)的所有地方更改为PartialFunction

然后,您可以直接使用f,或者在密钥和值之间没有简单代数关系的地方提供Map[K,V]作为实例。

e.g。

def methodUsingMapping(x: PartialFunction[Int,Boolean]) = ...

//then
val myMap = Map(1->true, 2->true, 3->false)
methodUsingMapping(myMap)


//or
val isEven = PartialFunction(n: Int => n % 2 == 0)
methodUsingMapping(isEven)

//or
//note: case statements in a block is the smart way
//      to define a partial function
//      In this version, the result isn't even defined for odd numbers
val isEven: PartialFunction[Int,Boolean] = {
  case n: Int if n % 2 == 0 => true
}
methodUsingMapping(isEven)

您也可能还想考虑使用(K) => Option[V],在这种情况下,您可以通过lift方法提供该类型的实例,该方法映射来自PartialFunction的继承

e.g。

def methodUsingMapping(x: (Int)=>Option[Boolean]) = ...

//then
val myMap = Map(1->true, 2->true, 3->false)
methodUsingMapping(myMap.lift)

//or
def isEven(n: Int) = Some(n % 2 == 0)
methodUsingMapping(isEven)

//or
def isEven(n: Int) = n % 2 == 0
methodUsingMapping(x => Some(isEven(x)))

答案 1 :(得分:6)

假设您将密钥放在这样的列表中,并且您想将其转换为正方形作为值。

scala> val keyList = ( 1 to 10 ).toList
keyList: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> val doSquare = ( x: Int ) => x * x
doSquare: Int => Int = <function1>

// Convert it to the list of tuples - ( key, doSquare( key ) )
scala> val tupleList = keyList.map( key => ( key, doSquare( key ) ) )
tuple: List[(Int, Int)] = List((1,1), (2,4), (3,9), (4,16), (5,25), (6,36), (7,49), (8,64), (9,81), (10,100))

val keyMap = tuple.toMap
keyMap: scala.collection.immutable.Map[Int,Int] = Map(5 -> 25, 10 -> 100, 1 -> 1, 6 -> 36, 9 -> 81, 2 -> 4, 7 -> 49, 3 -> 9, 8 -> 64, 4 -> 16)

或者在一行中完成

( 1 to 10 ).toList.map( x => ( x, x * x ) ).toMap

或者......如果您只有几个键......那么您可以编写特定的代码

Map( 1 -> doSquare( 1 ), 2 -> doSquare( 2 ) )

答案 2 :(得分:4)

因为您只需要定义4个方法来实现Map特征实现,所以您可以自己动手:

trait MapWithRelationship[K, +V] extends Map[K, V] {
  self =>
  def pred: (K, Any) => Boolean
  def underlying: Map[K, V]
  def get(key: K): Option[V] = underlying.get(key)
  def iterator: Iterator[(K, V)] = underlying.iterator
  def + [V1 >: V](kv: (K, V1)): MapWithRelationship[K, V1] = {
    val (k, v) = kv
    if (pred(k, v)) {
      new MapWithRelationship[K, V1] {
        val pred = self.pred
        val underlying = self.underlying + kv
      }
    } else {
      throw new Exception(s"Key-value pair $kv failed MapWithRelationship predicate")
    }
  }
  def -(key: K): MapWithRelationship[K, V] =
    new MapWithRelationship[K, V] {
      val pred = self.pred
      val underlying = self.underlying - key
    }
}

object MapWithRelationship {
  def apply[K, V](rule: (K, Any) => Boolean)(pairs: (K, V)*) = {
    val empty = new MapWithRelationship[K, V] { 
      def pred = rule 
      def underlying = Map.empty[K, V]
    }
    pairs.foldLeft(empty)(_ + _)
  }
}

然后你可以这样使用:

scala> val x = MapWithRelationship[Int, Int]((k, v) => v == k * k)()
x: MapWithRelationship[Int,Int] = Map()

scala> val x2 = x + (1 -> 1)
x2: MapWithRelationship[Int,Int] = Map(1 -> 1)

scala> val x3 = x + (5 -> 25)
x3: MapWithRelationship[Int,Int] = Map(5 -> 25)

scala> val x4 = x + (6 -> "foo")
java.lang.Exception: Key-value pair (6,foo) failed MapWithRelationship predicate
  at MapWithRelationship$class.$plus(<console>:21)
  at MapWithRelationship$$anon$3.$plus(<console>:33)
  ... 32 elided

答案 3 :(得分:1)

您可以使用以下方法制作无限的方块图:

val mySquareMap = Map.empty[Int, Int].withDefault(d => d * d)

此地图仍有+getiterator以及其他无法正常工作的方法,但如果您需要只读地图返回方格,会工作。

当然,使用它会更有效,也可能更清晰:

val mySquare = (d:Int) => d * d

作为一种功能。但是,如果您需要使用某些需要该类型的API,则上述Map可能会有用。

为了拥有一个更加完善的解决方案,你可能最好建立一个扩展Map[Int, Int]的类来覆盖get以返回其参数的平方。