随笔分类 - scala
scala language
摘要:接收类型参数的类和特质是“泛型”的,但是它们生成的类型是"参数化"。 ”泛型“的意思是我们用一个泛化的类或特质来定义许许多多具体的类型。 如果说S是类型T的子类型,那么Queue[S]应不应该被当作Queue[T]的子类型? 如果是,可以说Queue特质在类型参数T上是协变的(convariant)
阅读全文
摘要:object Twice { def apply(x: Int): Int = x * 2 def unapply(z: Int): Option[Int] = if (z % 2 == 0) Some(z / 2) else None } object TwiceTest extends Application { val x = Twice(21) x match { ca...
阅读全文
摘要:collect will apply a partial function to all elements of a Traversable and return a different collection
阅读全文
摘要:The Scala collections library provides specialised implementations for Sets of fewer than 5 values (see the source). The iterators for these implement
阅读全文
摘要:If two Iterables aren't the same size, then zipAll can provide fillers for what it couldn't find a complement for. e.g. Iterable(x1, x2, x3) zipAll (I
阅读全文
摘要:最关键的部分是计算序列的同时,不会引发无限递归, #:: 表达式的右边只有在被请求时才会被求值。
阅读全文
摘要:The view method will create a non-strict version of the collection which means that the elements of the collection will only be made available at acce
阅读全文
摘要:result:
阅读全文
摘要:隐式类可以用来扩展对象的功能非常方便 example:
阅读全文
摘要:How to use type alias to name a Tuple2 pair into a domain type called CartItem type CartItem[Donut, Int] = Tuple2[Donut, Int]
阅读全文
摘要:Partial函数的定义 scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut" | "Strawberry Donut" => "Very Tasty"}isVeryTasty: Partial
阅读全文
摘要:可以使用scala库,可以从字面上看出是在调用 递归函数: code resule:
阅读全文
摘要:Ordering using andThen: f(x) andThen g(x) = g(f(x)) Ordering using compose: f(x) compose g(x) = f(g(x)) result:
阅读全文
摘要:Scala collection such as List or Sequence or even an Array to variable argument function using the syntax :_ *. code : result:
阅读全文
摘要:result
阅读全文
摘要:result: 实例对象 stack 只能接受整型值。然而,如果类型参数有子类型,子类型可以被传入: class Fruit class Apple extends Fruit class Banana extends Fruit val stack = new Stack[Fruit] val a
阅读全文
摘要:code: result:
阅读全文
摘要:output:
阅读全文
浙公网安备 33010602011771号