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

How to setup Scala notebook

How to setup Scala notebook

We need the following tools

Git to do the clone of below repo
https://github.com/Bridgewater/scala-notebook

Sbt

Download it from

http://www.scala-sbt.org/download.html

After that go to place where you cloned the scala-notebook

And type the full path to place where you extracted sbt

Example command is

cd D:\Users\d760770\Desktop\Jagat\dev\code\open\scala-notebook

Then run sbt

D:\Users\d760770\Desktop\Jagat\dev\tools\sbt-0.13.7\sbt\bin\sbt

After this sbt will take sometime to download all dependencies

When its done , just start the web based notebook

>project server
>run

Wait for sometime

The browser url is

http://127.0.0.1:8899

Enjoy Scala notebook

Eclipse sbt java.lang.NoSuchMethodError: scalaz.Scalaz$.ToValidationV

Hi,
You will get the below error if we dont keep plugins.sbt in correct place.
Documenting for helping others not to waste time.
Eclipse 4.4
java.lang.NoSuchMethodError: scalaz.Scalaz$.ToValidationV(Ljava/lang/Object;)Lscalaz/syntax/ValidationV;  
Added the required plugin settings in below location should fix the error
/.sbt/0.13/plugins/plugins.sbt
full log is below.
[info] About to create Eclipse project files for your project(s).  java.lang.NoSuchMethodError: scalaz.Scalaz$.ToValidationV(Ljava/lang/Object;)Lscalaz/syntax/ValidationV;          at com.typesafe.sbteclipse.core.package$.setting(package.scala:66)          at com.typesafe.sbteclipse.core.Eclipse$.skipParents(Eclipse.scala:495)          at com.typesafe.sbteclipse.core.Eclipse$$anonfun$action$1.apply$mcZ$sp(Eclipse.scala:103)          at com.typesafe.sbteclipse.core.Eclipse$$anonfun$action$1.apply(Eclipse.scala:103)          at com.typesafe.sbteclipse.core.Eclipse$$anonfun$action$1.apply(Eclipse.scala:103)          at scala.Option.getOrElse(Option.scala:120)          at com.typesafe.sbteclipse.core.Eclipse$.action(Eclipse.scala:103)          at com.typesafe.sbteclipse.core.Eclipse$$anonfun$eclipseCommand$2.apply(Eclipse.scala:82)          at com.typesafe.sbteclipse.core.Eclipse$$anonfun$eclipseCommand$2.apply(Eclipse.scala:82)          at sbt.Command$$anonfun$applyEffect$1$$anonfun$apply$2.apply(Command.scala:60)          at sbt.Command$$anonfun$applyEffect$1$$anonfun$apply$2.apply(Command.scala:60)          at sbt.Command$$anonfun$applyEffect$2$$anonfun$apply$3.apply(Command.scala:62)          at sbt.Command$$anonfun$applyEffect$2$$anonfun$apply$3.apply(Command.scala:62)          at sbt.Command$.process(Command.scala:95)          at sbt.MainLoop$$anonfun$1$$anonfun$apply$1.apply(MainLoop.scala:87)          at sbt.MainLoop$$anonfun$1$$anonfun$apply$1.apply(MainLoop.scala:87)          at sbt.State$$anon$1.process(State.scala:176)          at sbt.MainLoop$$anonfun$1.apply(MainLoop.scala:87)          at sbt.MainLoop$$anonfun$1.apply(MainLoop.scala:87)          at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)          at sbt.MainLoop$.next(MainLoop.scala:87)          at sbt.MainLoop$.run(MainLoop.scala:80)          at sbt.MainLoop$$anonfun$runWithNewLog$1.apply(MainLoop.scala:69)          at sbt.MainLoop$$anonfun$runWithNewLog$1.apply(MainLoop.scala:66)          at sbt.Using.apply(Using.scala:25)          at sbt.MainLoop$.runWithNewLog(MainLoop.scala:66)          at sbt.MainLoop$.runAndClearLast(MainLoop.scala:49)          at sbt.MainLoop$.runLoggedLoop(MainLoop.scala:33)          at sbt.MainLoop$.runLogged(MainLoop.scala:25)          at sbt.xMain.run(Main.scala:26)          at xsbt.boot.Launch$$anonfun$run$1.apply(Launch.scala:109)          at xsbt.boot.Launch$.withContextLoader(Launch.scala:129)          at xsbt.boot.Launch$.run(Launch.scala:109)          at xsbt.boot.Launch$$anonfun$apply$1.apply(Launch.scala:36)          at xsbt.boot.Launch$.launch(Launch.scala:117)          at xsbt.boot.Launch$.apply(Launch.scala:19)          at xsbt.boot.Boot$.runImpl(Boot.scala:44)          at xsbt.boot.Boot$.main(Boot.scala:20)          at xsbt.boot.Boot.main(Boot.scala)  [error] java.lang.NoSuchMethodError: scalaz.Scalaz$.ToValidationV(Ljava/lang/Object;)Lscalaz/syntax/ValidationV;  


Scala in Action Chapter 3 Classes and Objects notes

