
scala
BrownWong
None
展开
-
Operator Overloading
1. OverviewAlmost all “operators” are actually methods. Consider this most basic of examples: 1 + 2 That plus sign between the numbers? It’s a method.1 + 2 is the same as 1.+(2), because of the “infi原创 2016-10-08 22:20:56 · 381 阅读 · 0 评论 -
A Taste of scala
scala code exampleclass Upper { def upper(strings: String*): Seq[String] = { strings.map((s:String) => s.toUpperCase()) }}val up = new Upperprintln(up.upper("Hello", "World!"))In Sc原创 2016-10-05 14:05:12 · 275 阅读 · 0 评论 -
Semicolons
Semicolons are expression delimiters and they are inferred. Scala treats the end of a line as the end of an expression, except when it can infer that the expression continues to the next line, as in th原创 2016-10-05 16:51:13 · 706 阅读 · 0 评论 -
Variable Declarations
Scala allows you to decide whether a variable is immutable (read-only) or not (readwrite) when you declare it. immutable “variable”an immutable “variable” is declared with the keyword val (think value原创 2016-10-05 17:08:35 · 435 阅读 · 0 评论 -
Ranges
Sometimes we need a sequence of numbers from some start to finish. A Range literal is just what we need. The following examples show how to create ranges for the types that support them, Int, Long, Flo原创 2016-10-05 18:11:53 · 454 阅读 · 0 评论 -
Partial Functions(偏函数)
1.DefinitionPartial functions are partial in the sense that they aren’t defined for all possible inputs, only those inputs that match at least one of the specified case clauses.Only case clauses can be原创 2016-10-05 20:55:03 · 1603 阅读 · 0 评论 -
A Taste of `Future`
The scala.concurrent.Future API uses implicit arguments to reduce boilerplate in your code. They are another tool for concurrency provided by Scala. Akka uses Futures, but you can use them separately w原创 2016-10-06 11:21:45 · 496 阅读 · 0 评论 -
Type Inference
1. OverviewSome functional programming languages, like Haskell, can infer almost all types, because they can perform global type inference. Scala can’t do this, in part because Scala has to support sub原创 2016-10-06 18:51:21 · 792 阅读 · 0 评论 -
Scala Reserved Words
Table lists the reserved keywords in Scala. Many are found in Java and they usually have the same meanings in both languages. Word Description abstract Makes a declaration abstract. case Sta原创 2016-10-06 22:21:59 · 369 阅读 · 0 评论 -
Literal Values
Integer LiteralsInteger literals can be expressed in decimal, hexadecimal, or octal.You indicate a negative number by prefixing the literal with a – sign.For Long literals, it is necessary to append th原创 2016-10-06 23:41:17 · 1289 阅读 · 0 评论 -
Option,Some and None
Most languages have a special keyword or type instance that’s assigned to reference variables when there’s nothing else for them to refer to. In Java, it’s null, which is a keyword, not an instance of原创 2016-10-07 12:58:04 · 232 阅读 · 0 评论 -
Sealed Class Hierarchies
A key point about Option is that there are really only two valid subtypes. Either we have a value, the Some case, or we don’t, the None case. There are no other subtypes of Option that would be valid.原创 2016-10-07 14:57:25 · 333 阅读 · 0 评论 -
Package and Namespace
Scala adopts the package concept that Java uses for namespaces, but Scala offers more flexibility. Filenames don’t have to match the type names and the package structure does not have to match the dire原创 2016-10-07 15:48:58 · 335 阅读 · 0 评论 -
Import
1. Four ways to importThere are four ways to import:Import all types in a package, using the underscore(_) as a wildcard. e.g. import java.awt._Import individual Scala or Java types. e.g. import j原创 2016-10-07 18:40:39 · 351 阅读 · 0 评论 -
Pattern Matching
1. Values, Variables, and Types in Matches(1) BasicLet’s cover several kinds of matches. The following example matches on specific values, all values of specific types, and it shows one way of writing原创 2016-10-15 14:50:05 · 478 阅读 · 0 评论 -
Interpolated Strings
There are three kinds of interpolated strings.A String of the form s"foo ${bar}" will have the value of expression bar, converted to a String and inserted in place of ${bar}. If the expression bar ret原创 2016-10-13 13:32:30 · 489 阅读 · 0 评论 -
Identifiers Characters
What characters can you use in identifiers? Here is a summary of the rules for identifiers, used for method and type names, variables, etc:Characters Scala allows all the printable ASCII characters原创 2016-10-08 22:49:35 · 407 阅读 · 0 评论 -
Side Effect
When a function returns Unit it is totally side-effecting. There’s nothing useful that can be done with Unit, so the function can only perform side effects on some state, either globally, like performi原创 2016-10-09 00:07:23 · 370 阅读 · 0 评论 -
Methods with Empty/One Argument Lists
1. Methods with Empty Argument ListsIf a method takes no parameters, you can define it without parentheses. Callers must invoke the method without parentheses. Conversely, if you add empty parentheses原创 2016-10-09 00:45:19 · 299 阅读 · 0 评论 -
Precedence Rules
If an expression like 2.0 * 4.0 / 3.0 * 5.0 is actually a series of method calls on Doubles.Here they are in order from lowest to highest precedence:1. All letters2. |3. ^5. < >6. = !7. :8. + -9原创 2016-10-10 10:38:27 · 384 阅读 · 0 评论 -
Scala if Statements
What’s different in Scala is that if statements and almost all other statements in Scala are actually expressions that return values. If it’s true, then the corresponding block is evaluated. Otherwise原创 2016-10-10 11:15:53 · 283 阅读 · 0 评论 -
Scala for Comprehensions
The term comprehension comes from functional programming. It expresses the idea that we are traversing one or more collections of some kind, “comprehending” what we find, and computing something new fr原创 2016-10-11 14:38:06 · 506 阅读 · 0 评论 -
Logical Equality and Compare References
In Java, == compares object references only. It doesn’t perform a logical equality check. You use the equals method for that purpose. In contrast, Scala uses == for logical equality, but it calls the e原创 2016-10-11 17:07:23 · 357 阅读 · 0 评论 -
scala脚本编译运行
用cmd编译和运行scala脚本。脚本内容: Decla.scalaobject Decla{ def main(args: Array[String]){ var myVar : Int = 10 val myVal : String = "Hello Scala" var myVar1 = 20 val myVal1 =原创 2016-09-26 15:28:29 · 4284 阅读 · 0 评论 -
Exception Handling
Scala encourages a coding style that lessens the need for exceptions and exception handling. But exceptions are still used, especially where Scala interacts with Java code, where use of exceptions is m原创 2016-10-11 22:15:54 · 305 阅读 · 0 评论 -
Method Declarations
Method Default and Named ArgumentsHere is an example:package com.brown/** * Created by BrownWong on 2016/9/29. */case class Point(x: Double=0.0, y: Double=0.0){ def shift(deltaX: Double=0.0, delt原创 2016-10-06 00:40:49 · 439 阅读 · 0 评论 -
Call by Name, Call by Value
1. DescriptionTypically, parameters to functions are by-value parameters; that is, the value of the parameter is determined before it is passed to the function. But what if we need to write a functi原创 2016-10-12 13:10:50 · 365 阅读 · 0 评论 -
lazy val
1. DescriptionA related scenario to by-name parameters is the case where you want to evaluate an expression once to initialize a value, not repeatedly, but you want to defer that invocation. There are原创 2016-10-12 15:02:19 · 498 阅读 · 0 评论 -
Enumeration
While enumerations are a built-in part of many programming languages, Scala takes a different route and implements them as an Enumeration class in its standard library. You just define an object that原创 2016-10-12 17:01:55 · 272 阅读 · 0 评论 -
Abstract Types VS Parameterized Types
1. Parameterized typesScala supports parameterized types, which are very similar to generics in Java. Java uses angle brackets (<…>), while Scala uses square brackets ([…]), because < and > are often u原创 2016-10-07 23:03:13 · 260 阅读 · 0 评论