Pack consecutive duplicates of list elements into sublists.
If a list contains repeated elements they should be placed in separate sublists.
Example: scala> pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e))
res0: List[List[Symbol]] = List(List('a, 'a, 'a, 'a), List('b), List('c, 'c), List('a, 'a), List('d), List('e, 'e, 'e, 'e))

题目描述: 如果一个list中有相同的元素,则将相同的元素放到一个新的list中,最后返回list[list]

scala List span 函数:

定义:

final def span(p: (A) ⇒ Boolean): (List[A], List[A])
Splits this list into a prefix/suffix pair according to a predicate. Note: c span p is equivalent to (but possibly more efficient than) (c takeWhile p, c dropWhile p), provided the evaluation of the predicate p does not cause any side-effects.
returns
a pair consisting of the longest prefix of this list whose elements all satisfy p, and the rest of this list.
Definition Classes
List → LinearSeqOptimized → TraversableLike → GenTraversableLike
Annotations
@inline()

即span 根据输入的bool表达式,将list进行分割。返回一个list集合。但是碰到第一个不满足的元素,即返回。如:

list 的partition: 会遍历所有元素。

思路: 题目的要求是,连续的相等的元素放到同一个 list中,因此

使用span 进行分割。

 def packList[T](a:List[T]): List[List[T]] = {
def _pack(res:List[List[T]], tmp:List[T]):List[List[T]] = {
tmp match{
case Nil => res
case ls =>{
val (s:List[T], r:List[T]) = tmp span{_ == tmp.head}
_pack(res:::List(s), r)
}
}
}
_pack(List(), a)
}

scala 学习之:list span 用法的更多相关文章

  1. 【Todo】【读书笔记】大数据Spark企业级实战版 & Scala学习

    下了这本<大数据Spark企业级实战版>, 另外还有一本<Spark大数据处理:技术.应用与性能优化(全)> 先看前一篇. 根据书里的前言里面,对于阅读顺序的建议.先看最后的S ...

  2. 学习AngularJs:Directive指令用法(完整版)

    这篇文章主要学习AngularJs:Directive指令用法,内容很全面,感兴趣的小伙伴们可以参考一下   本教程使用AngularJs版本:1.5.3 AngularJs GitHub: http ...

  3. jQuery学习笔记之Ajax用法详解

    这篇文章主要介绍了jQuery学习笔记之Ajax用法,结合实例形式较为详细的分析总结了jQuery中ajax的相关使用技巧,包括ajax请求.载入.处理.传递等,需要的朋友可以参考下 本文实例讲述了j ...

  4. Scala学习(一)--Scala基础学习

    Scala基础学习 摘要: 在篇主要内容:如何把Scala当做工业级的便携计算器使用,如何用Scala处理数字以及其他算术操作.在这个过程中,我们将介绍一系列重要的Scala概念和惯用法.同时你还将学 ...

  5. Scala学习资源

    Scala学习资源: Scala官方网站:http://www.scala-lang.org/ Scala github:https://github.com/scala/scala Twitter ...

  6. scala学习笔记(4):占位符

    scala 中占位符的用法 1.作为“通配符”,类似Java中的*.如import scala.math._ 2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如val s = s ...

  7. 机器学习(三)--- scala学习笔记

    Scala是一门多范式的编程语言,一种类似Java的编程语言,设计初衷是实现可伸缩的语言.并集成面向对象编程和函数式编程的各种特性. Spark是UC Berkeley AMP lab所开源的类Had ...

  8. 【Scala】Scala学习资料

    Scala学习资料 java 树形 分类器_百度搜索 决策树分类器-Java实现 - CSDN博客 KNN分类器-Java实现 - CSDN博客 学习:java设计模式—分类 - 飞翔荷兰人 - 博客 ...

  9. Scala学习网址

    scala学习网址为:https://twitter.github.io/scala_school/zh_cn https://www.zhihu.com/question/26707124

  10. Spark之Scala学习

    1. Scala集合学习: http://blog.csdn.net/lyrebing/article/details/20362227 2. scala实现kmeans算法 http://www.t ...

随机推荐

  1. 好久没上cnblogs了

    以为登录不上了,原来还是可以登录上的,不错~~~上来标记一下

  2. (Collection)350. Intersection of Two Arrays II

    /* Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2 ...

  3. (Array)121. Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  4. 德国GFZ

    关于GFZ的介绍,图片中有,这里不赘述. 在下面的图片中介绍的,除了能够提供免费的数据支持外,就属左边的应用框架. 1.目前开源框架里,空间数据库多是postgis,根据数据量和组织方式,可以选择mo ...

  5. yii-basic-app-2.0.5/basic/config/web.php

    <?php $params = require(__DIR__ . '/params.php'); $config = [ 'id' => 'basic', 'basePath' => ...

  6. 关于android.view.WindowManager$BadTokenException问题出现以及解决的一些记录

    1.出现 在app showdialog()时偶尔会出现,根据stackoverflow.com的描述,貌似是show的时候用作context的activity以及destroy了,,,一些异步操作会 ...

  7. 64位系统装oracle(ora-12154 )

    装了n次的oracle,昨下午装服务器的oracle,结果遇到了一个问题,让我百思不得其解,但最终在大家的帮助下终于解决了. 我装的服务器是windows server 2007 64位的,装完ora ...

  8. Xenko基础API笔记2-手势

    交互: Drag Gesture Type : Continuous Configuration class: GestureConfigDrag Event class: GestureEventD ...

  9. 二叉树遍历(Binary Tree Traversal)

    二叉树的递归遍历比较简单,这里说一下非递归遍历,以中序遍历为例子. 非递归遍历主要用到栈来协助进行.对于一个二叉树,首先根节点入栈,如果有左儿子,则继续入栈,重复直到最左边的儿子,这时候此节点值为要遍 ...

  10. python中获取上一个月一号的方法

    业务场景: 我们经常会跑一些月级别或者周级别的报表. 周级别的报表还比较好确定,就是七天前的直接用timedelta(days=7)来获取开始日期就可以了; 但是月级别的报表就要麻烦一些,因为time ...