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

Scala example source code file (iterators.scala)

This example Scala source code file (iterators.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, array/*, double, double, elements, elements, pair, pair

The Scala iterators.scala source code

package examples

object iterators {

  def Array(elems: Double*): Array[Double] = {
    val ar = new Array[Double](elems.length)
    for (i <- 0 until elems.length)
      ar(i) = elems(i)
    ar
  }

  def printArray(xs: Array[Double]) =
    xs.iterator foreach { x => println(x) }

  def findGreater(xs: Array[Double], limit: Double) =
    xs.iterator
      .zip(Iterator.from(0))
      .filter{case Pair(x, i) => x > limit }
      .map{case Pair(x, i) => i}

  def main(args: Array[String]) {
    val ar = Array/*[Double]*/(6, 2, 8, 5, 1)
    printArray(ar)
    println("Elements greater than 3.0:")
    findGreater(ar, 3.0) foreach { x => Console.println(ar(x)) }
  }

}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.