4 ms·
This was exactly what I was thinking when I read the code. The goal should not be to remove loops. As a language construct there is nothing wrong with loops its
by remon 12y ago
This was exactly what I was thinking when I read the code. The goal should not be to remove loops. As a language construct there is nothing wrong with loops itself. The goal should be to make more readable and maintainable code, preferably without increasing verbosity. The functional alternatives posted here are not actually code quality improvements and should not be presented as such.
- BFay 12y agoI 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.
- bollockitis 12y agoOut of curiosity, could you share an example of how this compares to Clojure or Scala? I am not familiar with either.
- markc 12y agoHere'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}
- codygman 12y ago(group-by :author articles) groupBy ((==) `on` author) articles What's wrong with cheating? :P
- codygman 12y agoformatted 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 https://news.ycombinator.com/item?id=8874785
- codygman 12y agoI linked a Haskell version above as well: https://news.ycombinator.com/item?id=8878421 https://news.ycombinator.com/item?id=8878421
- a-saleh 12y agoWell, it depends. I think the primary improvement is that loop is no longer a language construct. It is just another function. As a prime example I would take Clojure. On first look: "No loops?" "This reduce functions everyhere look ugly." On second look: "Oh, I can import paralel reduce instead of the single-threaded one?" [1] "Somebody created a library to transparently switch between local reduce and one using hadoop?" [2] [1] http://clojure.org/reducers http://clojure.org/reducers [2] https://github.com/aphyr/tesser https://github.com/aphyr/tesser
- remon 12y agoI understand what you're saying and you make good points but I still do not think removing a loop as a language feature is necessarily an interesting goal. Generally speaking this discussion tends to revolve around reducing verbosity or doing more with less (so, generalizing multiple language features into a single one). I'm just not sure if that's the most interesting discussion to have. The goal and primary focus should be clean, readable, maintainable code. If I buy a more maintainable and more easy to share codebase with a few extra lines of code or a more verbose expression of a loop that's a trade I'd always make (note; I'm not claiming that's necessarily a trade you need to make for every case in every language).