alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (t3511.scala)

This example Scala source code file (t3511.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

array, array, list, list, test, test

The Scala t3511.scala source code




import scala.collection.immutable._


// ticket #3511
object Test {
  
  def main(args: Array[String]) {
    assert(Stream.from(0).view.force.take(5) == List(0, 1, 2, 3, 4))
    
    val s = Stream.from(0)
    val smap = s.view.map(_ * 2).force.take(5)
    assert(smap == List(0, 2, 4, 6, 8))
    
    val sfilter = s.view.filter(_ % 2 == 0).force.take(5)
    assert(sfilter == List(0, 2, 4, 6, 8))
    
    val sflatmap = s.view.flatMap(n => List(n, n * 2)).force.take(6)
    assert(sflatmap == List(0, 0, 1, 2, 2, 4))
    
    val stakewhile = s.view.takeWhile(_ < 10).force
    assert(stakewhile == List.range(0, 10))
    
    val szip = s.view.zip(s.map(_ / 2)).force.take(5)
    assert(szip == List((0, 0), (1, 0), (2, 1), (3, 1), (4, 2)))
    
    val szipall = s.view.zipAll(List(0, 1, 2), 0, 0).force.take(5)
    assert(szipall == List((0, 0), (1, 1), (2, 2), (3, 0), (4, 0)))
    
    val spatch = s.view.patch(1, List(5, 5, 5), 5).force.take(5)
    assert(spatch == List(0, 5, 5, 5, 6))
  }
  
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala t3511.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.