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

Scala example source code file (view-iterator-stream.scala)

This example Scala source code file (view-iterator-stream.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

id, int, int, list, list, perturber, perturber, perturberfn, stream, string, test, traversableonce, traversableonce, unit

The Scala view-iterator-stream.scala source code

import scala.collection.{ mutable, immutable, generic }
import collection.TraversableView

object Test {
  type PerturberFn[T] = TraversableOnce[T] => TraversableOnce[T]
  lazy val Id = new Perturber(Nil, identity[TraversableOnce[Int]] _) { }
  class Perturber(val labels: List[String], val f: PerturberFn[Int]) extends PerturberFn[Int] {
    def apply(xs: TraversableOnce[Int]): TraversableOnce[Int] = f(xs)
    def show(xs: TraversableOnce[Int]): String = {
      val res       = f(xs)
      val resString = "" + res
      val rest      = res.toTraversable
      val failed    = (rest take 100).size == 100

      "%-45s %-30s %s".format(toString, resString,
        if (failed) "<failed>" else rest.mkString(" ")
      )
    }
    def and(g: Perturber): Perturber =
      new Perturber(this.labels ++ g.labels, f andThen g.f)
    
    override def toString = labels mkString " -> "
  }
  object Perturber {
    def apply(label: String, f: PerturberFn[Int]) = new Perturber(List(label), f)
  }

  def naturals = Stream from 1
  val toV : Perturber = Perturber("view", _.toTraversable.view)
  val toI : Perturber = Perturber("toIterator", _.toIterator)
  val toS : Perturber = Perturber("toStream", _.toStream)
  val toIS : Perturber = Perturber("toIndexedSeq", _.toIndexedSeq)

  def p(ps: Perturber*): Perturber = if (ps.isEmpty) Id else ps.reduceLeft(_ and _)
  def drop(n: Int): Perturber = Perturber("drop " + n, _.toIterator drop n)
  def take(n: Int): Perturber = Perturber("take " + n, _.toIterator take n)
  def slice(from: Int, until: Int): Perturber =
    Perturber(
      "slice(%d, %d)".format(from, until),
      _.toTraversable.slice(from, until)
    )
  
  val fns = List[Perturber](toV, toI, toS, toIS)

  def tds(n: Int): Perturber = p(drop(n), take(n / 2), slice(1, n / 4))
  def dts(n: Int): Perturber = p(take(n), drop(n / 2), slice(1, n / 4))
  def sdt(n: Int): Perturber = p(slice(n, n * 2), drop(n / 2), take(n / 4))
  def std(n: Int): Perturber = p(slice(n, n * 2), take(n / 2), drop(n / 4))
  
  val transforms = (fns.permutations map (xs => p(xs take 3: _*))).toList.distinct
  def mkOps(n: Int) = List[Perturber](tds(n), dts(n), sdt(n), std(n))
  def runOps(n: Int) = {
    val xs: List[(String, List[String])] = mkOps(n) map { op =>
      ("" + op, transforms map (_ show op(naturals)) sorted)
    }
    for ((k, v) <- xs) {
      println("\n** " + k + " **\n")
      println("-------------------")
      v foreach println
    }
    ()
  }
  
  def main(args: Array[String]): Unit = {
    runOps(20)
  }
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala view-iterator-stream.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.