News for hackers. Stuff that matters.
Re:Java 8: No more loops by BFay on Monday January 12, 2015 @10:56AM (#8874527)
I welcome the functional features, but I have to agree that these specific examples are ugly compared to the more familiar alternatives.

Maybe this will look more readable after getting used to it, but I'd much rather be looking at Clojure or Scala for now.

Re:Java 8: No more loops by bollockitis on Monday January 12, 2015 @12:18PM (#8875028)
Out of curiosity, could you share an example of how this compares to Clojure or Scala? I am not familiar with either.
Re:Java 8: No more loops by markc on Monday January 12, 2015 @03:05PM (#8876092)
Here's a quick version in Clojure. I'm sure it can be done cleaner than this..

  (def articles
    [{:title "title1" :author "author1" :tags #{:Java :t2 :t3}}
     {:title "title2" :author "author1" :tags #{:Jvxa :t2 :t3}}
     {:title "title3" :author "author3" :tags #{:Java :t3}}])
  
  ;; find the first article in the collection that has the tag “Java”.
  (first (filter #(contains? (:tags %) :Java) articles))
  ;; ==> {:tags #{:t2 :Java :t3}, :title "title1", :author "author1"}
  
  ;;get all the elements that match instead of just the first
  (filter #(contains? (:tags %) :Java) articles)
  ;; ==> ({:tags #{:t2 :Java :t3}, :title "title1", :author "author1"}
          {:tags #{:Java :t3}, :title "title3", :author "author3"})
  
  ;;group all the articles based on the author.
  (group-by :author articles)  ;; cheating?
  ;; ==> {"author1"
             [{:tags #{:t2 :Java :t3}, :title "title1", :author "author1"}
              {:tags #{:Jvxa :t2 :t3}, :title "title2", :author "author1"}],
          "author3"
             [{:tags #{:Java :t3}, :title "title3", :author "author3"}]}
  
  ;;find all the different tags used in the collections
  (apply clojure.set/union (map :tags articles))
  ;; ==> #{:Jvxa :t2 :Java :t3}
Re:Java 8: No more loops by codygman on Tuesday January 13, 2015 @01:50AM (#8878503)

    (group-by :author articles)
    groupBy ((==) `on` author) articles
What's wrong with cheating? :P
Re:Java 8: No more loops by codygman on Tuesday January 13, 2015 @01:20AM (#8878447)
I linked a Haskell version above as well: https://news.ycombinator.com/item?id=8878421
Re:Java 8: No more loops by codygman on Tuesday January 13, 2015 @12:00AM (#8878242)
formatted Scala code by hibikir from above in thread[0]:

    def topJavaArticle(articles:List[Article]) = articles.find(_.tags.contains("Java"))

    def javaArticles(articles:List[Article]) = articles.filter(_.tags.contains("Java"))

    def byAuthor(articles:List[Article) = articles.groupBy(_.author)
0: https://news.ycombinator.com/item?id=8874785