|
Scala example source code file (viewtest1.scala)
The Scala viewtest1.scala source codepackage test trait Ordered[a] { def < (x: a): Boolean } object O { implicit def view (x: String): Ordered[String] = new Ordered[String] { def < (y: String) = x.compareTo(y) < 0 } } object Empty extends Tree[Nothing] case class Node[c <% Ordered[c]](elem: c, l: Tree[c], r: Tree[c]) extends Tree[c] abstract class Tree[+a <% Ordered[a]] { def insert[b >: a <% Ordered[b]](x: b): Tree[b] = this match { case Empty => new Node(x, Empty, Empty) case Node(elem, l, r) => if (x == elem) this else if (x < elem) Node(elem, l insert x, r) else Node(elem, l, r insert x) } def elements: List[a] = this match { case Empty => List() case Node(elem, l, r) => l.elements ::: List(elem) ::: r.elements } } object Test { import O.view def main(args: Array[String]) { var t: Tree[String] = Empty for (s <- args) { t = t insert s } println(t.elements) } } Other Scala examples (source code examples)Here is a short list of links related to this Scala viewtest1.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.