请选择 进入手机版 | 继续访问电脑版
楼主: Nicolle
4379 31

Scala Cookbook:Recipes for Object-Oriented and Functional Programming [推广有奖]

巨擘

0%

还不是VIP/贵宾

-

TA的文库  其他...

Python(Must-Read Books)

SAS Programming

Must-Read Books

威望
16
论坛币
12402323 个
通用积分
1620.7415
学术水平
3305 点
热心指数
3329 点
信用等级
3095 点
经验
477211 点
帖子
23879
精华
91
在线时间
9878 小时
注册时间
2005-4-23
最后登录
2022-3-6

Nicolle 学生认证  发表于 2015-5-5 11:32:21 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

本帖被以下文库推荐

giggleholy 发表于 2015-5-5 11:36:08 |显示全部楼层 |坛友微信交流群
  1. Extracting Parts of a String That Match Patterns
  2. Problem
  3. You want to extract one or more parts of a string that match the regular-expression
  4. patterns you specify.
  5. Solution
  6. Define the regular-expression patterns you want to extract, placing parentheses around
  7. them so you can extract them as “regular-expression groups.” First, define the desired
  8. pattern:
  9. val pattern = "([0-9]+) ([A-Za-z]+)".r
  10. Next, extract the regex groups from the target string:
  11. val pattern(count, fruit) = "100 Bananas"
  12. This code extracts the numeric field and the alphabetic field from the given string as
  13. two separate variables, count and fruit, as shown in the Scala REPL:
  14. scala> val pattern = "([0-9]+) ([A-Za-z]+)".r
  15. pattern: scala.util.matching.Regex = ([0-9]+) ([A-Za-z]+)
  16. scala> val pattern(count, fruit) = "100 Bananas"
  17. count: String = 100
  18. fruit: String = Bananas
  19. Discussion
  20. The
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

auirzxp 学生认证  发表于 2015-5-5 11:37:06 |显示全部楼层 |坛友微信交流群
  1. Accessing a Character in a String
  2. Problem
  3. You want to get a character at a specific position in a string.
  4. Solution
  5. You could use the Java charAt method:
  6. scala> "hello".charAt(0)
  7. res0: Char = h
  8. However, the preferred approach is to use Scala’s Array notation:
  9. scala> "hello"(0)
  10. res1: Char = h
  11. scala> "hello"(1)
  12. res2: Char = e
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

  1. Add Your Own Methods to the String Class
  2. Problem
  3. Rather than create a separate library of String utility methods, like a StringUtilities
  4. class, you want to add your own behavior(s) to the String class, so you can write code
  5. like this:
  6. "HAL".increment
  7. Instead of this:
  8. StringUtilities.increment("HAL")
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

silverniu 在职认证  发表于 2015-5-6 01:44:45 |显示全部楼层 |坛友微信交流群
  1. Parsing a Number from a String
  2. Problem
  3. You want to convert a String to one of Scala’s numeric types.
  4. Solution
  5. Use the to* methods that are available on a String (courtesy of the StringLike trait):
  6. scala> "100".toInt
  7. res0: Int = 100
  8. scala> "100".toDouble
  9. res1: Double = 100.0
  10. scala> "100".toFloat
  11. res2: Float = 100.0
  12. scala> "1".toLong
  13. res3: Long = 1
  14. scala> "1".toShort
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

Nicolle 学生认证  发表于 2015-9-12 07:43:14 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

Nicolle 学生认证  发表于 2015-9-12 07:44:44 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

Nicolle 学生认证  发表于 2015-9-12 07:45:33 |显示全部楼层 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

ReneeBK 发表于 2015-9-12 07:47:22 |显示全部楼层 |坛友微信交流群
  1. Replacements for ++ and −−

  2. Problem
  3. You want to increment or decrement numbers using operators like ++ and −− that are available in other languages, but Scala doesn’t have these operators.

  4. Solution
  5. Because val fields are immutable, they can’t be incremented or decremented, but var Int fields can be mutated with the += and −= methods:

  6. scala> var a = 1
  7. a: Int = 1

  8. scala> a += 1

  9. scala> println(a)
  10. 2

  11. scala> a −= 1

  12. scala> println(a)
  13. 1
  14. As an added benefit, you use similar methods for multiplication and division:

  15. scala> var i = 1
  16. i: Int = 1

  17. scala> i *= 2

  18. scala> println(i)
  19. 2

  20. scala> i *= 2

  21. scala> println(i)
  22. 4

  23. scala> i /= 2

  24. scala> println(i)
  25. 2
  26. Note that these symbols aren’t operators; they’re implemented as methods that are available on Int fields declared as a var. Attempting to use them on val fields results in a compile-time error:

  27. scala> val x = 1
  28. x: Int = 1

  29. scala> x += 1
  30. <console>:9: error: value += is not a member of Int
  31.               x += 1
  32.                 ^
复制代码

使用道具

ReneeBK 发表于 2015-9-12 07:48:06 |显示全部楼层 |坛友微信交流群
  1. Comparing Floating-Point Numbers

  2. Problem
  3. You need to compare two floating-point numbers, but as in some other programming languages, two floating-point numbers that should be equivalent may not be.

  4. Solution
  5. As in Java and many other languages, you solve this problem by creating a method that lets you specify the precision for your comparison. The following “approximately equals” method demonstrates the approach:

  6. def ~=(x: Double, y: Double, precision: Double) = {
  7.   if ((x - y).abs < precision) true else false
  8. }
  9. You can use this method like this:

  10. scala> val a = 0.3
  11. a: Double = 0.3

  12. scala> val b = 0.1 + 0.2
  13. b: Double = 0.30000000000000004

  14. scala> ~=(a, b, 0.0001)
  15. res0: Boolean = true

  16. scala> ~=(b, a, 0.0001)
  17. res1: Boolean = true
复制代码

使用道具

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

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-28 20:39