随笔分类 -  scala

scala language
摘要:接收类型参数的类和特质是“泛型”的,但是它们生成的类型是"参数化"。 ”泛型“的意思是我们用一个泛化的类或特质来定义许许多多具体的类型。 如果说S是类型T的子类型,那么Queue[S]应不应该被当作Queue[T]的子类型? 如果是,可以说Queue特质在类型参数T上是协变的(convariant) 阅读全文
posted @ 2019-08-20 11:07 嵌入式实操 阅读(160) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2019-08-15 10:09 嵌入式实操 阅读(125) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2019-08-15 08:57 嵌入式实操 阅读(140) 评论(0) 推荐(0)
摘要:collect will apply a partial function to all elements of a Traversable and return a different collection 阅读全文
posted @ 2019-08-14 15:14 嵌入式实操 阅读(379) 评论(0) 推荐(0)
摘要:The Scala collections library provides specialised implementations for Sets of fewer than 5 values (see the source). The iterators for these implement 阅读全文
posted @ 2019-08-14 14:58 嵌入式实操 阅读(154) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2019-08-14 14:43 嵌入式实操 阅读(525) 评论(0) 推荐(0)
摘要:最关键的部分是计算序列的同时,不会引发无限递归, #:: 表达式的右边只有在被请求时才会被求值。 阅读全文
posted @ 2019-07-16 10:38 嵌入式实操 阅读(142) 评论(0) 推荐(0)
摘要: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 阅读全文
posted @ 2019-07-15 09:03 嵌入式实操 阅读(87) 评论(0) 推荐(0)
摘要:result: 阅读全文
posted @ 2019-07-12 14:03 嵌入式实操 阅读(159) 评论(0) 推荐(0)
摘要:隐式类可以用来扩展对象的功能非常方便 example: 阅读全文
posted @ 2019-07-12 11:22 嵌入式实操 阅读(167) 评论(0) 推荐(0)
摘要:How to use type alias to name a Tuple2 pair into a domain type called CartItem type CartItem[Donut, Int] = Tuple2[Donut, Int] 阅读全文
posted @ 2019-07-12 11:13 嵌入式实操 阅读(150) 评论(0) 推荐(0)
摘要:Partial函数的定义 scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut" | "Strawberry Donut" => "Very Tasty"}isVeryTasty: Partial 阅读全文
posted @ 2019-07-12 10:59 嵌入式实操 阅读(139) 评论(0) 推荐(0)
摘要:可以使用scala库,可以从字面上看出是在调用 递归函数: code resule: 阅读全文
posted @ 2019-07-12 10:02 嵌入式实操 阅读(187) 评论(0) 推荐(0)
摘要:Ordering using andThen: f(x) andThen g(x) = g(f(x)) Ordering using compose: f(x) compose g(x) = f(g(x)) result: 阅读全文
posted @ 2019-07-12 09:50 嵌入式实操 阅读(89) 评论(0) 推荐(0)
摘要:Scala collection such as List or Sequence or even an Array to variable argument function using the syntax :_ *. code : result: 阅读全文
posted @ 2019-07-11 17:57 嵌入式实操 阅读(110) 评论(0) 推荐(0)
摘要:result 阅读全文
posted @ 2019-07-11 17:39 嵌入式实操 阅读(108) 评论(0) 推荐(0)
摘要:result: 实例对象 stack 只能接受整型值。然而,如果类型参数有子类型,子类型可以被传入: class Fruit class Apple extends Fruit class Banana extends Fruit val stack = new Stack[Fruit] val a 阅读全文
posted @ 2019-07-10 16:26 嵌入式实操 阅读(142) 评论(0) 推荐(0)
摘要:code: result: 阅读全文
posted @ 2019-07-10 16:14 嵌入式实操 阅读(116) 评论(0) 推荐(0)
摘要:result: 阅读全文
posted @ 2019-07-10 15:53 嵌入式实操 阅读(164) 评论(0) 推荐(0)
摘要:output: 阅读全文
posted @ 2019-07-10 14:52 嵌入式实操 阅读(197) 评论(0) 推荐(0)