11 scala 本地函数

11 scala 本地函数

更多干货

  • 分布式实战(干货)
  • spring cloud 实战(干货)
  • mybatis 实战(干货)
  • spring boot 实战(干货)
  • React 入门实战(干货)
  • 构建中小型互联网企业架构(干货)
  • python 学习持续更新
  • ElasticSearch 笔记
  • scala 学习持续更新

本地函数

import scala.io.Source

object FunctionOps {
  def main(args: Array[String]) {
    val width = args(0).toInt
    for (arg <- args.drop(1))
      processData(arg, width)

    var increase = (x: Int) => x + 1
    println(increase(10))
    increase = (x: Int) => x + 9999

    val someNumbers = List(-11, -10, -5, 0, 5, 10)    //函数的几种演变写发
    someNumbers.foreach((x: Int) => print(x))
    someNumbers.filter((x: Int) => x > 0).foreach((x: Int) => print(x))
    someNumbers.filter((x) => x > 0).foreach((x: Int) => print(x))
    someNumbers.filter(x => x > 0).foreach((x: Int) => print(x))
    someNumbers.filter(_ > 0).foreach((x: Int) => print(x))
    val f = (_: Int) + (_: Int)
    println(f(5, 10))
  }


  def processData(filename: String, width: Int) {

    def processLine(line: String) {
      if (line.length > width)
        println(filename + ": " + line)
    }

    val source = Source.fromFile(filename)
    for (line <- source.getLines)
      processLine(line)

  }


}

你可能感兴趣的:(scala,多语言)