class MongoDBClinet(val host: String, val port: Int)

val means these values are immutable

Primary constructor - It is called when object is created or called from overloaded constructors

If use var then scala also creates getters and setters
If use val then scala creates only getters , remember val are not changable so no setters

When both val and var are missing the instance values are treated as private and not accessible to anyone outside the class

Section 3.1

Download mongodb from http://www.mongodb.org/downloads

Extract it to some location

Start the mongodb

cd C:\Jagat\tools\mongodb-win32-i386-2.4.9\
mkdir db_data
bin\mongod.exe --db_path=db_data

:http://127.0.0.1:28017/

This will show admin path

The client waits at port  27017

Section 3.2

Classes and Objects

Construct the class with default values for host and port

class MongoClient ( val host : String , val port : Int ) {
def this() = this("127.0.0.1",27017)
}

The first statement in overloaded constructor has to be either other overloaded consturcotr or the primaty constructor.

To do some otherration before you invoke construcotr we use companion objects

Add the mongodb jar driver to class path of REPL session


Add jar to scala REPL classpath

scala> :cp C:\Jagat\tools\scala-eclipse\workspace\scalaination\lib\mongo-java-dr
iver-2.12.0.jar
Added 'C:\Jagat\tools\scala-eclipse\workspace\scalaination\lib\mongo-java-driver
-2.12.0.jar'.  Your new classpath is:

Section 3.3

Packaging

In scala you can have nested packages

package A {

package B {


}


}

You can also use java style packaging with package declared at the top of the file

Scala packaging structure does not have to be matching with the folder structure in file system like Java. But when we compile the classes the required folder structure is auto generated as JVM needs that for working. Remember under the hood scala is running on the top of JVM

To add the jar in scala classpath via command line

scalac -classpath my.jar MyNewClass.sclaa

Section 3.4

Scala imports

You can add import statement at any point in the code.

The import will be visible lexically in the code.

* equivalent of java is _ in scala

To import all use

my.package._

If you declare classes or objects without any package , then they all belong to empty package. The cannot be imported to any other package. But members of empty package can see each other

Remap package scala class to avoid conflicts

import java.sql.{Data => SqlDate }

Hide a class , the Date class below cannot be used

import java.sql.{Data => _ }

Section 3.5

Objects and companion objects

There is no static variables in scala

Scala supports concept of having companion object and classes.

bstract class Role {

  def canAccess(page: String): Boolean
}

class Root extends Role {
  override def canAccess(page: String) = page != "Admin"

}

object Role {

  def apply(roleName: String) = roleName match {
    case "root" => new Root
    case "analyst" => new Root
  }
}

Note the object Role and class Role in above code , they are companion object and companion class

Package objects

Package objects allow you to define something at central place which can be used by all the memebers of the package

They are generally defined in file named

package.scala , in the package that corresponds to it

package object ch3 {
 
  val minAge = 18

}

This above variable can be used anywhere in this package named ch3



Scala in Action Chapter 1 Why scala notes

Mixin : Class that provides certain functionality that can be inherited by subclass. But it is not made to be instantiated self. It can be seen as interface with implemented methods

Self Type : A mixin doesnt depend on any method or fields of class that it is mixed into. Sometimes it is useful to use those. This is known as self Type

Type abstraction
  • Parametrization
  • Abstract members

Concepts

  • Referential transparency : Value can be replaced by expression which calculated that value
  • Higher order functions : Functions that take functions as input
  • Lexical closures
  • Pattern matching
  • Single assignment (val)
  • LAzy evaluation
  • Type inference
  • Tail call recursion
  • List comprehension
  • Monodaic effects
  • No side effects
Types of languages

Static
Variables have type , values have type and are checked at compile time
Dynamic
Values have types , variables dont have type

Type inference : Compiler try to infer the type at compile time

Macros : Functions loaded at compile time and used by compiler. Compile time meta programming

Setup Scala to use Maven in Eclipse

 

Eclipse needs separate plugin for maven and scala

1)

Download Scala M2E Plugin

Update site

http://alchim31.free.fr/m2e-scala/update-site

2)

Create a new Maven project

Choose archtype as

Group id
net.alchim31.maven
artifact id
scala-archteype-simple

3)


From now you can use your normal maven commands


Note:


In the pom.xml


I tried with scala.version as 2.10.0


It was failing , i decreased it to 2.8.0 it passed. Just in case you get some error like me

sbt proxy settings

Follow the following to configure sbt use behind the proxy

On Windows

Add the new environment variable with following settings

Variable name

JAVA_OPTS
 
Value
 
-Dhttp.proxyHost=yourserver -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password
 
 
On Linux
 
export JAVA_OPTS="$JAVA_OPTS -Dhttp.proxyHost=yourserver -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password" 
 
Start new command propt and use sbt 

Getting started with Scala IDE Tutorial 1

 

Download the Scala editor

http://scala-ide.org/

Create new Scala Project

image

Create first Object

File > New > Scala Object

image

Write first Class

image

Run As Scala Application

Control + F11

See the output in Console

Congrats : You have run your first Scala IDE program

Lets see next