A rational number is a number that can be expressed as a ratio n/d, where n and d are integers, except that d cannot be zero.
n is called the numerator and d the denominator.
Each rational number will be represented by on Rational object. when you add two Rational objects, you'll create a new Rational object to hold the sum.
scala> val oneHalf = new Rational(1, 2)
oneHalf: Rational = 1/2
scala> val twoThirds = new Rational(2, 3)
twoThirds: Rational = 2/3
scala> (oneHalf / 7) + (1 twoThirds)
res0: Rational = 17/42
2015년 11월 12일 목요일
2015년 11월 10일 화요일
Scala2e Chapter5 Basic Types and Operations 5.9 Rich wrappers
Scala2e Chapter5 Basic Types and Operations 5.8 Operator precedence and associativity
Operator precedence determines which parts of an expression are evaluated before the other parts.
show the precedence given to the first character of a method in decreasing order of precedence, with characters on the same line having the same precedence.
The higher a character is in this table, the higher the precedence of methods that start with that start with that character.
Any method that ends in a ':' character is invoked on its right operand, passing in the left operand.
Methods that end in any other character are the other way around.
They are invoked on their left operand, passing in the right operand.
So a * b yields a.*(b), but a ::: b yields b. :::(a).
show the precedence given to the first character of a method in decreasing order of precedence, with characters on the same line having the same precedence.
The higher a character is in this table, the higher the precedence of methods that start with that start with that character.
Any method that ends in a ':' character is invoked on its right operand, passing in the left operand.
Methods that end in any other character are the other way around.
They are invoked on their left operand, passing in the right operand.
So a * b yields a.*(b), but a ::: b yields b. :::(a).
2015년 11월 4일 수요일
Scala2e Chapter5 Basic Types and Operations 5.7 Object equality
If you want to compare two objects for equality, you can use either ==, or its inverse !=.
scala> 1 == 2
res31: Boolean = false
scala> 1 != 2
res32: Boolean = true
scala> 2 == 2
res33: Boolean = true
scala> List(1, 2, 3) == List(1, 2, 3)
res34: Boolean = true
scala> List(1, 2, 3) == List(4, 5, 6)
res35: Boolean = false
scala> 1 == 1.0
res36: Boolean = true
scala> List(1, 2, 3) == "hello"
res37: Boolean = false
scala> List(1, 2, 3) == null
res38: Boolean = false
scala> null == List(1, 2, 3)
res39: Boolean = false
This kind of comarison will yield true on different objects, so long as their contents are the same and their equals method is written to be based on contents.
scala> ("he"+"llo") == "hello"
res40: Boolean = true
In Java, you can use == to compare both primitive and reference types.
On primitive types, Java's == compares value equality, as in Scala.
On reference types, however, Java's == compares reference equality, which means the two variables point to the same object on the JVM's heap.
Scala provides a facility for comparing reference equality, as well, under the name eq. However, eq and its opposite, ne, only apply to objects that directly map to Java objects.
Scala2e Chapter5 Basic Types and Operations 5.6 Bitwise operations
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.
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월 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.
피드 구독하기:
글 (Atom)