Scala 函数式程序设计原理(4)--Types and Pattern Matching

4.1 Objects Everywhere

Pure Object Orientation:

A pure object-oriented language is one in which every value is an object.

If the language is based on classes, this means that the type of each value is a class.

4.2 Functions as Objects

(x: Int) => x * xis expanded to: { class AnonFun extends Function1[Int, Int] {  def apply(x: Int) = x * x  }  new AnonFun}or, shorter, using anonymous class syntax:new Function1[Int, Int] {  def apply(x: Int) = x * x}

Note that a method such as

def f(x: Int): Boolean = ...

is not itself a function value.

But if f is used in a place where a Function type is expected, it is converted automatically to the function value

(x: Int) => f(x)

or, expanded:

new Function1[Int, Boolean] {
    def apply(x: Int) = f(x)
}

eta-expansion: the transformation that converts the name F to this anonymous function, is called in lambdacalculus, eta expansion.

4.3 Subtyping and Generics

  • S <: T means: S is a subtype of T, and
  • S >: T means: S is a supertype of T, or T is a subtype of S.

[S >: NonEmpty <: IntSet]

would restrict S any type on the interval between NonEmpty and IntSet.

The Linkov Substitution Principle:

The following principle, started by Barbara Liskov, tell us when a type can be a subtype of another.

  If A <: B, then everything one can to do with a value of type B one should also be able to do with a value of type A.

The actual definition Liskov used is a bit more formal. It says:

  Let q(x) be a property provable about objects x of type B. Then q(y) should be provable for objects y of type A where A <: B.

4.4 Variance

Say C[T] is a parameterized type and A,B are types such that A <: B.

In general, there are three possible relationships between C[A] and C[B]:

C[A] <: C[B]                     C is covariant

C[A] >: C[B]                     C is contravariant

neither C[A] nor C[B] is a subtype of the other    C is nonvariant

Scala lets you declare the variance of a type by annotating the type parameter:

class C[+A] { ... }  C is covariant

class C[-A] { ... }   C is contravariant

class C[A] { ... }   C is nonvariant

Typing Rules for Functions

Generaly, we have the following rulw for subtyping between function types:

  if A2 <: A1 and B1 <: B2, then

  (A1 => B1) <: (A2 => B2)

So functions are contravariant in their argument type(s) and covariant in their result type.

  • covariant type parameters can only appear in method results
  • contravariant type parameters can only appear in method parameters
  • invariant type parameters can appear anywhere

4.5 Decomposition

4.6 Pattern Matching

Match Syntax:

Rules:

  • match is followed by a sequence of cases, pat => expr.
  • Each case associates an expression expr with a pattern pat.
  • A MarchError exception si thrown if no pattern matches the value of the selector.
e match {
    case pat1 => expr1     ...    ...  case patn => exprn
}

What Do Patterns Match?

  • A constructor pattern C(p1, ..., pn) matches all the values of type C (or a subtype) that have been constructed with arguments matching the patterns p1, ..., pn.
  • A variable pattern x matches any value, and binds the name of the variable to this value
  • A constant pattern c matches values that are equal to c( in the sense of ==)

4.7 Lists

The list is a fundamental data structure in functional programming.

A list having x1, ..., xn as elements is written List(x1, ..., xn)

时间: 2024-10-11 08:38:30

Scala 函数式程序设计原理(4)--Types and Pattern Matching的相关文章

Scala 函数式程序设计原理(6)--Collections

6.1 Other Collections Operations on Vectors: Vectors are created analogously to lists: val nums = Vector(1, 2, 3, -88) val peoplr = Vector("Bob", "James", "Peter") They support the same operations as lists, with the exception

Scala 函数式程序设计原理(1)

课程地址:https://www.coursera.org/learn/progfun1/home/welcome 1.1 Programming Paradigms In a restricted sense, a functional programming language is one which does not have mutable variables, or imperative control structures. In a wider sense, a functiona

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

我使用Scala有一两年的时间了,这门语言仿佛有一种魔力,让人用过就不想放手.Scala给我的整个程序生涯带来了非常深刻的影响,让我学会了函数式编程,让我知道了世界上居然还有这么一种优雅.高效.强大的语言. Scala在国外已经非常流行,但是不知为何,在国内总是不温不火,在此,我特别想为Scala这门语言在国内的发展做一些事情.不才不敢谈Scala的编程经验,因为要成为Scala大神还有很长的路要走,只好翻译一份Scala视频教程以飨读者,大家发现有误的地方,请多多批评指教. 这个视频的作者是S

Scala函数式编程进阶

1 package com.dtspark.scala.basics 2 3 /** 4 * 函数式编程进阶: 5 * 1,函数和变量一样作为Scala语言的一等公民,函数可以直接赋值给变量: 6 * 2, 函数更长用的方式是匿名函数,定义的时候只需要说明输入参数的类型和函数体即可,不需要名称,但是如果你要使用的话,一般会把这个匿名函数赋值给一个变量(其实是val常量),Spark源码中大量存在这种语法,必须掌握: 7 * 3, 函数可以作为参数直接传递给函数,这极大的简化的编程的语法,为什么这

Beginning Scala study note(5) Pattern Matching

The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters and pattern matching. 1. Basic Pattern Matching In Scala, your cases can include types, wildcards, sequences, regular expressions, and so forth. scal

第3课 Scala函数式编程彻底精通及Spark源码阅读笔记

本课内容: 1:scala中函数式编程彻底详解 2:Spark源码中的scala函数式编程 3:案例和作业 函数式编程开始: def fun1(name: String){ println(name) } //将函数名赋值给一个变量,那么这个变量就是一个函数了. val fun1_v = fun1_ 访问 fun1_v("Scala") 结果:Scala 匿名函数:参数名称用 => 指向函数体 val fun2=(content: String) => println(co

虚拟网卡TUN/TAP 驱动程序设计原理

昨天韦哥写了<Linux下Tun/Tap设备通信原理>一文,只提到了两个使用Tun的用户进程之间的通信路径,并没有说明Tun虚拟网卡驱动是如何实现的,而正好看到了这里的一篇讲解这方面的文章,果断转载了,感谢作者,原文在这里:虚拟网卡TUN/TAP 驱动程序设计原理 简介 虚拟网卡Tun/tap驱动是一个开源项目,支持很多的类UNIX平台,OpenVPN和Vtun都是基于它实现隧道包封装.本文将介绍tun/tap驱动的使用并分析虚拟网卡tun/tap驱动程序在linux环境下的设计思路. tun

scala akka 修炼之路6(scala函数式柯里化风格应用场景分析)

胜败兵家事不期,包羞忍耻是男儿--斗牛士fighting,fighting,fighting... 小象学习和使用scala也一段时间了,最初小象学习scala主要为了学习spark生态,但是深入学习scala的一些特性后,深深被scala函数式和面向对象的风格所折服,不得不赞美设计这门语言的设计者.小象大学阶段在使用MATLAB做数据分析和自动化设计时,就非常喜欢使用MATLAB的命令行和面向矩阵运算的风格编写分析代码:喜欢使用java编写层次化和清晰的模块接口,而这些Scala语言设计中都有

Programming in Scala (Second Edition) 读书笔记15 case class and pattern matching

一个算术表达式包含: 数字,变量,二元操作符,一元操作符.用下面几个类来模拟它们 package chapter15 abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr case class UnOp(operator: String, arg: Expr) extends Expr case class BinOp(operator: