【scala初学】Trait Traversable


collections.png

     Traversable是所有collections的父类

     Traversable是所有集合层级的最顶端的,他只有一个抽象方法, foreach:

1
def  foreach[U](f :  Elem  = > U)

     实现Traversable只需要定义这个方法,其他方法可以直接继承得到。

foreach 方法以为遍历集合中的所有元素,并且应用传入的方法,f , 对每一个元素。 函数(或者方法,操作)的类型是Elem => U, Elem 是集合元素的类型 而 U是一个任意的结果类型.  函数的f的调用是他的一个副作用,事实上函数的结果都会被 foreach方法丢弃。



  函数分类:Traversable 的方法可以分类如下:

Addition:

         方法名: ++ 

      说明: 同时追加两个traversable,或者追加一个迭代的所有元素到traversable


Map : 

      方法名: map, flatMap, collect

        说明: 通过应用函数到集合的元素中,来产生一个新的集合


Conversions :

      方法名:toArraytoListtoIterabletoSeqtoIndexedSeqtoStreamtoSet,toMap

   说明: 使得 Traversable 变的更加具体。所有这些转换返回的接收器的参数不会改变,如果集合的运行时类型已经匹配了要求的集合类型。 如: 在一个列表中应用 toList,将会调用该列表的 toList方法。


Copying operations :

      方法名 : copyToBuffer,  copyToArray

   说明:作用如他们的名字


Size info:

      方法名:isEmptynonEmptysize, and hasDefiniteSize

   说明:作用如其名,唯一需要说明的是hasDefiniteSize  ,判断一个集合是否有穷还是无穷


Element retrieval: 

      方法名:headlastheadOptionlastOption, and find.

      说明: 头尾方法的定义.  但是有些集合不能很好的定义头尾方法。 如,hash存储是key,value, 又如某些运行时会不断改变元素顺序的。 


Sub-collection retrieval operations :

      方法名: tailinitslicetakedroptakeWhile,dropWhilefilterfilterNotwithFilter

   说明:返回子集合


Subdivision operations : 

      方法名: plitAtspanpartitiongroupBy

   说明:分割集合成几个子集合


Element tests: 

     方法名:existsforallcount

     说明:测试断言(书生:即方法做的事情)是否成立


Folds :

     方法名:foldLeftfoldRight/::\reduceLeftreduceRight

     说明:用二进制的方式操作连续的元素


Specific folds : 

     方法名: sumproductminmax    

     说明:用于特定类型,如数字


String :

    方法名: mkStringaddStringstringPrefix

  说明:转换一个集合到字符串的方式


View:

     方法名: view

   说明:这个方法将单独介绍




Class Traversable中的方法

WHAT IT ISWHAT IT DOES
Abstract Method:
xs foreach fExecutes function f for every element of xs.
Addition:
xs ++ ysA collection consisting of the elements of both xs and ysys is a TraversableOnce collection, i.e., either a Traversable or an Iterator.
Maps:
xs map f The collection obtained from applying the function f to every element in xs.
xs flatMap fThe collection obtained from applying the collection-valued function f to every element in xs and concatenating the results.
xs collect fThe collection obtained from applying the partial function f to every element in xs for which it is defined and collecting the results.
Conversions:
xs.toArrayConverts the collection to an array.
xs.toListConverts the collection to a list.
xs.toIterableConverts the collection to an iterable.
xs.toSeqConverts the collection to a sequence.
xs.toIndexedSeqConverts the collection to an indexed sequence.
xs.toStreamConverts the collection to a lazily computed stream.
xs.toSetConverts the collection to a set.
xs.toMapConverts the collection of key/value pairs to a map. If the collection does not have pairs as elements, calling this operation results in a static type error.
Copying:
xs copyToBuffer bufCopies all elements of the collection to buffer buf.
xs copyToArray(arr, s, n)Copies at most n elements of the collection to array arr starting at index s. The last two arguments are optional.
Size info:
xs.isEmptyTests whether the collection is empty.
xs.nonEmptyTests whether the collection contains elements.
xs.sizeThe number of elements in the collection.
xs.hasDefiniteSizeTrue if xs is known to have finite size.
Element Retrieval:
xs.headThe first element of the collection (or, some element, if no order is defined).
xs.headOptionThe first element of xs in an option value, or None if xs is empty.
xs.lastThe last element of the collection (or, some element, if no order is defined).
xs.lastOptionThe last element of xs in an option value, or None if xs is empty.
xs find pAn option containing the first element in xs that satisfies p, or None if no element qualifies.
Subcollections:
xs.tailThe rest of the collection except xs.head.
xs.initThe rest of the collection except xs.last.
xs slice (from, to)A collection consisting of elements in some index range of xs (from from up to, and excluding to).
xs take nA collection consisting of the first n elements of xs (or, some arbitrary n elements, if no order is defined).
xs drop nThe rest of the collection except xs take n.
xs takeWhile pThe longest prefix of elements in the collection that all satisfy p.
xs dropWhile pThe collection without the longest prefix of elements that all satisfy p.
xs filter pThe collection consisting of those elements of xs that satisfy the predicate p.
xs withFilter pA non-strict filter of this collection. Subsequent calls to mapflatMapforeach, and withFilter will only apply to those elements of xs for which the condition p is true.
xs filterNot pThe collection consisting of those elements of xs that do not satisfy the predicate p.
Subdivisions:
xs splitAt nSplit xs at a position, giving the pair of collections (xs take n, xs drop n).
xs span pSplit xs according to a predicate, giving the pair of collections (xs takeWhile p, xs.dropWhile p).
xs partition pSplit xs into a pair of two collections; one with elements that satisfy the predicate p, the other with elements that do not, giving the pair of collections (xs filter p, xs.filterNot p)
xs groupBy fPartition xs into a map of collections according to a discriminator function f.
Element Conditions:
xs forall pA boolean indicating whether the predicate p holds for all elements of xs.
xs exists pA boolean indicating whether the predicate p holds for some element in xs.
xs count pThe number of elements in xs that satisfy the predicate p.
Folds:
(z /: xs)(op)Apply binary operation op between successive elements of xs, going left to right and starting with z.
(xs :\ z)(op)Apply binary operation op between successive elements of xs, going right to left and starting with z.
xs.foldLeft(z)(op)Same as (z /: xs)(op).
xs.foldRight(z)(op)Same as (xs :\ z)(op).
xs reduceLeft opApply binary operation op between successive elements of non-empty collection xs, going left to right.
xs reduceRight opApply binary operation op between successive elements of non-empty collection xs, going right to left.
Specific Folds:
xs.sumThe sum of the numeric element values of collection xs.
xs.productThe product of the numeric element values of collection xs.
xs.minThe minimum of the ordered element values of collection xs.
xs.maxThe maximum of the ordered element values of collection xs.
Strings:
xs addString (b, start, sep, end)Adds a string to StringBuilder b that shows all elements of xs between separators sep enclosed in strings start and endstartsepend are all optional.
xs mkString (start, sep, end)Converts the collection to a string that shows all elements of xs between separators sep enclosed in strings start and endstartsepend are all optional.
xs.stringPrefixThe collection name at the beginning of the string returned from xs.toString.
Views:
xs.viewProduces a view over xs.
xs view (from, to)Produces a view that represents the elements in some index range of xs.









本文转自 wws5201985 51CTO博客,原文链接:http://blog.51cto.com/yjplxq/1428183,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值