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

Scala example source code file (fromjava.java)

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

a, b, b, contra, function1, function1, integer, integer, module, object, object, ordering, string, traversable

The Scala fromjava.java source code

import scala.*;
import scala.math.Ordering;
import scala.math.Numeric;
import scala.collection.Seq;
import scala.collection.Traversable;
import scala.collection.Traversable$;
import scala.collection.immutable.Set;
import scala.collection.immutable.HashSet;
import scala.collection.immutable.Map;
import scala.collection.immutable.Map$;
import scala.collection.immutable.HashMap;
import scala.collection.immutable.Vector;
import scala.collection.immutable.List;
import scala.collection.generic.CanBuildFrom;

class A { };
class B { };

// This one compiles but it would be better if it didn't.
// Checking in under pos anyway in the interests of making sure
// we are informed if the status changes.
class Contra {
  // Not an Ordering<Character>.
  static Ordering<Object> charOrd = scala.math.Ordering$Char$.MODULE$;
  
  public boolean useCharOrd() {
    return charOrd.compare(new Object(), new Object()) == 0;
  }
  
  static Numeric<?> intNum = scala.math.Numeric$IntIsIntegral$.MODULE$;
}

public class fromjava {
  public static Function1<A, B> f1 = new scala.runtime.AbstractFunction1() {
    public B apply(A a) {
      return null;
    }
  };
  
  public static Function1<Tuple2 f2 = new scala.runtime.AbstractFunction1, B>() {
    public B apply(Tuple2<? extends Object, B> tup) {
      return tup._2();
    }
  };
  
  public static String vector(Vector<String> x) {
    Vector<String> y = x.take(2);
    return y.head();
  }
  public static String list(List<String> x) {
    List<String> y = x.drop(2);
    return y.head();
  }
  public static Tuple2<String, Integer> map(Map x) {
    Traversable<Tuple2 y = x.drop(2);
    return y.head();
  }
  public static <T> Object sum(Traversable x) {
    return x.sum(Contra.intNum);
  }
  // Looks like sum as given below fails under java5, so disabled.
  //
  // [partest] testing: [...]/files/pos/javaReadsSigs                                [FAILED]
  // [partest] files/pos/javaReadsSigs/fromjava.java:62: name clash: sum(scala.collection.Traversable<A>) and sum(scala.collection.Traversable) have the same erasure
  // [partest]   public static B sum(Traversable<A> x) {
  // [partest]                   ^
  //
  //
  // can't make this work with an actual CanBuildFrom: see #4389.
  // public static B sum(Traversable<A> x) {
  //   // have to cast it unfortunately: map in TraversableLike returns
  //   // "That" and such types seem to be signature poison.
  //   return ((Traversable<B>)x.map(f1, null)).head();
  // }
}

Other Scala examples (source code examples)

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