http://blog.csdn.net/pipisorry/article/details/52913548

python参考[python函数式编程:apply, map, lambda和偏函数 ]

Scala 中下划线的用法

1、存在性类型:Existential types
def foo(l: List[Option[_]]) = ...

2、高阶类型参数:Higher kinded type parameters
case class A[K[_],T](a: K[T])

3、临时变量:Ignored variables
val _ = 5

4、临时参数:Ignored parameters
List(1, 2, 3) foreach { _ => println("Hi") }

5、通配模式:Wildcard patterns
Some(5) match { case Some(_) => println("Yes") }
match {
     case List(1,_,_) => " a list with three element and the first element is 1"
     case List(_*)  => " a list with zero or more elements "
     case Map[_,_] => " matches a map with any key type and any value type "
     case _ =>
 }
val (a, _) = (1, 2)
for (_ <- 1 to 10)

6、通配导入:Wildcard imports
import java.util._

7、隐藏导入:Hiding imports
// Imports all the members of the object Fun but renames Foo to Bar
import com.test.Fun.{ Foo => Bar , _ }

// Imports all the members except Foo. To exclude a member rename it to _
import com.test.Fun.{ Foo => _ , _ }

8、连接字母和标点符号:Joining letters to punctuation
def bang_!(x: Int) = 5

9、占位符语法:Placeholder syntax
List(1, 2, 3) map (_ + 2)
_ + _   
( (_: Int) + (_: Int) )(2,3)

val nums = List(1,2,3,4,5,6,7,8,9,10)

nums map (_ + 2)
nums sortWith(_>_)
nums filter (_ % 2 == 0)
nums reduceLeft(_+_)
nums reduce (_ + _)
nums reduceLeft(_ max _)
nums.exists(_ > 5)
nums.takeWhile(_ < 8)

10、偏应用函数:Partially applied functions
def fun = {
    // Some code
}
val funLike = fun _

List(1, 2, 3) foreach println _

1 to 5 map (10 * _)

//List("foo", "bar", "baz").map(_.toUpperCase())
List("foo", "bar", "baz").map(n => n.toUpperCase())

11、初始化默认值:default value
var i: Int = _

12、作为参数名:

//访问map
var m3 = Map((1,100), (2,200))
for(e<-m3) println(e._1 + ": " + e._2)
m3 filter (e=>e._1>1)
m3 filterKeys (_>1)
m3.map(e=>(e._1*10, e._2))
m3 map (e=>e._2)

指代一个集合中的每个元素

例如我们要在一个Array a中筛出偶数,并乘以2:a.filter(_%2==0).map(2*_)。
又如要对缓冲数组ArrayBuffer b排序,可以这样:val bSorted = b.sorted(_)
在元组中,可以用方法_1, _2, _3访问组员。如(1,2)._2。其中句点可以用空格替代。

Note: 改成python语法的话就要使用lambda表达式了,或者直接使用列表解析。

13、参数序列:parameters Sequence
_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理。例如val s = sum(1 to 5:_*)就是将1 to 5当作参数序列处理。
//Range转换为List
List(1 to 5:_*)

//Range转换为Vector
Vector(1 to 5: _*)

//可变参数中
def capitalizeAll(args: String*) = {
  args.map { arg =>
    arg.capitalize
  }
}

val arr = Array("what's", "up", "doc?")
capitalizeAll(arr: _*)

这里需要注意的是,以下两种写法实现的是完全不一样的功能:

foo _               // Eta expansion of method into method value

foo(_)              // Partial function application

Example showing why foo(_) and foo _ are different:

trait PlaceholderExample {
  def process[A](f: A => Unit)

val set: Set[_ => Unit]

set.foreach(process _) // Error
  set.foreach(process(_)) // No Error
}

In the first case, process _ represents a method; Scala takes the polymorphic method and attempts to make it monomorphic by filling in the type parameter, but realizes that there is no type that can be filled in for A that will give the type (_ => Unit) => ? (Existential _ is not a type).

