I want to sum the Map("one" -> 2, "two" -> 3, "three" -> 4)'s values 2+3+4, of course I can use the following methods:
Map("one" -> 2, "two" -> 3, "three" -> 4).foldLeft(0)(_ + _._2)
Map("one" -> 2, "two" -> 3, "three" -> 4).values.sum()
I found Map has a another more direct API sum: def sum: A, but, I don't search any example about this API, How to use it?
For sum[B >: (A, B)](implicit num: Numeric[B]): B, as you see, it needs the implicit parameter and the parameter type is Numeric[B] type.
Scala Numeric is trait that defined a series of math operation.
http://www.scala-lang.org/api/current/scala/math/Numeric.html
for your case, the type is Map[String, Int], so you need to implement the implicit for Numeric[(String, Int)] for sum method, like:
trait MyTupleNumeric extends Numeric[(String, Int)] {
def plus(x: (String, Int), y: (String, Int)) = ("", x._2 + y._2)
override def minus(x: (String, Int), y: (String, Int)): (String, Int) = ("", x._2 - x._2)
override def times(x: (String, Int), y: (String, Int)): (String, Int) = ("", x._2 * y._2)
override def negate(x: (String, Int)): (String, Int) = ("", -x._2)
override def fromInt(x: Int): (String, Int) = ("", x)
override def toInt(x: (String, Int)): Int = x._2
override def toLong(x: (String, Int)): Long = x._2.toLong
override def toFloat(x: (String, Int)): Float = x._2.toFloat
override def toDouble(x: (String, Int)): Double = x._2.toDouble
override def compare(x: (String, Int), y: (String, Int)): Int = x._2 - y._2
}
implicit object MyTupleNumericImplicit extends MyTupleNumeric
val f = implicitly[Numeric[(String, Int)]] // implicitly is used find implicits base on the type
println(f.plus(("one", 2), ("two", 3)))
val r = Map("one" -> 2, "two" -> 3, "three" -> 4)
println(r.sum._2)
println(r.sum(MyTupleNumericImplicit))
As the above code, we have implement own Numeric type with (String, Int), and implements methods.
And we implicit this into our scope, so we can use implicitly to get the function and call.
And the sum method also could find the implicit parameter for Numeric[(String, Int)]
The Map.sum method doesn't do what you want -- your functions look good as they are.
The reason why Map has a sum method is that all TraversableOnce classes have a sum method, which only works if the collection type is numeric. However, a Map[K, V] is a TraversableOnce[(K, V)], so this approach won't work (the key-value tuple is not a numeric type).
Related
I am trying to convert implicitly a List[(Int, String)] to List[(IntWrap, String)], which is giving an error of TypeMismatch.
I tried few other conversion which works which are
List[Int] to List[IntWrap] and Tuple2[Int, String] to Tuple2[IntWrap, String] which works.
case class IntWrap(a : Int)
implicit def int2IntWrap(x: Int) = IntWrap(x)
implicit def int2IntWrapForTuple2(tuple: Tuple2[Int, String]) = (IntWrap(tuple._1), tuple._2)
//Defined few methods
def foo(t : (IntWrap, String)) = println(t._2)
def foo_list(t: List[IntWrap]) = println(t.size)
def foo_list_tuple(t : List[(IntWrap, String)]) = println(t.size)
foo(3 -> "hello") //this works
foo_list(List(1, 2, 3)) //this works
val l : List[(IntWrap, String)] = List(3 -> "hello")
foo_list_tuple(l) //this works
val l1 = List(3 -> "hello")
foo_list_tuple(l1) //this one doesn't work
//error: type mismatch; found: List[(Int, String)] required: List[(IntWrap, String)]
How can I make this work?
Try defining implicit conversion like so
implicit def listOfTupleToListOfIntWrapTuple(l: List[(Int, String)]): List[(IntWrap, String)] =
l.map(tuple => (IntWrap(tuple._1), tuple._2))
Let's say I have:
def foo(i: Int, s: String)
and have:
val tuple: (Int, String) = (1, "s")
can I pass tuple to foo without adding a wrapper for foo?
Yes, its possible. Using .tupled one can convert the lambda into accepting the tuple as argument.
Scala REPL
scala> def foo(i: Int, s: String): Int = i
foo: (i: Int, s: String)Int
scala> (foo _).tupled
res3: ((Int, String)) => Int = scala.Function2$$Lambda$226/234698513#45984654
scala> val tuple: (Int, String) = (1, "s")
tuple: (Int, String) = (1,s)
scala> (foo _).tupled(tuple)
res5: Int = 1
foo(tuple._1, tuple._2) should work.
If you want something more maintainable, I would recommend following:
type MyTuple = (Int, String)
def foo(t:MyTuple) = ??? // some code
val tuple = (1, "s")
foo(tuple) // works
Also inside foo, the best way to unwrap the tuple would be
val (int, string) = t
I tried implicit conversions in the following example:
val m: Map[Int, Int] = Map(10 -> "asd") //fine
val mm: Map[Int, Int] = Map("asd" -> 20) //type mismatch; found: (String, Int)
//required: (Int, Int)
implicit def stringToInt(str: String): Int = 10
Why can't we apply implicit conversions to map keys? Is there a way to work around this?
It doesn't work because you're using -> which is an (inline) operator:
implicit final class ArrowAssoc[A](self : A) extends scala.AnyVal {
#scala.inline
def ->[B](y : B) : scala.Tuple2[A, B] = { /* compiled code */ }
def →[B](y : B) : scala.Tuple2[A, B] = { /* compiled code */ }
}
You can see that by the time B is evaluated, A is already "fixed". Let's just say that you can only (implicitly) convert the right hand side of a tuple when using -> operator:
implicit def stringToInt(str: String): Int = 10
implicit def intToStr(str: Int): String = "a"
val a: Map[Int, Int] = Map(10 -> "asd") //fine
val b: Map[Int, Int] = Map("asd" -> 20) // error! cant change left side
val c: Map[String, String] = Map("asd" -> 20) // fine
val d: Map[String, String] = Map(10 -> "asd") // error! cant change left side
Because of similar compiler quirks related to using operator ->, #Jorg's solution works in one direction, but not the other:
implicit def tupleIntifier(t: (String, Int)) = (10, 10)
implicit def tupleIntifier2(t: (Int, String)) = (10, 10)
val a: Map[Int, Int] = Map("asd" -> 20) // uses tupleIntifier
val b: Map[Int, Int] = Map(10 -> "asd") // fails!!
However, if you avoid using -> operator altogether and simply use (key, value) syntax, it will work:
val a: Map[Int, Int] = Map((10, "asd"))
val b: Map[Int, Int] = Map(("asd", 20))
implicit def stringToInt(str: String): Int = 15
println(a) // prints Map(10 -> 15)
println(b) // prints Map(15 -> 20)
Please, look at the error message you are getting:
error: type mismatch;
found : (String, Int)
required: (Int, Int)
val mm: Map[Int, Int] = Map("asd" -> 20)
^
The error message is not about String instead of Int, it is about (String, Int) instead of (Int, Int). So, you are simply converting the wrong thing:
implicit def tupleIntifier[T](t: (String, T)) = (10, t._2)
val mm: Map[Int, Int] = Map("asd" -> 20)
//=> mm: Map[Int,Int] = Map(10 -> 20)
Voila! It works.
If you were to add such a general implicit conversion, you would lose the type-safety that Scala is enforcing, because any String would become an Int as needed, anywhere, without intervention from the programmer.
In reality, when you want to create that map from other data, you probably already know the data types of that other data. So if the keys are known to be integers, convert them to Int and use them like that. Otherwise, use strings.
Your example is highly artificial. Which concrete problem are you trying to solve?
val list = List((1,2), (3,4))
list.map(tuple => {
val (a, b) = tuple
do_something(a,b)
})
// the previous can be shortened as follows
list.map{ case(a, b) =>
do_something(a,b)
}
// similarly, how can I shorten this (and avoid declaring the 'tuple' variable)?
def f(tuple: (Int, Int)) {
val (a, b) = tuple
do_something(a,b)
}
// here there two ways, but still not very short,
// and I could avoid declaring the 'tuple' variable
def f(tuple: (Int, Int)) {
tuple match {
case (a, b) => do_something(a,b)
}
}
def f(tuple: (Int, Int)): Unit = tuple match {
case (a, b) => do_something(a,b)
}
Use tupled
scala> def doSomething = (a: Int, b: Int) => a + b
doSomething: (Int, Int) => Int
scala> doSomething.tupled((1, 2))
res0: Int = 3
scala> def f(tuple: (Int, Int)) = doSomething.tupled(tuple)
f: (tuple: (Int, Int))Int
scala> f((1,2))
res1: Int = 3
scala> f(1,2) // this is due to scala auto-tupling
res2: Int = 3
tupled is defined for every FunctionN with N >= 2, and returns a function expecting the parameters wrapped in a tuple.
While this might look like a trivial suggestion, the f function, can be further simplified by just using _1 and _2 on a tuple.
def f(tuple: (Int, Int)): Unit =
do_something(tuple._1, tuple._2)
Obviously by doing this you're affecting readability (some meta-information about the meaning of the 1st and 2nd parameter of the tuple is removed) and should you wish to use elements of the tuple somewhere else in the f method you will need to extract them again.
Though for many uses this might be still the easiest, shortest and most intuitive alternative.
If I understand correctly you are trying to pass a tuple to a method with 2 args?
def f(tuple: (Int,Int)) = do_something(tuple._1, tuple._2)
by more readable, I mean giving variable names instead of using the _1 an _2 on the tuple
In this case, it's a good idea to use a case class instead of a tuple, especially since it only takes one line:
case class IntPair(a: Int, b: Int)
def f(pair: IntPair) = do_something(pair.a, pair.b)
If you get (Int, Int) from external code which can't be changed (or you don't want to change), you could add a method converting from a tuple to IntPair.
Another option: {(a: Int, b: Int) => a + b}.tupled.apply(tuple). Unfortunately, {case (a: Int, b: Int) => a + b}.apply(tuple) doesn't work.
Is there a way to destructure input parameters of a function in Scala (akin to Clojure)?
So, instead of
scala> def f(p: (Int, Int)) = p._1
f: (p: (Int, Int))Int
I'd like to have this (it doesn't work):
scala> def f((p1, p2): (Int, Int)) = p1
I guess in scala you would use pattern matching to achieve the same, e.g. like this:
val f: (Int, Int) => Int = { case (p1, p2) => p1 }
Or, equivalently:
def f(p: (Int, Int)) = p match { case(p1, p2) => p1 }
If the types can be inferred, the (Int, Int) => Int can be dropped:
List((1, 2), (3, 4)) map { case (p1, p2) => p1 }
def f(p: ((Int, Int), (Int, Int))) = p._1 > f: (p: ((Int, Int), (Int, Int)))(Int, Int)
f((1,2), (3,4)) > res1: (Int, Int) = (1,2)