-
-
Save debasishg/711e386a819f45c879fe71d49fed43f2 to your computer and use it in GitHub Desktop.
Revisions
-
coacoas revised this gist
Jun 28, 2012 . 2 changed files with 12 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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("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; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ List(one, two, three) 3 3 5 Vector(four, five, six) 4 4 3 -
coacoas revised this gist
Jun 28, 2012 . 2 changed files with 34 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
coacoas created this gist
Jun 28, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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>()); } }