<commit_before>---
layout: post
title: Scala学习笔记(二)：函数编程&集合
categories: [scala]
tags: [scala, functional, collection]
---

关于Scala的函数和集合

# lambda

匿名函数（至少python是叫lambda）算是FO的基本功能了。scala定义一个匿名函数按照如下格式：

```scala
object Main {

  def main(args: Array[String]): Unit = {
    val say = (sth: String) => {println("say: %s".format(sth))}
    say("hello")
    say("world")
  }
}

// output:
// say: hello
// say: world
```

# Using pattern matching

scala中，pattern matching的逻辑语句使用频率很高，配合case class可以代替if用在一些条件判断的情况

```scala
object Main {

  def main(args: Array[String]): Unit = {
    def fibonacci(in: Any): Int = {
      in match {
        case 0 => 0
        case 1 => 1
        case n: Int => fibonacci(n - 1) + fibonacci(n -2)
        case _ => 0
      }
    }

    println(fibonacci("ABC"))
    println(fibonacci(3))
  }
}

// output:
// 0
// 2
```

# Case Class

Case class是特殊的class：

1. Case class的创建不需要new操作符
2. Case class可作为case matching参数

```scala
object Main {

  def main(args: Array[String]): Unit = {
    trait Exp {}

    case class Value(value: Int) extends Exp {}
    case class Sum(l: Value, r: Value) extends Exp {}
    case class Sub(l: Value, r: Value) extends Exp {}

    def compute(exp: Exp): Int = {
      exp match {
        case Value(v) => v
        case Sum(l, r) => compute(l) + compute(r)
        case Sub(l, r) => compute(l) - compute(r)
        case _ => 0
      }
    }

    // 1 + 2
    val case1 = Sum(Value(1), Value(2))
    // 10 - 5
    val case2 = Sub(Value(10), Value(5))

    println(compute(case1))
    println(compute(case2))
  }
}

// output
// 3
// 5
```<commit_msg>add scala pattern binder example<commit_after>---
layout: post
title: Scala学习笔记(二)：函数编程&集合
categories: [scala]
tags: [scala, functional, collection]
---

关于Scala的函数和集合

# lambda

匿名函数（至少python是叫lambda）算是FO的基本功能了。scala定义一个匿名函数按照如下格式：

```scala
object Main {

  def main(args: Array[String]): Unit = {
    val say = (sth: String) => {println("say: %s".format(sth))}
    say("hello")
    say("world")
  }
}

// output:
// say: hello
// say: world
```

# Using pattern matching

scala中，pattern matching的逻辑语句使用频率很高，配合case class可以代替if用在一些条件判断的情况

```scala
object Main {

  def main(args: Array[String]): Unit = {
    def fibonacci(in: Any): Int = {
      in match {
        case 0 => 0
        case 1 => 1
        case n: Int => fibonacci(n - 1) + fibonacci(n -2)
        case _ => 0
      }
    }

    println(fibonacci("ABC"))
    println(fibonacci(3))
  }
}

// output:
// 0
// 2
```

# Case Class

Case class是特殊的class：

1. Case class的创建不需要new操作符
2. Case class可作为case matching参数

```scala
object Main {

  def main(args: Array[String]): Unit = {
    trait Exp {}

    case class Value(value: Int) extends Exp {}
    case class Sum(l: Value, r: Value) extends Exp {}
    case class Sub(l: Value, r: Value) extends Exp {}

    def compute(exp: Exp): Int = {
      exp match {
        case value @ Value(v) => println(s"value is ${value}"); v	// pattern binder: using @ if want to get reference of object Value
        case Sum(l, r) => compute(l) + compute(r)
        case Sub(l, r) => compute(l) - compute(r)
        case _ => 0
      }
    }

    // 1 + 2
    val case1 = Sum(Value(1), Value(2))
    // 10 - 5
    val case2 = Sub(Value(10), Value(5))

    println(compute(case1))
    println(compute(case2))
  }
}

// output
// value is 1
// value is 2
// 3
// value is 10
// value is 5
// 5
```
{'fraction_non_alphanumeric': 0.1555424875036754, 'fraction_numerical': 0.018523963540135253, 'mean_word_length': 2.5697796432318993, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4610807', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1375, 'n_words': 337}