Scala enables you to perform operations on individual bits of integer types with several bitwise methods.
bitwise-and(&), bitwise-or(|), bitwise-xor(^), unary_~
scala> 1 & 2
res24: Int = 0
scala> 1 | 2
res25: Int = 3
scala> 1 ˆ 3
res26: Int = 2
scala> ~1
res27: Int = 2
The first expression, 1 & 2, bitwise-ands each bit in 1 (0001) and 2 (0010), which yiedls 0(0000).
The second expression, 1 | 2, bitwise-ors each ibt in the same operands, yielding 3(0011).
The third expression, 1 ^ 3, bitwise-xors each bit in 1(0001) and 3(0011), yielding 2(0010).
The final expressiion, ~1, inverts each bit in 1 (0001), yielding -2, which in binary looks like 11111111111111111111111111111110.
Scala integer type also offer three shift methods: shift left(<<), shift right(>>), and unsigned shift right(>>>).
scala> 1
>> 31
res28: Int = 1
scala> 1
>>> 31
res29: Int = 1
scala> 1 << 2
res30: Int = 4
-1 in binary is 11111111111111111111111111111111. In the first example,-1 >> 31, -1 is shifted to the right 31 bit positions.
Since an Int consisits of 32 bits, this operation effectively moves the leftmost bit over until it becomes the rightmost bit.
Since the >> method fills with ones as it shifts right, because the leftmost bit of -1 is 1, the result is identical to the original left operand, 32 one its, or -1.
In the second example, -1>>> 31, the leftmost bit is again shifted right until it is in the rightmost position, but this time filling with zeroes along the way. Thus the result this time is binary 00000000000000000000000000000001, or 1. In the final example, 1 << 2,
the left operand, 1, is shifted left two positions (filling in with zeroes), resulting in binary 00000000000000000000000000000100, or 4.
2015년 11월 4일 수요일
2015년 11월 3일 화요일
Scala2e Chapter5 Basic Types and Operations 5.5 Relational and logical operations
You can compare numeric types with relational methods greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=), which yield a Boolean result. In addition, you can use the unary '!' operator(the unary_! method)
The logical-and and logical-or operations are short-circuited as in Java: expressions built from these operators are only evaluated as far as needed to determine the result.
In other words, the right-hand side of logical-and and logical-or expressions won't be evaluated if the left-hand side determines the result.
In the first expression, pepper and salt are invoked, but in the second, only salt is invoked. Given salt returns false, there's no need to call pepper.
Scala methods have a facility for delaying the evaluation of their arguments, or even declining to evaluate them at all.
The facility is call by-name parameters.
The logical-and and logical-or operations are short-circuited as in Java: expressions built from these operators are only evaluated as far as needed to determine the result.
In other words, the right-hand side of logical-and and logical-or expressions won't be evaluated if the left-hand side determines the result.
In the first expression, pepper and salt are invoked, but in the second, only salt is invoked. Given salt returns false, there's no need to call pepper.
Scala methods have a facility for delaying the evaluation of their arguments, or even declining to evaluate them at all.
The facility is call by-name parameters.
Scala2e Chapter5 Basic Types and Operations 5.4 Arithmetic operations
scala> 1.2 + 2.3
res6: Double = 3.5
scala> 3 1
res7: Int = 2
scala> 'b' '
a'
res8: Int = 1
scala> 2L * 3L
res9: Long = 6
scala> 11 / 4
res10: Int = 2
scala> 11 % 4
res11: Int = 3
scala> 11.0f / 4.0f
res12: Float = 2.75
scala> 11.0 % 4.0
res13: Double = 3.0
When both the left and right operands are integral types (Int, Long, Byte, Short, or Char), the / operator will tell you the whole number portion of the quotient, excluding any remainder. The % operator indicates the remainder of an implied integer division.
Scala2e Chapter5 Basic Types and Operations 5.3 Operators are methods
Scala provides a rich set of operators for its basic types.
class Int Contains a method named + that takes an Int and returns an Int result.
scala> val sum = 1 + 2 // Scala invokes (1).+(2)
sum: Int = 3
scala> val sumMore = (1).+(2)
sumMore: Int = 3
You call a method that takes multiple arguments using operator notation, you have to place those arguments in parentheses.
scala> val s = "Hello, world!"
s: java.lang.String = Hello, world!
scala> s indexOf 'o' // Scala invokes s.indexOf(’o’)
res0: Int = 4
scala> s indexOf ('o', 5) // Scala invokes s.indexOf(’o’, 5)
res1: Int = 8
Any method can be an operator
In Scala operators are not special language syntax: any method can be an operator. What makes a method an operator is how you use it. When you write "s.indexOf('o'), indexOf is not an operator. But when you write "s indexOf 'o', indexOf is an operator, because you're using it in operator notation.
Scala also has two other operator notations: prefix and postfix.
In prefix notation, you put the method name before the object on which you are invoking the method, for example, the '-' in -7. In postfix notation, you put the method after the object, for example, the "toLong" in "7 toLong".
In contrast to the infix operator notation = in which operatior take two operands. on th the left and the other to the right - prefix and postfix operators are unary: they take just on operand. In prefix notation, the operand is to the right of the operator. Some examples of prefix operators are -2.0, ! found, and ~0xFF.
scala> -2.0 // Scala invokes (2.0).unary_-
res2: Double = -2.0
scala> (2.0).unary_-
res3: Double = -2.0
The only identifiers that can be used as prefix operators are +, -, !, and ~.
Thus, if you define a method anmed unary_!, you could invoke that method on a value or variable of the appropriate type useing prefix operator notation, such as !p.
Postfix operators are methods that take no arguments, when they are invoked without a dot or parentheses. In Scala, you can leave off empty parentheses on method calls. The convention is that you include parentheses if the method has side effects, such as println(), but you can leave them off if the method has no side effects. such as toLowerCase invoked on a String:
scala> val s = "Hello, world!"
s: java.lang.String = Hello, world!
scala> s.toLowerCase
res4: java.lang.String = hello, world!
scala> s toLowerCase
res5: java.lang.String = hello, world!
class Int Contains a method named + that takes an Int and returns an Int result.
scala> val sum = 1 + 2 // Scala invokes (1).+(2)
sum: Int = 3
scala> val sumMore = (1).+(2)
sumMore: Int = 3
You call a method that takes multiple arguments using operator notation, you have to place those arguments in parentheses.
scala> val s = "Hello, world!"
s: java.lang.String = Hello, world!
scala> s indexOf 'o' // Scala invokes s.indexOf(’o’)
res0: Int = 4
scala> s indexOf ('o', 5) // Scala invokes s.indexOf(’o’, 5)
res1: Int = 8
Any method can be an operator
In Scala operators are not special language syntax: any method can be an operator. What makes a method an operator is how you use it. When you write "s.indexOf('o'), indexOf is not an operator. But when you write "s indexOf 'o', indexOf is an operator, because you're using it in operator notation.
Scala also has two other operator notations: prefix and postfix.
In prefix notation, you put the method name before the object on which you are invoking the method, for example, the '-' in -7. In postfix notation, you put the method after the object, for example, the "toLong" in "7 toLong".
In contrast to the infix operator notation = in which operatior take two operands. on th the left and the other to the right - prefix and postfix operators are unary: they take just on operand. In prefix notation, the operand is to the right of the operator. Some examples of prefix operators are -2.0, ! found, and ~0xFF.
scala> -2.0 // Scala invokes (2.0).unary_-
res2: Double = -2.0
scala> (2.0).unary_-
res3: Double = -2.0
The only identifiers that can be used as prefix operators are +, -, !, and ~.
Thus, if you define a method anmed unary_!, you could invoke that method on a value or variable of the appropriate type useing prefix operator notation, such as !p.
Postfix operators are methods that take no arguments, when they are invoked without a dot or parentheses. In Scala, you can leave off empty parentheses on method calls. The convention is that you include parentheses if the method has side effects, such as println(), but you can leave them off if the method has no side effects. such as toLowerCase invoked on a String:
scala> val s = "Hello, world!"
s: java.lang.String = Hello, world!
scala> s.toLowerCase
res4: java.lang.String = hello, world!
scala> s toLowerCase
res5: java.lang.String = hello, world!
Scala2e Chapter5 Basic Types and Operations 5.2 Literals
All of the basic types can be written with literals. A literal is a way to write a constant value directly in code.
- Integer literals
Int, Long, Short, Byte
forms: decimal, hexadecimal, octal
- Floating point literals
Floating point literals are made up of decimal digits, optionally containing a decimal point, and optionally followed by an E or e and an exponent.
If a floating-point literal ends in an F or f, it is a Float, otherwise it is a Double.
Optionally, a Double floating-point literal can end in D or d.
- Character literals
Character literals are composed of any Unicode character between single quotes, such as:
scala> val a = 'A'
a: Char = A
The octal number must be between '\0' and '\377'.
A character literal can also be given as a general Unicode character consisting
of four hex digits and preceded by a \u.
scala> val c = '\101'
c: Char = A
scala> val d = '\u0041'
d: Char = A
scala> val f = '\u0044'
f: Char = D
In fact, such Unicode characters can appear anywhere in a Scala program.
scala> val B\u0041\u0044 = 1
BAD: Int = 1
scala> val backslash = '\\'
backslash: Char = \
- String literals
A string literal is composed of characters surrounded by double quotes
Scala includes a special syntax for raw strings. You start and end a raw string with three double quotation marks in a row(""").
println("""Welcome to Ultamix 3000.
Type "HELP" for help.""")
Welcome to Ultamix 3000.
Type "HELP" for help.
The isssue is that the leading spaces before the second line are included in the string!.
To help with this common situation, you can call stripMargin on strings.
To use this method, put a pipe character(|) at the front of each line, and then call stripMargin on the whole string
println("""|Welcome to Ultamix 3000.
|Type "HELP" for help.""".stripMargin)
Welcome to Ultamix 3000.
Type "HELP" for help.
- Symbol literals
A symbol lilteral is written 'ident, where ident can be any alphanumeric identifier.
Symbol literals are typically used in situations where you would use just an identifier in a dynamically typed language. For instance, you might want to define a method that updates a record in a database;
scala> def updateRecordByName(r: Symbol, value: Any){
//code goes here
}
updateRecordByName: (Symbol, Any)Unit
The method takes as parameters a symbol indicating the name of a record field and a value with which the field should b updated in the record.
In a dynamically typed language, you could invoke this operation passing an undeclared field identifier to the method, but in Scala this would not compile:
scala> updateRecordByName(favoriteAlbum, "OK Computer")
:6: error: not found: value favoriteAlbum
updateRecordByName(favoriteAlbum, "OK Computer")
Instead, and almost as concisely, you can pas a symbol literal:
scala> updateRecordByName('favoriteAlub, "OK Computer")
There is not much you can do with a symbol, except find out its name:
scala>val s = 'aSymbol
s: Symbol = 'aSymbol
scala>s.name
res20: String = aSymbol
Another thing that's noteworthy is that symbols are interned. If you write the same symbol literal twice, both expressions wil refer to the exact same Symbol object.
- Boolean Iiterals
The Boolean type has two literals, true and false:
- Integer literals
Int, Long, Short, Byte
forms: decimal, hexadecimal, octal
scala> val hex = 0x5
hex: Int = 5
scala> val hex2 = 0x00FF
hex2: Int = 255
scala> val magic = 0xcafebabe
magic: Int = -889275714
scala> val oct = 035 // (35 octal is 29 decimal)
oct: Int = 29
scala> val nov = 0777
nov: Int = 511
scala> val dec = 0321
dec: Int = 209
scala> val dec1 = 31
dec1: Int = 31
scala> val dec2 = 255
dec2: Int = 255
scala> val dec3 = 20
dec3: Int = 20
scala> val prog = 0XCAFEBABEL
prog: Long = 3405691582
scala> val tower = 35L
tower: Long = 35
scala> val of = 31l
of: Long = 31
- Floating point literals
Floating point literals are made up of decimal digits, optionally containing a decimal point, and optionally followed by an E or e and an exponent.
If a floating-point literal ends in an F or f, it is a Float, otherwise it is a Double.
Optionally, a Double floating-point literal can end in D or d.
scala> val big = 1.2345
big: Double = 1.2345
scala> val bigger = 1.2345e1
bigger: Double = 12.345
scala> val biggerStill = 123E45
biggerStill: Double = 1.23E47
scala> val little = 1.2345F
little: Float = 1.2345
scala> val littleBigger = 3e5f
littleBigger: Float = 300000.0
scala> val anotherDouble = 3e5
anotherDouble: Double = 300000.0
scala> val yetAnother = 3e5D
yetAnother: Double = 300000.0
- Character literals
Character literals are composed of any Unicode character between single quotes, such as:
scala> val a = 'A'
a: Char = A
The octal number must be between '\0' and '\377'.
A character literal can also be given as a general Unicode character consisting
of four hex digits and preceded by a \u.
scala> val c = '\101'
c: Char = A
scala> val d = '\u0041'
d: Char = A
scala> val f = '\u0044'
f: Char = D
In fact, such Unicode characters can appear anywhere in a Scala program.
scala> val B\u0041\u0044 = 1
BAD: Int = 1
scala> val backslash = '\\'
backslash: Char = \
- String literals
A string literal is composed of characters surrounded by double quotes
Scala includes a special syntax for raw strings. You start and end a raw string with three double quotation marks in a row(""").
println("""Welcome to Ultamix 3000.
Type "HELP" for help.""")
Welcome to Ultamix 3000.
Type "HELP" for help.
The isssue is that the leading spaces before the second line are included in the string!.
To help with this common situation, you can call stripMargin on strings.
To use this method, put a pipe character(|) at the front of each line, and then call stripMargin on the whole string
println("""|Welcome to Ultamix 3000.
|Type "HELP" for help.""".stripMargin)
Welcome to Ultamix 3000.
Type "HELP" for help.
- Symbol literals
A symbol lilteral is written 'ident, where ident can be any alphanumeric identifier.
Symbol literals are typically used in situations where you would use just an identifier in a dynamically typed language. For instance, you might want to define a method that updates a record in a database;
scala> def updateRecordByName(r: Symbol, value: Any){
//code goes here
}
updateRecordByName: (Symbol, Any)Unit
The method takes as parameters a symbol indicating the name of a record field and a value with which the field should b updated in the record.
In a dynamically typed language, you could invoke this operation passing an undeclared field identifier to the method, but in Scala this would not compile:
scala> updateRecordByName(favoriteAlbum, "OK Computer")
updateRecordByName(favoriteAlbum, "OK Computer")
Instead, and almost as concisely, you can pas a symbol literal:
scala> updateRecordByName('favoriteAlub, "OK Computer")
There is not much you can do with a symbol, except find out its name:
scala>val s = 'aSymbol
s: Symbol = 'aSymbol
scala>s.name
res20: String = aSymbol
Another thing that's noteworthy is that symbols are interned. If you write the same symbol literal twice, both expressions wil refer to the exact same Symbol object.
- Boolean Iiterals
The Boolean type has two literals, true and false:
2015년 11월 2일 월요일
Scala2e Chapter5 Basic Types and Operations 5.1 Some basic types
Collectively, types Byte, Short, Int, Long, and Char are called integral types.
The integral types plus Float and Double are called numeric types.
Scala2e Chapter4 Classes and Objects 4.5 The Application trait
Scala provides a trit, scala.Application
import ChecksumAccumulator.calculate
object FallWinterSpringSummer extends Application {
for(season <- fall="" list="" p="" spring="" winter=""> println(season + ": " + calculae(season))
}
Then instead of writing a main method, you place the code you would have put in the main method directly between the curly braces of the singleton object. That's it. You can compile and run this application just like any other.
Inheriting from Application is shorter than writing an explicit main method, but it also has some shortcomings.
First, you can't use this trait if you need to access command-line arguments, because the args array isn't available.
Second, because of some restrictions in the JVM threading model, you need an explicit main method if your program is multi-threaded.
Finally, some implementations of the JVM do not optimize the initialization code of an object which is executed by the Application trait. So you should inherit from Application only when your program is relatively simple and single-threaded.
->
import ChecksumAccumulator.calculate
object FallWinterSpringSummer extends Application {
for(season <- fall="" list="" p="" spring="" winter=""> println(season + ": " + calculae(season))
}
Then instead of writing a main method, you place the code you would have put in the main method directly between the curly braces of the singleton object. That's it. You can compile and run this application just like any other.
Inheriting from Application is shorter than writing an explicit main method, but it also has some shortcomings.
First, you can't use this trait if you need to access command-line arguments, because the args array isn't available.
Second, because of some restrictions in the JVM threading model, you need an explicit main method if your program is multi-threaded.
Finally, some implementations of the JVM do not optimize the initialization code of an object which is executed by the Application trait. So you should inherit from Application only when your program is relatively simple and single-threaded.
->
피드 구독하기:
글 (Atom)