Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Java's one of the worst language examples of using FP collections I've seen. Even with hindsight I still find this to be uglier and unnecessarily more verbose than it needs to be.

E.g. same Example in Dart:

    class Article {
      String title;
      String author;
      List<String> tags;
      Article(this.title, this.author, this.tags);  
    }

    Article getFirstJavaArticle() =>
        articles.firstWhere((x) => x.tags.contains("Java"));

    List<Article> getAllJavaArticles() =>
        articles.where((x) => x.tags.contains("Java"));

    List<String> 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/101LinqSamples


Dart looks very elegant. You didn't include "group all the articles based on the author". Would that be equally clean in Dart?

Here's the Clojure version:

  ;; given articles = [{:title "t1" :author "a1" :tags #{:t1 :t2}} .. etc. ]
  ;; These 4 Clojure one-liners replace all the J8 code examples in the article.
  (first (filter #(contains? (:tags %) :Java) articles))
  (filter #(contains? (:tags %) :Java) articles)
  (group-by :author articles)
  (apply clojure.set/union (map :tags articles))


And your example doesn’t even tell what it’s doing.

I’d have to look up what .expand, .where etc means, while Java just uses the standard FP names that every CompSci student knows.


Where makes sense if you think of it from a SQL perspective which I think has more widespread use than the corresponding FP terms (ie lots of developers don't have a CompSci background, but few of them haven't used trivial SQL).


> uglier and unnecessarily more verbose than it needs to be

Nice autologism.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: