I wanted to know if theres a way to override a method within the same class in scala.
class xyz {
def a() : Unit = {
var hello = "Hello"
}
def b() : Unit = {
//method to override the functionality of b, for example lets say I want it to just print "Hi, how is your day going" until its somehow reset and after its resett it should go back to doing var hello = "Hello"
}
}
def c() : Unit = {
//reset a to do what it was doing earlier (var hello = "Hello")
}
Basically I want to compute var hello = "Hello" whenever a() is called until b() is called and then a() should print "Hi, how is your day going" until its reset when c() is called and then it should go back to performing var hello = "Hello". Is there a way to use this, if not is there another way? I don't want to use conditionals. Thanks in advance.
So, basically you want to define a() to use a dynamic behaviour.
object Behave {
val helloComputeBehaviour: () => Unit =
() => {
// default behaviour
var hello = "Hello"
}
val printDayGreetingBehaviour: () => Unit =
() => {
// behaviour after switch
println("Hi, how is your day going")
}
var behaviour: () => Unit =
helloComputeBehaviour
def a(): Unit =
behaviour()
def b(): Unit = {
// switch behaviour
behaviour = printDayGreetingBehaviour
}
def c(): Unit = {
// go back to default behaviour
behaviour = helloComputeBehaviour
}
}
As someone who strongly prefers not to use vars, I do not think the following is elegant, but if vars are your cup of tea, you could do something like this:
class xyz {
private val resetHello: () => Unit = () => {
// set hello ...
}
private val printHi: () => Unit = () => {
// print "Hi..."
}
// variable holding the current behavior of def a()
private var behaviorOfA: () => Unit = resetHello
def a(): Unit = {
// execute the current behavior
behaviorOfA()
}
def b(): Unit = {
behaviorOfA = printHi
}
def c(): Unit = {
behaviorOfA = resetHello
}
}
Related
I'm new in Scala and i'm facing a few problems in my assignment :
I want to build a stream class that can do 3 main tasks : filter,map,and forEach.
My streams data is an array of elements. Each of the 3 main tasks should run in 2 different threads on my streams array.
In addition, I need to divde the logic of the action and its actual run to two different parts. First declare all tasks in stream and only when I run stream.run() I want the actual actions to happen.
My code :
class LearningStream[A]() {
val es: ExecutorService = Executors.newFixedThreadPool(2)
val ec = ExecutionContext.fromExecutorService(es)
var streamValues: ArrayBuffer[A] = ArrayBuffer[A]()
var r: Runnable = () => "";
def setValues(streamv: ArrayBuffer[A]) = {
streamValues = streamv;
}
def filter(p: A => Boolean): LearningStream[A] = {
var ls_filtered: LearningStream[A] = new LearningStream[A]()
r = () => {
println("running real filter..")
val (l,r) = streamValues.splitAt(streamValues.length/2)
val a:ArrayBuffer[A]=es.submit(()=>l.filter(p)).get()
val b:ArrayBuffer[A]=es.submit(()=>r.filter(p)).get()
ms_filtered.setValues(a++b)
}
return ls_filtered
}
def map[B](f: A => B): LearningStream[B] = {
var ls_map: LearningStream[B] = new LearningStream[B]()
r = () => {
println("running real map..")
val (l,r) = streamValues.splitAt(streamValues.length/2)
val a:ArrayBuffer[B]=es.submit(()=>l.map(f)).get()
val b:ArrayBuffer[B]=es.submit(()=>r.map(f)).get()
ls_map.setValues(a++b)
}
return ls_map
}
def forEach(c: A => Unit): Unit = {
r=()=>{
println("running real forEach")
streamValues.foreach(c)}
}
def insert(a: A): Unit = {
streamValues += a
}
def start(): Unit = {
ec.submit(r)
}
def shutdown(): Unit = {
ec.shutdown()
}
}
my main :
def main(args: Array[String]): Unit = {
var factorial=0
val s = new LearningStream[String]
s.filter(str=>str.startsWith("-")).map(s=>s.toInt*(-1)).forEach(i=>factorial=factorial*i)
for(i <- -5 to 5){
s.insert(i.toString)
}
println(s.streamValues)
s.start()
println(factorial)
}
The main prints only the filter`s output and the factorial isnt changed (still 1).
What am I missing here ?
My solution: #Levi Ramsey left a few good hints in the comments if you want to get hints and not the real solution.
First problem: Only one command (filter) run and the other didn't. solution: insert to the runnable of each command a call for the next stream via:
ec.submit(ms_map.r)
In order to be able to close all sessions, we need to add another LearningStream data member to the class. However we can't add just a regular LearningStream object because it depends on parameter [A]. Therefore, I implemented a trait that has the close function and my data member was of that trait type.
I have a tagless final implementation with unit test, when I run the unit test only the first step is invoked not the rest.
Here is the test target:
class NameThing[F[_]: Monad](implicit console: Console[F]) {
def program: F[Unit] = for {
_ <- console.prompt
rawName <- console.read
fullName = parse(rawName)
_ <- console.display(fullName)
} yield ()
def parse(rawName:String):FullName = {
val parts = rawName.split(" ")
FullName(parts(0), parts(1))
}
}
The unit test is:
implicit object TestConsole extends Console[Test] {
override def prompt: Test[Unit] = {
println("ok1")
Reader(TestEnv => TestEnv.prompt)
}
override def read: Test[String] = {
println("ok2")
Reader(TestEnv => TestEnv.read)
}
override def display(fullName: FullName): Test[Unit] = {
println("ok3")
Reader(TestEnv => TestEnv.display(fullName.toString))
}
}
val result = new NameThing[Test]().program.run
I only see ok1 displayed.
Complete code here: https://bitbucket.org/jameskingconsulting/scala-effects
Try
new NameThing[Test]().program.run(TestEnv())
new NameThing[Test]().program.run is just a TestEnv => Unit (where .run is Kleisli's run), you should call it on a TestEnv to actually run the program.
I am new to Scala, trying to understand the syntactic behaviors of Scala. I will be really appreciated if anybody help me. Thanks
With Anonymous Object: Here in this scenario if I want to print the value of resinside the main function body then what logic I need to apply?
package oops
object AnonymousObject
{
def main(args:Array[String]):Unit =
{
new student().detail(5,9) // Line 1
}
}
class student
{
def detail(x:Int, y:Int):Int =
{
val res = x*y
println(res)
}
}
Without Anonymous Object:
For more information, in this scenario given below, there has no problem to achieve it because of var s
class Student
{
var id:Int = 0; // All fields must be initialized
var name:String = null;
}
object MainObject
{
def main(args:Array[String])
{
var s = new Student() // Creating an object
println(s.id+" "+s.name);
}
}
An object which has no reference name. So simply you can print like this way inside the main
object AnonymousObject
{
def main(args:Array[String]):Unit =
{
val res = new student().detail(5,9)
println(res)
}
}
class student
{
def detail(x:Int, y:Int):Int =
{
x*y
}
}
Output: 45
Often we need objects that can be reused and takes some time to generate:
def foo() = {
// long lines of code...
val pattern = Pattern.comile("pattern") // suppose this takes long time
// use pattern
}
Then it can be moved to outer scope
private[this] lazy val pattern = Pattern.comile("pattern") // suppose this takes long time
def foo() = {
// long lines of code...
// pattern need to be available only here!
// use pattern
}
But this complicates the source because it leaks scope of the variable pattern, while it is used only in specific location of foo. I am curious about this can be simplified with a macro function:
def foo() = {
// long lines of code...
val pattern = singleton { Pattern.comile("pattern") }
// use pattern
}
If it is possible, we can extend it for more interesting case; ThreadLocal singleton:
def foo() = {
// long lines of code...
val obj = threadLocal { new NotThreadSafeObject() }
// use obj
}
Which will be extended as:
private[this] lazy val foo_obj_gen_by_macro = {
val tl = new ThreadLocal[NotThreadSafeObject]()
tl.set(new NotThreadSafeObject())
tl
}
def foo() = {
// long lines of code...
val obj = foo_obj_gen_by_macro.get
// use obj
}
If it would C++, this can be very easily achieved by using static variable inside of function scope:
void foo() {
// long lines of code...
static Pattern pattern = Pattern.Compile("pattern");
// use pattern
}
Why not just scope it ?
lazy val foo: () => String = {
val pattern = Pattern.compile("pattern")
def result(): String = ???
result _
}
Or as thoredge mentioned even simpler
lazy val foo: () => String = {
val pattern = Pattern.compile("pattern")
() => ???
}
I am not a Groovy expert, but I did read the book "Groovy in Action". In Groovy, each closure comes with a "context", where the items inside the closure can get access to pseudo-variables like "this", "owner", and "delegate", that let the items know who called the closure. This allows one to write DSLs like this (from Groovy in Action):
swing = new SwingBuilder()
frame = swing.frame(title:'Demo') {
menuBar {
menu('File') {
menuItem 'New'
menuItem 'Open'
}
}
panel {
// ...
}
}
Note that 'menuBar' "knows" that it belongs to 'frame' because it can get context information about the owner and delegate of the closure.
Is this possible to do in Scala? If so, how?
One way is to use a scala.util.DynamicVariable to track the context. Something like the SwingBuilder could be implemented as
import scala.util.DynamicVariable
import javax.swing._
object SwingBuilder {
case class Context(frame: Option[JFrame], parent: Option[JComponent])
}
class SwingBuilder {
import SwingBuilder._
val context = new DynamicVariable[Context](Context(None,None))
def frame(title: String)(f: =>Unit) = {
val res = new JFrame(title)
res.add(new JPanel())
context.withValue(Context(Some(res),context.value.parent)){f;res}
}
def menuBar(f: =>Unit) = {
val mb = new JMenuBar()
context.value.frame.foreach(_.setJMenuBar(mb))
context.withValue(Context(context.value.frame,Some(mb))){f;mb}
}
def menu(title: String)(f: =>Unit) = {
val m = new JMenu(title)
context.value.parent.foreach(_.asInstanceOf[JMenuBar].add(m))
context.withValue(Context(context.value.frame,Some(m))){f;m}
}
def menuItem(title: String) = {
val mi = new JMenuItem(title)
context.value.parent.foreach(_.asInstanceOf[JMenu].add(mi))
}
}
object Test {
def main(args: Array[String]) {
val builder = new SwingBuilder()
import builder._
val f = frame("Demo") {
val mb = menuBar {
menu("File") {
menuItem("New")
menuItem("Open")
}
}
}
f.setVisible(true)
}
}