Java 8: No more loops
21–30 of 99 posts
Re: Java 8: No more loops
#22[deleted]
It's mostly the same semantics, but it costs an extra object for each element in your list. If the only thing you're going to do is sum those longs or serialize them over the wire or something, the extra 6 characters have a significant impact on run-time. There's lots of solutions in this space (e.g. Rust is a static language that uses a lot of "zero-cost" abstractions like tagged pointers that it can prove are safe specifically because of the static types), but Java's "make the programmer do it explicitly" isn't so bad for 1995.
Re: Java 8: No more loops
#23 public IList getDistinctTags(IEnumerable articles)
{
return articles.SelectMany(a => a.Tags).Distinct().ToList();
}
The entire LINQ "empire" (.NET 3.5) is built on top of IEnumerable which was around since .NET 2.0. Streams seem to be very artificial; why not rely on Iterable?Oh, and no "yield" in Java.
Re: Java 8: No more loops
#24[deleted]
It's the price to pay for implementing generics via type erasure, not for getting functional features in a statically typed language. http://stackoverflow.com/a/24421331
[1] http://stackoverflow.com/questions/3773985/why-is-an-int-in-...
edit: clarity
Re: Java 8: No more loops
#25I really want to like functional programming, but the functional version of each of these seems less readable and more verbose.
Re: Java 8: No more loops
#26Who cares about streams? Who cares about Optional? We just want to filter a list in a clear, terse manner. (Some people do care about streams and Optional, and I wish them well, but that's orthogonal to the question at hand.)
Consider the examples given. Here they are implemented in Gosu:
getFirstJavaArticle() : Article {
return articles.firstWhere(\ article -> article.Tags.contains("Java"))
}
getAllJavaArticles() : List {
return articles.where(\ article -> article.Tags.contains("Java"))
}
groupByAuthor() : Map> {
return articles.partition( \ article -> article.Author )
}
public getDistinctTags() : Set {
return articles.*Tags.toSet()
}
(I cheated a bit on the last one by just using a Set, but that's more appropriate and communicates the uniqueness of the elements in the collection to the API consumer.)Beyond the dot-star flatmap operator, there isn't anything very fancy going on: just closures being passed to methods, returning familiar classes that don't require additional transformation to pass on to the rest of the world.
It's too bad, because this is certainly good enough. As Jack Nicholson said: What if this... is as good as it gets?
Re: Java 8: No more loops
#27However, having that explicit "stream()" signifier is a very Java-y thing to do and appears to ask the programmer to decide how best to compile the given line. I would expect the compiler should be doing that work for us.
Re: Java 8: No more loops
#28Earlier quoted context omitted.
I disagree. The benefit of having the standard terms is they are the same in every FP library you use. If you spend the time to learn and internalize them within one, it'll be the same in every language or library that implements them. I don't understand how map, filter, collect, reduce are obtuse or arcane.
Those aren't the worst offenders. I'm referring to curry, cadr, lambda, monad, etc. But map is confusing: it's also a data structure. "Wait, does map() create a new key-value dictionary?" Filter and collect are okay. Reduce is pretty arcane but tolerable, but the actual operation feels intuitively closer to "categorize" or "group." My point about math was cultural. Math seems to revel in having its own peculiar and o…
Re: Java 8: No more loops
#29E.g. same Example in Dart:
class Article {
String title;
String author;
List tags;
Article(this.title, this.author, this.tags);
}
Article getFirstJavaArticle() =>
articles.firstWhere((x) => x.tags.contains("Java"));
List getAllJavaArticles() =>
articles.where((x) => x.tags.contains("Java"));
List getDistinctTags() =>
articles.expand((x) => x.tags).toSet().toList();
Can even be shorter without the Optional typing, but it's more readable to be explicit to have them. Dart benefits from having Collection and Stream mixins so you always get a rich API on Dart's collections.If anyone's interested to comparing FP collections in different languages, I've ported C# 101 LINQ examples in:
- Swift https://github.com/mythz/swift-linq-examples
- Clojure https://github.com/mythz/clojure-linq-examples
- Dart https://github.com/dartist/101LinqSamplesRe: Java 8: No more loops
#30 public final class Article{
public final String title;
public final String author;
public final List tags;
public Article(String title, String author, List tags) {
this.title = title;
this.author = author;
this.tags = tags;
}
}
Getters don't seem very useful on an immutable object.