Showing posts with label Scala. Show all posts
Showing posts with label Scala. Show all posts

Friday, January 1, 2021

Scala REPL no echo on input Ask Question

 This seems to be an issue with JLine2 being built with JDK9+, but being used on JDK8. If you're on bionic, try this PPA for jline2: https://launchpad.net/~lokkju/+archive/ubuntu/java-compat/

(or just download and install https://launchpad.net/~lokkju/+archive/ubuntu/java-compat/+build/16458066/+files/libjline2-java_2.14.6-1ubuntu1~bionicppa1_all.deb via dpkg)





You can verify jline2 is the problem by running scala -Ydebug, and looking for ByteBuffer class not found error.




Friday, November 27, 2020

Extracting structure failed sbt task failed, see log for details

 Step 1: 

we have check the installed sbt version . 

  if sbt installed version on out laptop is say : 1.3.13 

[info] welcome to sbt 1.3.13 (Ubuntu Java 11.0.9.1)


then we have to use the same version in  build.properties in  sbt project in Eclipse or IntelliJ


when both are same , we will not get the mentioned error.


first Time when we are compiling sbt better to use  command line .

like : 

Need to run the following commands in the same sequence.

sbt 

clean

complie   



Friday, June 5, 2020

Scala foldLeft and foldRight

According to my experience, one of the best ways to workout the intuition is to see how it works on the very simple examples:
List(1, 3, 8).foldLeft(100)(_ - _) : Explanation:((100 - 1) - 3) - 8 == 88
List(1, 3, 8).foldRight(100)(_ - _): Explanation:  1 - (3 - (8 - 100)) == -94
As you can see, foldLeft/Right just passes the element of the list and the result of the previous application to the the operation in second parentheses. It should be also mentioned that if you apply these methods to the same list, they will return equal results only if the applied operation is associative.
More examples
class Foo(val name: String, val age: Int, val sex: Symbol)

object Foo {
  def apply(name: String, age: Int, sex: Symbol) = new Foo(name, age, sex)
}

val fooList = Foo("Hugh Jass", 25, 'male) ::
              Foo("Biggus Dickus", 43, 'male) ::
              Foo("Incontinentia Buttocks", 37, 'female) ::
              Nil
              
              
val stringListFoldRight = fooList.foldRight(List[String]()) { ( f,z) =>
  val title = f.sex match {
    case 'male => "Mr."
    case 'female => "Ms."
  }
  z :+ s"$title ${f.name}, ${f.age}"
}

println(stringListFoldRight) 
//List(Ms. Incontinentia Buttocks, 37, Mr. Biggus Dickus, 43, Mr. Hugh Jass, 25)

val stringListFoldLeft = fooList.foldLeft(List[String]()) { (z,f) =>
  val title = f.sex match {
    case 'male => "Mr."
    case 'female => "Ms."
  }
  z :+ s"$title ${f.name}, ${f.age}"
}

println(stringListFoldLeft) 
//List(Mr. Hugh Jass, 25, Mr. Biggus Dickus, 43, Ms. Incontinentia Buttocks, 37)



Monday, May 18, 2020

Call by name vs call by value in Scala, clarification

First, let's assume we have a function with a side-effect. This function prints something out and then returns an Int.

def something() = {
  println("calling something")
  1 // return value
}

Now we are going to define two function that accept Int arguments that are exactly the same except that one takes the argument in a call-by-value style (x: Int) and the other in a call-by-name style (x: => Int).

def callByValue(x: Int) = {
  println("x1=" + x)
  println("x2=" + x)
}

def callByName(x: => Int) = {
  println("x1=" + x)
  println("x2=" + x)
}

Now what happens when we call them with our side-effecting function?

scala> callByValue(something())
calling something
x1=1
x2=1

scala> callByName(something())
calling something
x1=1
calling something
x2=1

So you can see that in the call-by-value version, the side-effect of the passed-in function call (something()) only happened once. However, in the call-by-name version, the side-effect happened twice.

This is because call-by-value functions compute the passed-in expression's value before calling the function, thus the same value is accessed every time. Instead, call-by-name functions recompute the passed-in expression's value every time it is accessed.


Another Example:

Imagine you want to build a "nagger app" that will Nag you every time since time last you got nagged.

Examine the following implementations:

object main  {

    def main(args: Array[String]) {

        def onTime(time: Long) {
            while(time != time) println("Time to Nag!")
            println("no nags for you!")
        }

        def onRealtime(time: => Long) {
            while(time != time) println("Realtime Nagging executed!")
        }

        onTime(System.nanoTime())
        onRealtime(System.nanoTime())
    }
}

In the above implementation the nagger will work only when passing by name the reason is that, when passing by value it will re-used and therefore the value will not be re-evaluated while when passing by name the value will be re-evaluated every time the variables is accessed


Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...