Skip to content

Instantly share code, notes, and snippets.

@debasishg
Forked from coacoas/CollectionTest.java
Created August 24, 2018 09:40
Show Gist options
  • Save debasishg/711e386a819f45c879fe71d49fed43f2 to your computer and use it in GitHub Desktop.
Save debasishg/711e386a819f45c879fe71d49fed43f2 to your computer and use it in GitHub Desktop.

Revisions

  1. @coacoas coacoas revised this gist Jun 28, 2012. 2 changed files with 12 additions and 4 deletions.
    10 changes: 9 additions & 1 deletion CollectionTest.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@

    package org.example;

    import scala.Function1;
    @@ -19,7 +20,7 @@ public static void main(String[] args) {
    strings.map(length, ListBuilder.<String, Integer>newCBF()).foreach(println);


    Vector<String> stringv = Vector$.MODULE$.empty().appendBack("one").appendBack("two").appendBack("three");
    Vector<String> stringv = vector("four", "five", "six");
    System.out.println(stringv.toString());
    Vector<Integer> outv = stringv.map(length, VectorBuilder.<String, Integer>newCBF());
    outv.foreach(println);
    @@ -89,4 +90,11 @@ public final static <A> List<A> list(A... as) {
    return l;
    }

    public final static <A> Vector<A> vector(A... as) {
    Vector<A> v = Vector$.MODULE$.empty();
    for (A a : as) {
    v = v.appendBack(a);
    }
    return v;
    }
    }
    6 changes: 3 additions & 3 deletions output
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ List(one, two, three)
    3
    3
    5
    Vector(one, two, three)
    Vector(four, five, six)
    4
    4
    3
    3
    5
  2. @coacoas coacoas revised this gist Jun 28, 2012. 2 changed files with 34 additions and 23 deletions.
    49 changes: 26 additions & 23 deletions CollectionTest.java
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,24 @@

    public class ScalaCollectionTest {

    // With proper setup, it doesn't seem too bad.

    public static void main(String[] args) {
    List<String> strings = list("one", "two", "three");
    System.out.println(strings.toString());
    strings.map(length, ListBuilder.<String, Integer>newCBF()).foreach(println);


    Vector<String> stringv = Vector$.MODULE$.empty().appendBack("one").appendBack("two").appendBack("three");
    System.out.println(stringv.toString());
    Vector<Integer> outv = stringv.map(length, VectorBuilder.<String, Integer>newCBF());
    outv.foreach(println);
    }

    /******************************************************/
    /*** Pay no attention to the man behind the curtain ***/
    /******************************************************/

    public final static Function1<String, Integer> length = new AbstractFunction1<String, Integer>() {

    @Override
    @@ -19,6 +37,14 @@ public Integer apply(String arg0) {
    }
    };

    public final static Function1<Integer, Void> println = new AbstractFunction1<Integer, Void>() {
    @Override
    public Void apply(Integer arg0) {
    System.out.println(arg0);
    return null;
    }
    };

    final static class VectorBuilder {
    public static <Source, Target> CanBuildFrom<Vector<Source>, Target, Vector<Target>> newCBF() {
    return new CanBuildFrom<Vector<Source>, Target, Vector<Target>>() {
    @@ -62,28 +88,5 @@ public final static <A> List<A> list(A... as) {
    }
    return l;
    }

    public final static class PrintLn<A> extends AbstractFunction1<A, Void> {

    @Override
    public Void apply(A arg0) {
    System.out.println(arg0.toString());
    return null;
    }

    }

    public static void main(String[] args) {

    List<String> strings = list("one", "two", "three");
    System.out.println(strings.toString());
    strings.map(length, ListBuilder.<String, Integer>newCBF()).foreach(new PrintLn<Integer>());


    Vector<String> stringv = Vector$.MODULE$.empty().appendBack("one").appendBack("two").appendBack("three");
    System.out.println(stringv.toString());
    Vector<Integer> outv = stringv.map(length, VectorBuilder.<String, Integer>newCBF());
    outv.foreach(new PrintLn<Integer>());
    }

    }
    8 changes: 8 additions & 0 deletions output
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    List(one, two, three)
    3
    3
    5
    Vector(one, two, three)
    3
    3
    5
  3. @coacoas coacoas created this gist Jun 28, 2012.
    89 changes: 89 additions & 0 deletions CollectionTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    package org.example;

    import scala.Function1;
    import scala.collection.generic.CanBuildFrom;
    import scala.collection.immutable.List;
    import scala.collection.immutable.List$;
    import scala.collection.immutable.Vector;
    import scala.collection.immutable.Vector$;
    import scala.collection.mutable.Builder;
    import scala.runtime.AbstractFunction1;

    public class ScalaCollectionTest {

    public final static Function1<String, Integer> length = new AbstractFunction1<String, Integer>() {

    @Override
    public Integer apply(String arg0) {
    return arg0.length();
    }
    };

    final static class VectorBuilder {
    public static <Source, Target> CanBuildFrom<Vector<Source>, Target, Vector<Target>> newCBF() {
    return new CanBuildFrom<Vector<Source>, Target, Vector<Target>>() {
    Vector<Target> v = Vector$.MODULE$.<Target>empty();

    @Override
    public Builder<Target, Vector<Target>> apply() {
    return v.newBuilder();
    }

    @Override
    public Builder<Target, Vector<Target>> apply(Vector<Source> arg0) {
    return v.newBuilder();
    }
    };
    }
    }

    final static class ListBuilder {
    public static <Source, Target> CanBuildFrom<List<Source>, Target, List<Target>> newCBF() {
    return new CanBuildFrom<List<Source>, Target, List<Target>>() {
    List<Target> v = List$.MODULE$.<Target>empty();

    @Override
    public Builder<Target, List<Target>> apply() {
    return v.newBuilder();
    }

    @Override
    public Builder<Target, List<Target>> apply(List<Source> arg0) {
    return v.newBuilder();
    }
    };
    }
    }

    public final static <A> List<A> list(A... as) {
    List<A> l = List$.MODULE$.empty();
    for (int i = as.length - 1; i >= 0; i--) {
    l = l.$colon$colon(as[i]);
    }
    return l;
    }

    public final static class PrintLn<A> extends AbstractFunction1<A, Void> {

    @Override
    public Void apply(A arg0) {
    System.out.println(arg0.toString());
    return null;
    }

    }

    public static void main(String[] args) {

    List<String> strings = list("one", "two", "three");
    System.out.println(strings.toString());
    strings.map(length, ListBuilder.<String, Integer>newCBF()).foreach(new PrintLn<Integer>());


    Vector<String> stringv = Vector$.MODULE$.empty().appendBack("one").appendBack("two").appendBack("three");
    System.out.println(stringv.toString());
    Vector<Integer> outv = stringv.map(length, VectorBuilder.<String, Integer>newCBF());
    outv.foreach(new PrintLn<Integer>());
    }

    }