scala> //#m:74
scala> val data = List(1, 2, 3, 4)
data: List[Int] = List(1, 2, 3, 4)
scala> data.foreach(x => println(s"This is ${x}"))
This is 1
This is 2
This is 3
This is 4
scala> //This do not work, as we have to wrap x:Int with Round Brackets
scala> data.foreach(x:Int => println(s"This is ${x}"))
:1: error: ')' expected but '(' found.
data.foreach(x:Int => println(s"This is ${x}"))
^
:1: error: ';' expected but ')' found.
data.foreach(x:Int => println(s"This is ${x}"))
^
scala> //When the type of argument is explicitly specified(as here x:Int),
scala> //use Round brackets...ie()
scala> data.foreach((x:Int) => println(s"This is ${x}"))
This is 1
This is 2
This is 3
This is 4
Showing posts with label b4225| (). Show all posts
Showing posts with label b4225| (). Show all posts
Use Round brackets...ie(), when the Type annotation is used for an argument
Elements in Sequence are accessed using Round Brackets..ie ()
Elements are Sequence accessed using Round brackets...ie(), unlike in Java where we use []
scala> val data = List(1, 2, 3, 4) data: List[Int] = List(1, 2, 3, 4) scala> val second = data(1) second: Int = 2
Multiple Argument List : Example
A method will multiple argument list are also Variadic methods
scala> // #PS:37
scala> // When a function is defined with Multiple argument list(getMark()
scala> // in this case),
scala> // (1) : last argument can be passed using curly bracket,
scala> // which gives a nice looking block syntax instead of using Round
scala> // bracket
scala> //(2) : Implicit arguments can be passed for last argument list
scala>
scala> // getMark() is defined to take 3 arguments
scala> // Arg 1 : String
scala> // Arg 2 : Int
scala> // Arg 3 : Function
scala> def getMark(subject: String, mark:Int)(printFn: (String, Int) => String) = {
| val finalMark = if (subject == "math") {
| mark + 10
| }else {
| mark
| }
| printFn(subject, finalMark)
| }
getMark: (subject: String, mark: Int)(printFn: (String, Int) => String)String
scala>
scala> // Calling getMark() : Example 1 ( Last argument passed using curly bracket)
scala> getMark("math", 80){
| (subject:String, mark:Int) => {
| s"$subject mark is -> $mark"
| }
| }
res33: String = math mark is -> 90
scala>
scala> // Calling getMark() : Example 2
scala> getMark("science", 70){
| (subject:String, mark:Int) => {
| if(mark <= 80) {
| s"$subject mark is low -> $mark"
| }else {
| s"$subject mark is good -> $mark"
| }
| }
| }
res34: String = science mark is low -> 70
Semicolon(;) : The expression delimiter
An expression can be delimited using semicolon(;)
An end of line is inferred to be semicolon(and therefore end of expression) unless otherwise the end of line has one of these characters ; equalto(=), Curly Brace({), Comma(,), Periods(.) or Operators(+, -, etc...). These characters at the end of the line denotes a continuation of the expression
PS :
The Return type of a function is called as Result Type (#m:68) in Scala
Also note, in function type 'fn2: ()Unit', empty Round brackets represents a function that takes no arguments
scala> val no:Int = 0; no: Int = 0 scala> val no2:Float = 0; val no3:Double = 0; no2: Float = 0.0 no3: Double = 0.0
An end of line is inferred to be semicolon(and therefore end of expression) unless otherwise the end of line has one of these characters ; equalto(=), Curly Brace({), Comma(,), Periods(.) or Operators(+, -, etc...). These characters at the end of the line denotes a continuation of the expression
scala> def fn1() =
| println("Multiline expression using equal to")
fn1: ()Unit
scala>
| def fn2() = {
| println("Multiline expression using Curly brace")
| }
fn2: ()Unit
scala> def fn3(arg1: Int,
| arg2: Float) =
| println("This is " +
| "another example")
fn3: (arg1: Int, arg2: Float)Unit
PS :
The Return type of a function is called as Result Type (#m:68) in Scala
Also note, in function type 'fn2: ()Unit', empty Round brackets represents a function that takes no arguments
The injector (or) default method : apply()
The apply method in a class can be called using the Round brackets...ie (). This helps to create a convention wherein a Method in an Object is called like a Function
scala> class SumOf {
| def apply(no1: Int, no2: Int) : Int = {
| no1 + no2
| }
| }
defined class SumOf
scala> val sum = new SumOf()
sum: SumOf = SumOf@1b54903a
scala> // Calling the method apply() explicitly
scala> println(sum.apply(3, 2))
5
scala> // apply() method called without its name using
scala> // Round brackets...ie ()
scala> println(sum(3, 2))
5
Class & Object : 101
Demonstrates,
> run-main e11_Classes.a11_101.a15_ConsDemo
[info] Compiling 1 Scala source to /Users/raj/gitws/scalaexamples/target/scala-2.10/classes...
[info] Running e11_Classes.a11_101.a15_ConsDemo
(ClassWithMemberVariable : Members are -> ,val1,, ,101)
(Method arguments are -> ,WrappedArray(val1, val2, val3))
ClassWithoutMemberVariable
[success] Total time: 1 s, completed Apr 29, 2016 9:45:39 PM
Here WrappedArray wraps Java Array
- [] : Parameterized Types
- () : Argument List for Member Variables
- Unit
- class
- object
- and more...
package e11_Classes.a11_101
//** Member variables are given in the argument list...
// ie (mArg1, mArg2)
//** Outermost curly braces {} also acts as Primary Constructor #PS:12
class ClassWithMemberVariable(mArg1: String, mArg2: Int) {
//** Square bracket([]) is used to Parameterize types
//** Equal to (=) Separates Method signature from Method Body
//** String* : Star is appended to denote variable length argument
// list
//** Seq : Is an Abstraction for Collections that can be
// iterated in a fixed order
def demoFunction(arg1: String*): Seq[String] = {
println("ClassWithMemberVariable : Members are -> ", mArg1,
", ", mArg2)
println("Method arguments are -> ", arg1)
arg1
}
}
// **Argument List..ie () not needed in the Class definition
// if we do not have any member variables
class ClassWithoutMemberVariable {
// **When a function do not return anything
// its return type can be denoted by Unit
def demoFunction : Unit = {
println("ClassWithoutMemberVariable")
}
//** Curly braces {} used to enclose the body of the method
// can be omitted if the method has only single expression
def sayHello = "Hello"
}
// ** object : An object will have only one instance and there
// conforms to the Singleton pattern
object a15_ConsDemo {
def main(args: Array[String]) {
val obj1 = new ClassWithMemberVariable("val1", 101)
obj1.demoFunction("val1", "val2", "val3")
//** Note here the absence of Argument list
//...round brackets ie..()
val obj2 = new ClassWithoutMemberVariable
obj2.demoFunction
}
}
> run-main e11_Classes.a11_101.a15_ConsDemo
[info] Compiling 1 Scala source to /Users/raj/gitws/scalaexamples/target/scala-2.10/classes...
[info] Running e11_Classes.a11_101.a15_ConsDemo
(ClassWithMemberVariable : Members are -> ,val1,, ,101)
(Method arguments are -> ,WrappedArray(val1, val2, val3))
ClassWithoutMemberVariable
[success] Total time: 1 s, completed Apr 29, 2016 9:45:39 PM
Here WrappedArray wraps Java Array
Subscribe to:
Posts (Atom)