楼主: Lisrelchen
2528 16

Programming Scala [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2015-5-1 10:16:44 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Programming Scala

Scalability = Functional Programming + Objects


Book Description
Learn how to be more productive with Scala, a new multi-paradigm language for the Java Virtual Machine (JVM) that integrates features of both object-oriented and functional programming. With this book, you'll discover why Scala is ideal for highly scalable, component-based applications that support concurrency and distribution.

Programming Scala clearly explains the advantages of Scala as a JVM language. You'll learn how to leverage the wealth of Java class libraries to meet the practical needs of enterprise and Internet projects more easily. Packed with code examples, this book provides useful information on Scala's command-line tools, third-party tools, libraries, and available language-aware plugins for editors and IDEs.
Book Details
  • Publisher:        O'Reilly Media
  • By:        Dean Wampler, Alex Payne
  • ISBN:        978-0-596-15595-7
  • Year:        2009
  • Pages:        448
  • Language:        English
  • File size:        3.37 MB
  • File format:        PDF
  • Download:        

    本帖隐藏的内容

    Programming Scala.rar (3.19 MB, 需要: 20 个论坛币) 本附件包括:
    • Programming Scala.pdf

  • https://github.com/deanwampler/prog-scala-2nd-ed-code-examples


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Program SCALA gram Ming advantages discover features support learn

本帖被以下文库推荐

沙发
Nicolle(真实交易用户) 学生认证  发表于 2016-4-3 23:09:12
提示: 作者被禁止或删除 内容自动屏蔽

藤椅
Nicolle(真实交易用户) 学生认证  发表于 2016-4-3 23:10:48
提示: 作者被禁止或删除 内容自动屏蔽

板凳
Nicolle(真实交易用户) 学生认证  发表于 2016-4-3 23:14:08
提示: 作者被禁止或删除 内容自动屏蔽

报纸
Nicolle(真实交易用户) 学生认证  发表于 2016-4-3 23:15:21
提示: 作者被禁止或删除 内容自动屏蔽

地板
ReneeBK(未真实交易用户) 发表于 2016-4-3 23:17:51
  1. Guards in case Clauses
  2. Matching on literal values is very useful, but sometimes you need a little additional logic:

  3. // src/main/scala/progscala2/patternmatching/match-guard.sc

  4. for (i <- Seq(1,2,3,4)) {
  5.   i match {
  6.     case _ if i%2 == 0 => println(s"even: $i")                       // 1
  7.     case _             => println(s"odd:  $i")                       // 2
  8.   }
  9. }
  10. 1
  11. Match only if i is even. We use _ instead of a variable, because we already have i.

  12. 2
  13. Match the only other possibility, that i is odd.
复制代码

7
ReneeBK(未真实交易用户) 发表于 2016-4-3 23:19:21
  1. // src/main/scala/progscala2/patternmatching/match-deep.sc

  2. // Simplistic address type. Using all strings is questionable, too.
  3. case class Address(street: String, city: String, country: String)
  4. case class Person(name: String, age: Int, address: Address)

  5. val alice   = Person("Alice",   25, Address("1 Scala Lane", "Chicago", "USA"))
  6. val bob     = Person("Bob",     29, Address("2 Java Ave.",  "Miami",   "USA"))
  7. val charlie = Person("Charlie", 32, Address("3 Python Ct.", "Boston",  "USA"))

  8. for (person <- Seq(alice, bob, charlie)) {
  9.   person match {
  10.     case Person("Alice", 25, Address(_, "Chicago", _)) => println("Hi Alice!")
  11.     case Person("Bob", 29, Address("2 Java Ave.", "Miami", "USA")) =>
  12.       println("Hi Bob!")
  13.     case Person(name, age, _) =>
  14.       println(s"Who are you, $age year-old person named $name?")
  15.   }
  16. }
复制代码

8
ReneeBK(未真实交易用户) 发表于 2016-4-3 23:38:14
  1. // src/main/scala/progscala2/patternmatching/match-vararglist.sc

  2. // Operators for WHERE clauses
  3. object Op extends Enumeration {                                      // 1
  4.   type Op = Value

  5.   val EQ   = Value("=")
  6.   val NE   = Value("!=")
  7.   val LTGT = Value("<>")
  8.   val LT   = Value("<")
  9.   val LE   = Value("<=")
  10.   val GT   = Value(">")
  11.   val GE   = Value(">=")
  12. }
  13. import Op._

  14. // Represent a SQL "WHERE x op value" clause, where +op+ is a
  15. // comparison operator: =, !=, <>, <, <=, >, or >=.
  16. case class WhereOp[T](columnName: String, op: Op, value: T)          // 2

  17. // Represent a SQL "WHERE x IN (a, b, c, ...)" clause.
  18. case class WhereIn[T](columnName: String, val1: T, vals: T*)         // 3

  19. val wheres = Seq(                                                    // 4
  20.   WhereIn("state", "IL", "CA", "VA"),
  21.   WhereOp("state", EQ, "IL"),
  22.   WhereOp("name", EQ, "Buck Trends"),
  23.   WhereOp("age", GT, 29))

  24. for (where <- wheres) {
  25.   where match {
  26.     case WhereIn(col, val1, vals @ _*) =>                            // 5
  27.       val valStr = (val1 +: vals).mkString(", ")
  28.       println (s"WHERE $col IN ($valStr)")
  29.     case WhereOp(col, op, value) => println (s"WHERE $col $op $value")
  30.     case _ => println (s"ERROR: Unknown expression: $where")
  31.   }
  32. }
  33. 1
  34. An enumeration for the comparison operators, where we assign a “name” that’s the string representation of the operator in SQL.

  35. 2
  36. A case class for WHERE x OP y clauses.

  37. 3
  38. A case class for WHERE x IN (val1, val2, …) clauses.

  39. 4
  40. Some example objects to parse.

  41. 5
  42. Note the syntax for matching on a variable argument: name @ _*.
复制代码

9
ReneeBK(未真实交易用户) 发表于 2016-4-3 23:41:53
  1. Matching on Regular Expressions
  2. Regular expressions (or regexes) are convenient for extracting data from strings that have a particular structure.

  3. Scala wraps Java’s regular expressions.[7] Here is an example:

  4. // src/main/scala/progscala2/patternmatching/match-regex.sc

  5. val BookExtractorRE = """Book: title=([^,]+),\s+author=(.+)""".r     // 1
  6. val MagazineExtractorRE = """Magazine: title=([^,]+),\s+issue=(.+)""".r

  7. val catalog = Seq(
  8.   "Book: title=Programming Scala Second Edition, author=Dean Wampler",
  9.   "Magazine: title=The New Yorker, issue=January 2014",
  10.   "Unknown: text=Who put this here??"
  11. )

  12. for (item <- catalog) {
  13.   item match {
  14.     case BookExtractorRE(title, author) =>                           // 2
  15.       println(s"""Book "$title", written by $author""")
  16.     case MagazineExtractorRE(title, issue) =>
  17.       println(s"""Magazine "title", issue $issue""")
  18.     case entry => println(s"Unrecognized entry: $entry")
  19.   }
  20. }
  21. 1
  22. Match a book string, with two capture groups (note the parentheses), one for the title and one for the author. Calling the r method on a string creates a regex from it. Match a magazine string, with capture groups for the title and issue (date).

  23. 2
  24. Use them like case classes, where the string matched by each capture group is assigned to a variable.
复制代码

10
Lisrelchen(未真实交易用户) 发表于 2016-4-3 23:57:06
  1. // src/main/scala/progscala2/patternmatching/match-deep2.sc

  2. case class Address(street: String, city: String, country: String)
  3. case class Person(name: String, age: Int, address: Address)

  4. val alice   = Person("Alice",   25, Address("1 Scala Lane", "Chicago", "USA"))
  5. val bob     = Person("Bob",     29, Address("2 Java Ave.",  "Miami",   "USA"))
  6. val charlie = Person("Charlie", 32, Address("3 Python Ct.", "Boston",  "USA"))

  7. for (person <- Seq(alice, bob, charlie)) {
  8.   person match {
  9.     case p @ Person("Alice", 25, address) => println(s"Hi Alice! $p")
  10.     case p @ Person("Bob", 29, a @ Address(street, city, country)) =>
  11.       println(s"Hi ${p.name}! age ${p.age}, in ${a.city}")
  12.     case p @ Person(name, age, _) =>
  13.       println(s"Who are you, $age year-old person named $name? $p")
  14.   }
  15. }
  16. The p @ … syntax assigns to p the whole Person instance and similarly for a @ … and an Address.
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-27 05:13