Scala: generics and implicit - scala

I have to develop class StackMachine[T]. If T = Boolean, then there should be logical operations. If T = Int,Double,Long and etc. there should be ariphmetic operations. Firstly i developed class Stack[T].
class Stack[T](val stack: List[T]) {
val length: Int = stack.length
def isEmpty: Boolean = {length == 0}
def push(x: T): Stack[T] = {
new Stack[T](x :: stack)
}
def peak: T = {
if (this.isEmpty)
throw new ArrayIndexOutOfBoundsException
else stack.head
}
def pop(): Stack[T] = {
if (this.isEmpty)
throw new ArrayStoreException()
val x :: xs = stack
new Stack[T](xs)
}
The thin is that i dont know how to develop StackMachine[T] the presence of operations in which depends on the type.
I tried this:
case class StackMachine[T](val stack:Stack[T]){
def const(x: T): StackMachine[T] = {new StackMachine[T](new Stack[T](this.stack.push(x).stack))}
def dup: StackMachine[T] = {new StackMachine[T](new Stack[T](this.stack.push(this.stack.peak).stack))}
def swap: StackMachine[T] = {
val startStack = this.stack
val startPeak = startStack.peak
val secondStack = startStack.pop()
val secondPeak = secondStack.peak
val finalStack = secondStack.pop().push(startPeak)
StackMachine[T](stack)
}
def and(): StackMachine[Boolean] = {
val startStack = this.stack.asInstanceOf[Stack[Boolean]]
val startPeak = startStack.peak
val secondStack = startStack.pop()
val secondPeak = secondStack.peak
StackMachine[Boolean](new Stack[Boolean](secondStack.push(startPeak && secondPeak).stack))
}
def or: StackMachine[Boolean] = {
val startStack = this.stack.asInstanceOf[Stack[Boolean]]
val startPeak = startStack.peak
val secondStack = startStack.pop()
val secondPeak = secondStack.pop().peak
StackMachine[Boolean](new Stack[Boolean](secondStack.push(startPeak || secondPeak).stack))
}
def xor: StackMachine[Boolean] = {
val startStack = this.stack.asInstanceOf[Stack[Boolean]]
val startPeak = startStack.peak
val secondStack = startStack.pop()
val secondPeak = secondStack.pop().peak
StackMachine[Boolean](new Stack[Boolean](secondStack.push(startPeak ^ secondPeak).stack))
}
def sum(input : T)(implicit N: Numeric[T]) = {
val startStack = this.stack
val startPeak = startStack.peak
val secondStack = startStack.pop()
StackMachine[T](new Stack[T](secondStack.push(N.plus(startPeak,input)).stack))
}
def dif(input : T)(implicit N: Numeric[T]) = {
val startStack = this.stack
val startPeak = startStack.peak
val secondStack = startStack.pop()
StackMachine[T](new Stack[T](secondStack.push(N.minus(startPeak,input)).stack))
}
def mul(input : T)(implicit N: Numeric[T]) = {
val startStack = this.stack
val startPeak = startStack.peak
val secondStack = startStack.pop()
StackMachine[T](new Stack[T](secondStack.push(N.toDouble(startPeak).*(N.toDouble(input)).asInstanceOf[T]).stack))
}
def div(input : T)(implicit N: Numeric[T]) = {
val startStack = this.stack
val startPeak = startStack.peak
val secondStack = startStack.pop()
StackMachine[T](new Stack[T](secondStack.push(N.toDouble(startPeak)./(N.toDouble(input)).asInstanceOf[T]).stack))
}
def min(input : T)(implicit N: Numeric[T]) = {
val startStack = this.stack
val startPeak = startStack.peak
val secondStack = startStack.pop()
StackMachine[T](new Stack[T](secondStack.push(N.min(startPeak,input)).stack))
}
def max(input : T)(implicit N: Numeric[T]) = {
val startStack = this.stack
val startPeak = startStack.peak
val secondStack = startStack.pop()
StackMachine[T](new Stack[T](secondStack.push(N.max(startPeak,input)).stack))
}
}
But this is wrong, because operations shouldnt have input parameters because all variables have to be taken from Stack. More than that, this way i cant create diff and mul functions.
I thoght to make StackMachine[T] abstract and use imlplicit object, but failed because in that case, my functions cant return StackMachine. May be i just dont understand implicit well enough or there is another way of doing this?

Then yeah it seems the project is intended to be solved using a typeclass.
For example, see this small one for Boolean-like and:
sealed trait BehavesAsBoolean[T] {
def and(t1: T, t2: T): T
}
object BehavesAsBoolean {
implicit final val BooleanBehavesAsBoolean: BehavesAsBoolean[Boolean] =
new BehavesAsBoolean[Boolean] {
override def and(b1: Boolean, b2: Boolean): Boolean =
b1 && b2
}
}
final class StackMachine[T](stack: Stack[T]) {
def and(implicit ev: BehavesAsBoolean[T]): Option[StackMachine[T]] =
for {
// I changed the implementation of pop to return an Option[(T, Stack[T])]
(b1, s2) <- stack.pop
(b2, s3) <- s2.pop
} yield {
new StackMachine(s3.push(ev.and(b1, b2)))
}
}
Of course, you may still prefer to throw exceptions rather than using Option
Anyways, I hope this helps you to finish the code.
You can see the code running here.

Related

Scala - How to create a combine ArrayList of functions with and without parameters

Suppose I have to following:
def f1: Int ( or def f1(): Int )
def f2 (x: Int): Int
def f3 (x: Int): Int
def f4: Int
...
...
note: 'Int' here is just an example
I would like to do ....
class Container[T] {
val values = mutable.ListBuffer.empty[T => Int]
def addValue(value: T => Int) = values += v
def doSome(t: T): Int = values.foldLeft[Int](0){ (complete, v) => complete + v(t) }
}
val ContainerWithParam = new Container[Int]
val ContainerWithoutParam = new Container[???]
ContainerWithParam.addValue(f2)
ContainerWithoutParam.addValue(f1)
val result = ContainerWithParam.doSome(1000) + ContainerWithoutParam.doSome(???)
One solution is to use Option[Nothing]
class Container[T] {
val values = mutable.ListBuffer.empty[T => Int]
def addValue(value: T => Int) = values += v
def doSome(t: T): Int = values.foldLeft[Int](0){ (complete, v) => complete + v(t) }
}
def f1(nothing: Option[Nothing]): Int
val ContainerWithoutParam = new Container[Option[Nothing]]
ContainerWithoutParam.doSome(None)
but I think this is not a very clean and nice code...
If def f1: Int = ??? then ...
val containerWithParam = new Container[Int]
val containerWithoutParam = new Container[Unit]
containerWithParam.addValue(f2)
containerWithoutParam.addValue(_ => f1)
val result = containerWithParam.doSome(1000) +
containerWithoutParam.doSome(())
If def f1(): Int = ??? then .addValue(_ => f1()).
The answer from #jwvh is right, but as an alternative you can create a separate class for the case where the function does not have a parameter. This can re-use the original implementation.
class ContainerNoParam {
private val container = new Container[Unit];
def addValue(value: => Int): Unit = container.addValue(_ => value)
def doSome(): Int = container.doSome(())
}
val ContainerWithParam = new Container[Int]
val ContainerWithoutParam = new ContainerNoParam
ContainerWithParam.addValue(f2)
ContainerWithoutParam.addValue(f1)
val result = ContainerWithParam.doSome(1000) + ContainerWithoutParam.doSome()

Invoke private method in arbitrary scala object

Let's say I have a Scala object:
object SomeObject {
private def someMethod(msg: String): Unit = println(msg)
}
I can invoke someMethod with the following code:
import scala.reflect.runtime.{universe => ru}
import scala.reflect.ClassTag
def invokeObjectPrivateMethod[R](methodName: String, args: AnyRef*): R = {
val rm = ru.runtimeMirror(getClass.getClassLoader)
val instanceMirror = rm.reflect(SomeObject)
val methodSymbol = ru.typeOf[SomeObject.type].decl(ru.TermName(methodName)).asMethod
val method = instanceMirror.reflectMethod(methodSymbol)
method(args: _*).asInstanceOf[R]
}
invokeObjectPrivateMethod("someMethod", "it works")
But above I've hardcoded SomeObject into the function. What I'd really like is to pass an arbitrary object name/classtag/whatever so I can invoke a private function generically in ANY object.
These attempts have NOT worked:
// With explicit ClassTag parameter
def invokeObjectPrivateMethod2[R](classTag: ClassTag[_], methodName: String, args: AnyRef*): R = {
val rm = ru.runtimeMirror(getClass.getClassLoader)
val instanceMirror = rm.reflect(classTag)
val methodSymbol = ru.typeOf[classTag.type].decl(ru.TermName(methodName)).asMethod
val method = instanceMirror.reflectMethod(methodSymbol)
method(args: _*).asInstanceOf[R]
}
// This fails at runtime with: `scala.ScalaReflectionException: <none> is not a method`
invokeObjectPrivateMethod2[Unit](ClassTag(SomeObject.getClass), "someMethod", "it doesn't work")
// With implicit ClassTag/TypeTag
def invokeObjectPrivateMethod3[T: ClassTag, S: ru.TypeTag, R](methodName: String, args: AnyRef*)(implicit ct: ClassTag[T]): R = {
val rm = ru.runtimeMirror(getClass.getClassLoader)
val instanceMirror = rm.reflect(ct)
val methodSymbol = ru.typeOf[S].decl(ru.TermName(methodName)).asMethod
val method = instanceMirror.reflectMethod(methodSymbol)
method(args: _*).asInstanceOf[R]
}
// This fails at compile time: `not found: type SomeObject`
invokeObjectPrivateMethod3[SomeObject, SomeObject, Unit]("someMethod", "it also doesn't work")
I'm somewhat out of my depth, so what I'm trying with the 3rd option might not even make sense.
Thanks!
Try the following if you have String of object name
def invokeObjectPrivateMethod[R](objectName: String, methodName: String, args: AnyRef*): R = {
val rm = scala.reflect.runtime.currentMirror
val moduleSymbol = rm.staticModule(objectName)
val classSymbol = moduleSymbol.moduleClass.asClass
val moduleMirror = rm.reflectModule(moduleSymbol)
val objectInstance = moduleMirror.instance
val objectType = classSymbol.toType
val methodSymbol = objectType.decl(ru.TermName(methodName)).asMethod
val instanceMirror = rm.reflect(objectInstance)
val methodMirror = instanceMirror.reflectMethod(methodSymbol)
methodMirror(args: _*).asInstanceOf[R]
}
invokeObjectPrivateMethod("com.example.App.SomeObject", "someMethod", "it works") //it works
or if you have object ClassTag
def invokeObjectPrivateMethod[R](classTag: ClassTag[_], methodName: String, args: AnyRef*): R = {
val rm = scala.reflect.runtime.currentMirror
val clazz = classTag.runtimeClass
val classSymbol = rm.classSymbol(clazz)
val moduleSymbol = classSymbol.owner.info.decl(classSymbol.name.toTermName).asModule //see (*)
val moduleMirror = rm.reflectModule(moduleSymbol)
val objectInstance = moduleMirror.instance
val objectType = classSymbol.toType
val methodSymbol = objectType.decl(ru.TermName(methodName)).asMethod
val instanceMirror = rm.reflect(objectInstance)
val methodMirror = instanceMirror.reflectMethod(methodSymbol)
methodMirror(args: _*).asInstanceOf[R]
}
invokeObjectPrivateMethod(classTag[SomeObject.type], "someMethod", "it works") //it works
or if you have object TypeTag
def invokeObjectPrivateMethod[R](typeTag: TypeTag[_], methodName: String, args: AnyRef*): R = {
val rm = scala.reflect.runtime.currentMirror
val objectType = typeTag.tpe
val clazz = rm.runtimeClass(objectType) // see (**)
val classSymbol = rm.classSymbol(clazz)
val moduleSymbol = classSymbol.owner.info.decl(classSymbol.name.toTermName).asModule // see (*)
val moduleMirror = rm.reflectModule(moduleSymbol)
val objectInstance = moduleMirror.instance
val methodSymbol = objectType.decl(ru.TermName(methodName)).asMethod
val instanceMirror = rm.reflect(objectInstance)
val methodMirror = instanceMirror.reflectMethod(methodSymbol)
methodMirror(args: _*).asInstanceOf[R]
}
invokeObjectPrivateMethod(typeTag[SomeObject.type], "someMethod", "it works") //it works
or if you have object Class
def invokeObjectPrivateMethod[R](clazz: Class[_], methodName: String, args: AnyRef*): R = {
val rm = scala.reflect.runtime.currentMirror
val classSymbol = rm.classSymbol(clazz)
val moduleSymbol = classSymbol.owner.info.decl(classSymbol.name.toTermName).asModule //see (*)
val moduleMirror = rm.reflectModule(moduleSymbol)
val objectInstance = moduleMirror.instance
val objectType = classSymbol.toType
val methodSymbol = objectType.decl(ru.TermName(methodName)).asMethod
val instanceMirror = rm.reflect(objectInstance)
val methodMirror = instanceMirror.reflectMethod(methodSymbol)
methodMirror(args: _*).asInstanceOf[R]
}
invokeObjectPrivateMethod(SomeObject.getClass, "someMethod", "it works") //it works
or if you have object Type
def invokeObjectPrivateMethod[R](typ: Type, methodName: String, args: AnyRef*): R = {
val rm = scala.reflect.runtime.currentMirror
val classSymbol = typ.typeSymbol.asClass
val moduleSymbol = classSymbol.owner.info.decl(classSymbol.name.toTermName).asModule //see (*)
val moduleMirror = rm.reflectModule(moduleSymbol)
val objectInstance = moduleMirror.instance
val methodSymbol = typ.decl(ru.TermName(methodName)).asMethod
val instanceMirror = rm.reflect(objectInstance)
val methodMirror = instanceMirror.reflectMethod(methodSymbol)
methodMirror(args: _*).asInstanceOf[R]
}
invokeObjectPrivateMethod(typeOf[SomeObject.type], "someMethod", "it works") //it works
(*) Get the module symbol, given I have the module class, scala macro
(**) How to get ClassTag form TypeTag, or both at same time?
I assume that the method didn't have overloaded versions, otherwise you should work with typ.decl(...).alternatives.find(...).get.asMethod or typ.decl(...).alternatives.head.asMethod.
Using Java reflection
def invokeObjectPrivateMethod[R](packageName: String, objectName: String, methodName: String, args: AnyRef*): R = {
val javaClassName = s"$packageName.${objectName.replace('.', '$')}$$"
val clazz = Class.forName(javaClassName)
val method = clazz.getDeclaredMethods.find(_.getName == methodName).get
method.setAccessible(true)
val field = clazz.getField("MODULE$")
val instance = field.get(null) // null because field is static
method.invoke(instance, args: _*).asInstanceOf[R]
}
invokeObjectPrivateMethod("com.example", "App.SomeObject", "someMethod", "it works") //it works
Using Java method handles
def invokeObjectPrivateMethod[R](packageName: String, objectName: String, methodName: String, args: AnyRef*): R = {
val javaClassName = s"$packageName.${objectName.replace('.', '$')}$$"
val clazz = Class.forName(javaClassName)
val lookup = MethodHandles.lookup
val field = clazz.getField("MODULE$")
val fieldMethodHandle = lookup.unreflectGetter(field)
val instance = fieldMethodHandle.invokeWithArguments()
val method = clazz.getDeclaredMethods.find(_.getName == methodName).get
method.setAccessible(true)
val methodHandle = lookup.unreflect(method)
methodHandle.invokeWithArguments(instance +: args : _*).asInstanceOf[R]
}
invokeObjectPrivateMethod("com.example", "App.SomeObject", "someMethod", "it works") //it works

writing a custom get method for a scala map

I have a map which is something like
val m = Map("foo" -> "bar", "faz" -> "baz")
I need to write a custom get method, so that the key can be the key in the map with a number in the end.
So for example:
m.get("foo1") should return "bar"
I am looking for a good scala pattern to solve this problem.
Also I am generating the above map from a for loop using yield, so I can't do something like this
val m = CustomMap("foo" -> "bar")
Any solutions will be appreciated.
Thanks
First of all, you can generate a map from a for comprehension, and then convert it to CustomMap. You just need to define a
def apply(map: Map[String, String]) = CustomMap(map.toSeq :_*) in CustomMap - then you can do val m = CustomMap( for { ... } yield ... )
Secondly, if it doesn't have to be named get (it probably shouldn't be anyway), you can do this sort of thing with an implicit:
object PimpMyMap {
val pref = ".*?(\\d+)".r
implicit class Pimped[V](val map: Map[String,V]) extends AnyVal {
def getPrefix(key: String): Option[V] = map.get(key).orElse { key match {
case pref(k) => map.get(k)
case _ => None
}
}
Now you can write things like:
import PimpMyMap._
val map = Map("foo" -> 1)
val one = map.getPrefix("foo123") // Some(1)
val anotherOne = map.getPrefix("foo") // also Some(1);
You can do this with an implicit class and implicit conversion:
import scala.language.implicitConversions
object MapHelpers {
implicit def optionStringToString(maybeS: Option[String]): String = maybeS.getOrElse("")
implicit class MapWithIntKey(val m: Map[String, String]) extends Map[String, String] {
override def get(key: String): Option[String] = {
val intRegex = """(\d+)""".r
val keyWithoutInt = intRegex
.findFirstMatchIn(key)
.map(int => {
val idx = key.indexOf(int.toString)
key.slice(0, idx)
})
.getOrElse(key)
m.get(keyWithoutInt)
}
def +[V1 >: String](
kv: (String, V1)): scala.collection.immutable.Map[String, V1] = m + kv
def -(key: String): scala.collection.immutable.Map[String, String] = m - key
def iterator: Iterator[(String, String)] = m.iterator
}
}
object App {
import MapHelpers._
def testMapImplicit(): Unit = {
val myMap: MapWithIntKey = Map("foo" -> "bar", "faz" -> "baz")
val result: String = myMap.get("foo1")
println("result", result) // bar
}
}
Working Scastie
If you have a sure way to get the real key from the fake key, you can do this with Map.withDefault:
class CustomMap[K, +V] private (underlying: Map[K, Option[V]]) {
def get(k: K): Option[V] = underlying(k)
}
object CustomMap {
def apply[K, V](original: Map[K, V], keyReducer: K => K) = new CustomMap(originalMap.
mapValues(Some(_)).
withDefault(k => originalMap.get(keyReducer(k))
)
}
In your case, you can use this with
val stringKeyReducer: String => String = k.reverse.dropWhile(_.isDigit).reverse
to drop the digits at the end of your strings, so
CustomMap(Map("foo" -> "bar"), stringKeyReducer).get("foo1") = Some("bar")
Here is solution which combines both the answers.
import scala.language.implicitConversions
object MapHelpers {
implicit def optionStringToString(maybeS: Option[String]): String = maybeS.getOrElse("")
implicit class MapWithIntKey(val m: Map[String, String]) extends Map[String, String] {
override def get(key: String): Option[String] = {
val prefix = "(.*?)\\d+".r
m.get(key).orElse{
key match {
case prefix(p) => m.get(p)
case _ => None
}
}
}
def +[V1 >: String](kv: (String, V1)): scala.collection.immutable.Map[String, V1] = m + kv
def -(key: String): scala.collection.immutable.Map[String, String] = m - key
def iterator: Iterator[(String, String)] = m.iterator
}
}
object App {
import MapHelpers._
def testMapImplicit(): Unit = {
val myMap: MapWithIntKey = Map("foo" -> "bar", "faz" -> "baz")
println("result - number match ", myMap.get("foo1"))
println("result - exact match ", myMap.get("foo"))
}
}
App.testMapImplicit()
Working Scastie

Extending a SortedMap in Scala

I'm trying to extend a SortedMap in Scala, but I'm having some problems with SortedMapLike and canBuildFrom (the last one I can't even type it correctly). Here's some code; first the companion object:
object Timeline {
...
def newBuilder[A]: Builder[(Long, A), Timeline[A]] =
new ListBuffer[(Long, A)] mapResult fromSeq
def fromSeq[A](buf: Seq[(Long, A)]): Timeline[A] =
new Timeline(buf toMap)
def empty[A] = Timeline[A](Map[Long, A]())
}
Then the class (yes, all my Timelines are from Long to A):
final class Timeline[A] private(t: Map[Long, A])
extends SortedMap[Long, A]
with SortedMapLike[Long, A, Timeline[A]] {
private[this] lazy val iMap =
TreeMap(t.toArray: _*)(Ordering.fromLessThan[Long](_ > _))
override def newBuilder: Builder[(Long, A), Timeline[A]] = Timeline.newBuilder
override def empty: Timeline[A] = Timeline.empty
def -(key: Long) = Timeline(iMap - key)
def get(key: Long) = iMap.get(key)
def rangeImpl(from: Option[Long], until: Option[Long]) =
Timeline(iMap.rangeImpl(from, until))
def iterator = iMap.iterator
def ordering = iMap.ordering
}
I'm not sure all the above is the correct way to achieve this, but now comes the part I can't even type correctly:
implicit def canBuildFrom[A]: CanBuildFrom[Timeline[A], A, Timeline[A]] =
new CanBuildFrom[Timeline[A], A, Timeline[A]] {
def apply(): Builder[(Long, A), Timeline[A]] = newBuilder[A]
def apply(from: Timeline[A]): Builder[(Long, A), Timeline[A]] = newBuilder[A]
}
It seems I was incorrectly typing canBuildFrom:
implicit def canBuildFrom[A]: CanBuildFrom[Timeline[A], (Long, A), Timeline[A]] =
new CanBuildFrom[Timeline[A], (Long, A), Timeline[A]] {
def apply(): Builder[(Long, A), Timeline[A]] = newBuilder[A]
def apply(from: Timeline[A]): Builder[(Long, A), Timeline[A]] = newBuilder[A]
}

How to write a proper null-safe coalescing operator in scala?

Having seen the answers coming out of questions like this one involving horror shows like trying to catch the NPE and dredge the mangled name out of the stack trace, I am asking this question so I can answer it.
Comments or further improvements welcome.
Like so:
case class ?:[T](x: T) {
def apply(): T = x
def apply[U >: Null](f: T => U): ?:[U] =
if (x == null) ?:[U](null)
else ?:[U](f(x))
}
And in action:
scala> val x = ?:("hel")(_ + "lo ")(_ * 2)(_ + "world")()
x: java.lang.String = hello hello world
scala> val x = ?:("hel")(_ + "lo ")(_ => (null: String))(_ + "world")()
x: java.lang.String = null
Added orElse
case class ?:[T](x: T) {
def apply(): T = x
def apply[U >: Null](f: T => U): ?:[U] =
if (x == null) ?:[U](null)
else ?:[U](f(x))
def orElse(y: T): T =
if (x == null) y
else x
}
scala> val x = ?:(obj)(_.subField)(_.subSubField).orElse("not found")
x: java.lang.String = not found
Or if you prefer named syntax as opposed to operator syntax
case class CoalesceNull[T](x: T) {
def apply(): T = x
def apply[U >: Null](f: T => U): CoalesceNull[U] =
if (x == null) CoalesceNull[U](null)
else CoalesceNull[U](f(x))
def orElse(y: T): T =
if (x == null) y
else x
}
scala> val x = CoalesceNull(obj)(_.subField)(_.subSubField).orElse("not found")
x: java.lang.String = not found
More examples
case class Obj[T](field: T)
test("last null") {
val obj: Obj[Obj[Obj[Obj[String]]]] = Obj(Obj(Obj(Obj(null))))
val res0 = CoalesceNull(obj)(_.field)(_.field)(_.field)(_.field)()
res0 should === (null)
val res1 = CoalesceNull(obj)(_.field)(_.field)(_.field)(_.field).orElse("not found")
res1 should === ("not found")
}
test("first null") {
val obj: Obj[Obj[Obj[Obj[String]]]] = null
val res0 = CoalesceNull(obj)(_.field)(_.field)(_.field)(_.field)()
res0 should === (null)
val res1 = CoalesceNull(obj)(_.field)(_.field)(_.field)(_.field).orElse("not found")
res1 should === ("not found")
}
test("middle null") {
val obj: Obj[Obj[Obj[Obj[String]]]] = Obj(Obj(null))
val res0 = CoalesceNull(obj)(_.field)(_.field)(_.field)(_.field)()
res0 should === (null)
val res1 = CoalesceNull(obj)(_.field)(_.field)(_.field)(_.field).orElse("not found")
res1 should === ("not found")
}
test("not null") {
val obj: Obj[Obj[Obj[Obj[String]]]] = Obj(Obj(Obj(Obj("something"))))
val res0 = CoalesceNull(obj)(_.field)(_.field)(_.field)(_.field)()
res0 should === ("something")
val res1 = CoalesceNull(obj)(_.field)(_.field)(_.field)(_.field).orElse("not found")
res1 should === ("something")
}
}