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

Scala example source code file (tcpoly_infer_ticket1864.scala)

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

app, array, arraybuffer, b, b, buffer, buffer, int, richbuffer, s, s, t, t

The Scala tcpoly_infer_ticket1864.scala source code

import scala.collection.mutable.{Buffer, ArrayBuffer}

class RichBuffer[T, B[U] <: Buffer[U]](buffer: Buffer[T]) {
  def mymap[S](f: T => S)(implicit rv: B[S]): B[S] = {
    buffer.foreach{ e =>
      rv += f(e)
    }
    rv
  }
}

object App {
  def mymap2[T, B[U] <: Buffer[U], S](buffer: B[T], f: T => S)(implicit rv: B[S]): B[S] = {
    buffer.foreach{ e =>
      rv += f(e)
    }
    rv
  }

  def mymap3[T, B <: Buffer[T], S](buffer: B, f: T => T)(implicit rv: B): B = {
    buffer.foreach{ e =>
      rv += f(e)
    }
    rv
  }

  def mymap4[T, B[U] <: Buffer[U], S](buffer: B[T])(f: T => S) (implicit rv: B[S]): B[S] = {
    buffer.foreach{ e =>
      rv += f(e)
    }
    rv
  }


  def main(args: Array[String]) {
    implicit def richBuffer[T, B[U] <: Buffer[U]](buffer: B[T]): RichBuffer[T, B] =
      new RichBuffer[T, B](buffer)

    implicit val rv = new ArrayBuffer[Int]
    val buf = new ArrayBuffer[Int]
    (1 to 5).foreach(buf += _)
    buf.mymap(x => x*x)
    richBuffer(buf).mymap[Int](x => x*x)
    richBuffer[Int, ArrayBuffer](buf).mymap[Int](x => x*x)
    mymap2(buf, (x: Int) => x*x)
    mymap2[Int, ArrayBuffer, Int](buf, (x: Int) => x*x)
    // mymap3(buf, x => x*x)                                     // compiler error
    mymap3(buf, (x: Int) => x*x)
    mymap4(buf)(x => x*x)
  }
}

Other Scala examples (source code examples)

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