In the second case, process(_) is a lambda; when writing a lambda with no explicit argument type, Scala infers the type from the argument that foreach expects, and _ => Unit is a type (whereas just plain _ isn't), so it can be substituted and inferred.

This may well be the trickiest gotcha in Scala I have ever encountered.

[浅谈 Scala 中下划线的用途]

[Scala中的下划线到底有多少种应用场景?]

皮皮blog

from: http://blog.csdn.net/pipisorry/article/details/52913548

ref:

Scala:函数式编程之下划线underscore相关推荐

  1. 【编程语言】Scala 函数式编程

    函数是Scala 中的一等公民. 本文讨论Scala函数式编程的一些基本原理.你将会学到如何使用高阶函数,以及重用已有代码时,遵守 DRY 原则. Scala 的集合库很棒 # So what doe ...

  2. Scala函数式编程(三) scala集合和函数

    前情提要: scala函数式编程(二) scala基础语法介绍 scala函数式编程(二) scala基础语法介绍 前面已经稍微介绍了scala的常用语法以及面向对象的一些简要知识,这次是补充上一章的 ...

  3. Scala 函数式编程_部分应用函数_Partially Applied Functions

    2019独角兽企业重金招聘Python工程师标准>>> Scala 函数式编程部分应用函数或函数的部分应用 和其他遵循函数式编程范式的语言一样,Scala 允许部分应用一个函数. 调 ...

  4. scala函数式编程 educoder

    第一关:冒泡排序 本关任务:本关主题是利用Scala基础实现对冒泡排序算法的改进.使用冒泡算法存在这样一种情况:在第j(j<n-1)趟时就已排好序,但算法仍然执行后面几趟的比较.实际上,一旦算法 ...

  5. 【中英双语】高级Scala函数式编程

    [中英双语]高级Scala函数式编程 成为顶级 Scala 程序员,这样您就可以使用 Spark.Akka.Cats 或任何 Scala 工具!此教程共13.5小时,中英双语字幕,画质清晰无水印,源码 ...

  6. scala函数式编程_想要开始进行函数式编程,请进入scala

    scala函数式编程 意见 (Opinion) If you haven't used Scala yet, you're not the only one: Not even four percen ...

  7. 周末班补充视频Scala第2课:动手编写和运行自己的第一个Scala函数式编程的实例.

    周末班补充视频Scala第2课:动手编写和运行自己的第一个Scala函数式编程的实例. package com.dtspark.scala.functional.basics object MyFir ...

  8. scala函数式编程笔记: 纯函数式状态

    scala函数式编程:纯函数式状态读书笔记 Overview: 带状态的方法的声明式实现可能带有副作用,难以保持引用透明. 以纯函数式的方式实现带状态的函数的关键在于让状态更新是显式的,不要以副作用方 ...

  9. Scala函数式编程设计原理 第一课 编程范式(Programming Paradigms)

    我使用Scala有一两年的时间了,这门语言仿佛有一种魔力,让人用过就不想放手.Scala给我的整个程序生涯带来了非常深刻的影响,让我学会了函数式编程,让我知道了世界上居然还有这么一种优雅.高效.强大的 ...

  10. Scala 函数式编程

    一 函数式编程 1.1将函数值赋给变量 >>Scala中函数可以独立存在,不必像Java一样,还需要依附于类.而且我们可以直接将函数作为值赋给变量 >>Scala语法规定,将函 ...

最新文章

  1. 解决apache配置问题小结
  2. linux 图形界面 X Server 关闭 启动
  3. session和cache的区别是什么?
  4. linux cut列截取工具使用示例
  5. gitlab 安装_Linux学习14CentOS安装gitlab环境
  6. 30号晚直播丨数据操作加速器,CloudQuery v1.3.5 发布!
  7. iStack详解(三)——iStack多主检测方式
  8. [公告]博客迁移通知
  9. arm体系结构编程-入门介绍
  10. 《赛灵思中国通讯》学习记录第1期:为何使用Zynq SoC可以让企业产品利润激增
  11. 如何在excel表格中查找重复值
  12. mysql instead of_mysql unique option prefix myisam_recover instead of myisam-recover-options的解决方法...
  13. Jenkins | 搭建你第一个Jenkins应用
  14. 阿里云操作系统——飞天(Apsara)
  15. 在Letax中使用enumerate编辑 Step1 , Step2, ..... ,并设置缩进
  16. Linux中如何安装特定的gcc版本
  17. 【LEDE】x86软路由之路-12-浅谈复杂网络环境中的AP优化
  18. Oracle SQL 单引号与双引号区别
  19. 国科大.高级人工智能.2022期末考试真题回忆版
  20. win7桌面显示比例怎么设置【系统天地】

热门文章

  1. 通过WinForm控件创建的WPF控件无法输入的问题
  2. FFPLAY的原理(三)
  3. UVa 10400 记忆化搜索
  4. python核心编程课后习题解答第三章
  5. 推荐:免费万能视音频转换软件--格式工厂
  6. BZOJ 1030 [JSOI2007]文本生成器(ac自动机+dp)
  7. iOS 关于布局问题的一些认识
  8. JQuery canvas 验证码
  9. cocos2d-x 3.2 DrawNode 绘图API
  10. 加了2个皮肤的art dialog