stream vs. linq
TRANSCRIPT
async function aboutMe() {
let profile = { name: “Keisuke Kato” , twitter: “@k_kato” };
return await followTwitterAsync(profile);}
Stream vs. LINQ
‡Wikipedia, “Language Integrated Query”, https://en.wikipedia.org/wiki/Language_Integrated_Query‡Wikipedia, “Anders Hejlsberg”, https://en.wikipedia.org/wiki/Anders_Hejlsberg‡O'reilly, ”C#: Yesterday, Today, and Tomorrow: An Interview with Anders Hejlsberg, Part 1”, http://archive.oreilly.com/pub/a/dotnet/2005/10/17/interview-with-anders-hejlsberg.html
Stream LINQ
Language Java C#, F#, VB,Delphi
Since 2014 2007
Developer Oracle Microsoft
Influenced by ? SQL, Haskell
All youneed is
metaphors‡Wikipedia, George Lakoff and Mark Johnson, “Conceptual metaphor”, https://en.wikipedia.org/wiki/Conceptual_metaphor
Software Metaphors
‡Steve McConnell, “Code Complete, Second Edition”, https://www.safaribooksonline.com/library/view/code-complete-second/0735619670/ch02s03.html
David Gries science 1981
Donald Knuth art 1998
Watts Humphrey process 1989
P. J. Plauger and Kent Beck driving a car 1993, 2000
Alistair Cockburn game 2002
Eric Raymond bazaar 2000
Andy Hunt and DaveThomas
gardening 1999
Paul Heckel filming Snow White and theSeven Dwarfs
1994
Fred Brooks farming, huntingwerewolves, or drowningwith dinosaurs in a tar pit
1995
Stream
‡Google, “stream”, https://www.google.co.jp/search?q=stream&source=lnms&tbm=isch
LINQ
‡Google, “linq”, https://www.google.co.jp/search?q=linq&source=lnms&tbm=isch
‡Shueisha Bunko, “Kawano ngare no yoni”, http://bunko.shueisha.co.jp/yomi/0105_4.html‡LinQ, http://loveinq.com/
VS.
filter vs. Where Java C#
@Testpublic void filter() throws Exception {
// arrange List<Integer> list = Arrays.asList(1, 2, 3);
// act Stream<Integer> stream = list.stream().filter(x -> x == 1 || x == 3);
List<Integer> actual = stream.collect(Collectors.toList());
// assert assertEquals(true, actual.contains(1)); assertEquals(false, actual.contains(2)); assertEquals(true, actual.contains(3));}
[TestMethod]public void Where(){ // arrange List<int> list = new List<int>() { 1, 2, 3 }; // act IEnumerable<int> enumerable = list.Where(x => x == 1 || x == 3);
List<int> actual = enumerable.ToList();
// assert Assert.AreEqual(true, actual.Contains(1)); Assert.AreEqual(false, actual.Contains(2)); Assert.AreEqual(true, actual.Contains(3));}
sort vs. OrderBy Java C#
@Testpublic void orderBy() {
// arrange java.util.List<String> list = Arrays.asList("b", "c", "a");
// act Stream<String> stream = list.stream().sorted();
List<String> actual = stream.collect(Collectors.toList());
// assert assertEquals("a", actual.get(0)); assertEquals("b", actual.get(1)); assertEquals("c", actual.get(2));}
[TestMethod]public void Where(){ // arrange List<string> list = new List<string>() { “b”, “c”, “a” }; // act IEnumerable<string> enumerable = list.OrderBy(x => x);
List<string> actual = enumerable.ToList();
// assert Assert.AreEqual(“a”, actual[0]); Assert.AreEqual(“b”, actual[1]); Assert.AreEqual(“c”, actual[2]);}
‡Shueisha Bunko, “Kawano ngare no yoni”, http://bunko.shueisha.co.jp/yomi/0105_4.html‡LinQ, http://loveinq.com/
Stream + LINQ = jLinqer
‡Qiita, “Java に C# の LINQ を移植してみた – jLinqer”, http://qiita.com/k--kato/items/ec7ab8b392fa8bb0a732
Stream + LINQ = jLinqer
‡GutHub, “jLinqer”, https://github.com/k--kato/jLinqer
LINQ(C#) jLinqer(Java) Stream(Java)
ThenBy thenBy n/a
ThenByDescending thenByDescending n/a
SkipWhile skipWhile n/a
TakeWhile takeWhile n/a
Intersect intersect n/a
Union union n/a
Except except n/a
Reverse reverse n/a
SequenceEqual sequenceEqual n/a
OfType ofType n/a
ElementAt elementAt n/a
jLinqer @ Maven Central
‡Maven Central, “jLinqer”, https://oss.sonatype.org/content/groups/public/com/github/jlinqer/jlinqer/0.1.0/‡Sonatype. “Community Support - Open Source Project Repository HostingOSSRH-16339”, https://issues.sonatype.org/browse/OSSRH-16339
<dependency> <groupId>com.github.jlinqer</groupId> <artifactId>jlinqer</artifactId> <version>0.1.0</version></dependency>
Type-Erasure Problem
‡stackoverflow, “Generic method to perform a map-reduce operation. (Java-8)”, http://stackoverflow.com/questions/30826674/generic-method-to-perform-a-map-reduce-operation-java-8