7 ms·
But what is even? I am sure functional programming has its natural appealing in areas like recursion, but this example doesn't seem too convincing for winning
by yeukhon 11y ago
But what is even?
I am sure functional programming has its natural appealing in areas like recursion, but this example doesn't seem too convincing for winning readability, I can always write this in python
filter(even, range(0, 101)))
where even is a function I wrote.
Please give an example for "Clojure allows me to express thoughts that I wouldn't know how to express in for, while, if, variables and objects." if you can.
- j-pb 11y agoWe were talking about java here. ^^ So python is offering a lot more already with it somewhat first class functions. What would you guess even? does, btw? But sure, let's make things a bit harder. As below, let's produce a sequence of the form [true false false true true true false false false false ...] of exact length 1000 without computing any more than exactly 1000 elements. (take 1000 (mapcat (fn [i] (repeat i (odd? i))) (range))) Of course the really interesting stuff is hard to do in one liners. But I think one of the more mind bending things is probably core.async. Go style coroutines and channels implemented as a library on top of the language. Which allows for concurrent programming inside clojurescript which is compiled to JS and thus inherently single-threaded. http://swannodette.github.io/2013/08/02/100000-processes/ http://swannodette.github.io/2013/08/02/100000-processes/
- yeukhon 11y agoMy point was "even" and "filter" can be built by someone, just to be fair about the whole readability. If "even" is already built into the language, then that's a given. Readability and language understanding are not separable. I for one have a hard time actually understand what the code does above (although I can guess it). But I am not sure how ^ translate into [true false false true true true false false false false ...] sequence because, I thought we were calculating oddity of 1000 numbers. [1 2 3 4 5 6 7 8 9 10 ...] ==> [T F T T F T F T T T ] Perhaps the lack of understanding Clojure syntax to comprehend what the code is doing.
- j-pb 11y agoIt's part of the std lib. But yeah you're basically on the same page as me. Java doesn't have the ability to be extended in the ways that clojure or python can. Python also allows for functional programming to a certain degree, but clojure is a lot more powerful when it comes to language extensions. Threads as a library (for js environments) for clojure. https://github.com/clojure/core.async https://github.com/clojure/core.async Logic programming for clojure. https://github.com/clojure/core.logic https://github.com/clojure/core.logic Declarative UI building, with dataflow semantics. http://reagent-project.github.io http://reagent-project.github.io Additionally immutability makes your life a lot easier. Parallel computing in clojure is a no brainer, you just change your map to pmap, and that's it.
- yeukhon 11y agoThank you. I will look at the immutability part. I remember taking Racket in school a few years ago and that was a major thing about FPL like Racket / Lisp. I have always wanted to look at building something again in one of the popular FPLs.
- huahaiy 11y agoExactly the point. In many other languages, every programmer can write their own "even" and "filter" with their own idiosyncrasy. However, in Clojure, every one uses the same set of high level functions in the standard library, because they are sufficiently rich to express most of the programming concepts, without having to roll your own. In Clojure, readability is obtained through standardization. Every Clojure programmer instantly knows what the functions in the standard library do. Because there's not much syntax in a Lisp, code is very easy to read once one knows the functions.
- dragonwriter 11y ago> In many other languages, every programmer can write their own "even" and "filter" with their own idiosyncrasy. "filter" is incredibly useful, and most modern languages (including Java 8) have it built in. "even?" is sometimes useful, and languages are kind of hit-and-miss about it, but its usually small enough to inline, and the biggest cost of not having it built-in is probably things like people using modulus rather than first-bit testing in Java which, IIRC, produces somewhat slower code for that check.
- dragonwriter 11y ago> As below, let's produce a sequence of the form [true false false true true true false false false false ...] of exact length 1000 without computing any more than exactly 1000 elements. So, again, python: [i % 2 == 0 for i in range(1000)] (If you insist on Java, its fairly straightforward, though not a one-liner AFAIK, using Java 8 generators.)
- j-pb 11y agoYou misread the question. I didn't mean (map even? (range 1000)), but a sequence of length 1000 where the elements are 1 * true, 2 * false, 3 * true, 4 * false and so on. To compare it with what your code produces. [true false false true true true false false false false ...] [true false true false true false true false true false ...] Edited for more clarity.
- deleted 11y ago[deleted]
- dragonwriter 11y agoRight, sorry, in python that's something like: from itertools import chain, repeat chain.from_iterable([repeat(i % 2 != 0, i) for i in range(1,1000)]) In Java 8, I think the solution with streams would be fairly straightforward, something like: IntStream.range(1,1001).flatMap(i -> Stream<Boolean>.generate( () -> (x & 1) != 0 ).limit(i))
- sokoloff 11y agoDoesn't that python code give you a list of 500,500 elements, rather than a list of 1000 elements as originally specified?
- dragonwriter 11y agoIn both cases yes, and in both cases because I made the same stupid mistake when both languages have good means to do infinite streams and "take" equivalents. (And that's worse in the Java case, since I used those in the inner function but not the outer one.) So, I think I need to take a break from comment-box programming for a while; its clearly not working out for me today.
- SkidanovAlex 11y ago> (take 1000 (mapcat (fn [i] (repeat i (odd? i))) (range))) And this example is suppoed to support Clojure's readability?
- j-pb 11y agoYou do it in a different language that isn't haskel or ml and show me the result, then we can judge. You'd btw probably write it as. (->> (range) (map #(repeat % (odd? %))) (apply concat) (take 1000) If you want it more readable.
- xtian 11y agoI'd say the argument for FP in general is not that it allows for the expression of thoughts that are impossible to express with for, while, if, etc., but rather that functional programs are still able to be reasoned about at the point of complexity where imperative programs become completely intractable. Not an FP expert so I don't know if this is entirely accurate or how it plays out in reality.
- huahaiy 11y agoeven? is a predicate that test if the parameter is an even number, as the name suggests. ? is part of the function name, not a syntax symbol (there's not much syntax in a Lisp, after all). By convention, function name ends with ? are predicates (or Boolean functions) in Clojure. Any competent Clojure programmer would likely have known most of the Clojure core functions by heart: map, reduce, filter, etc. There are maybe about a hundred of them? Unlike imperative programming languages, where you use three primitives (assignment, condition and loop) to construct everything, in Clojure, you use these core functions to construct your program. In a sense, the process of learning Clojure is the process of learning the use of core functions. These functions are simply at a higher level of abstraction than the three primitives, hence some people say that Clojure is more expressive, because it's closer to human thoughts than Von Neumann machine specificity.
- sanderjd 11y ago> Any competent Clojure programmer would likely have known most of the Clojure core functions by heart I really like Clojure, but I've always found the relatively huge set of core functions to be a major barrier to entry. I always have this nagging feeling of "there's probably a core function I'm missing to make doing this or that easier", and end up spending a lot of time poring over documentation instead of just programming. Maybe that's just how it is, and the answer is to push through it until you know the functions better, but there's something to be said for shipping a language with fewer, simpler, more primitive operations.
- huahaiy 11y agoFrom a cognitive point of view, learning a large number of idioms is more manageable for human beings than having to reinvent the mental constructs from primitives every time. This human capacity of dealing with idioms is what called "chunking" in psychology (https://en.wikipedia.org/wiki/Chunking_%28psychology%29 https://en.wikipedia.org/wiki/Chunking_%28psychology%29) Human has unlimited memory capacity and fast memory access, whereas human logical processing power is fairly limited and error prone. That's why any real human language has a large vocabulary.
- 11y ago
- ane 11y agoYour Python example is functional programming! You're doing exactly the same thing as in Clojure, just not in a Lisp syntax. Therefore, in your question, you could substitute Clojure with functional programming, and the examples in which FP allows you to express thoughts you wouldn't know how to express in for, while and so on -- are plentiful. I may not be answering your question, but there's a reason for that: the functional programming aspect isn't Clojure's main selling point. It's the other ideas. Software transactional memory, immutability, its emphasis on state and identity, the protocol system, transients, concurrency, transducers, the list goes on.
- yeukhon 11y agoYou are referring to higher-order function I assume. I don't think that is a property of FPL. I am definitely not dismissing FPL, I am just arguging about the actual examples given were not strong enough to dismiss Java being hard to read.
- NhanH 11y agoArguably, first class higher order function (and immutability) is defining characteristics of functional programming. Although, I don't think the functional/ imperative is necessary a good axis for categorizing languages. There are potentially more differences between Clojure/Ocaml than Python/Clojure, for example.
- yogthos 11y agoThe big difference is that Clojure defaults to immutable data structures. While you can chain functions together in Java and Python, you still have to manually keep track of all the references. The main benefit of modern functional languages is in their use of revisioned data structures. This has same benefits as garbage collection. Without GC you have to manually track where you allocate and deallocate things, and this is highly error prone. With GC the runtime does this for you, and you can focus on other things. Same thing applies with mutable and immutable data. With mutable data structures all the changes are done in place, and you have to keep track of everywhere it's used to update it predictably. With immutable data structures you're "copying" the data any time you make a change, and the runtime keeps track of the references for you.
- jrumbut 11y agoEven as someone who prefers a functional style, that quote is pretty unfortunate. Any Turing complete language is going to be able to express the same ideas (with varying degrees of gracefulness, resource usage, ease of debugging, etc).
- cobaltblue 11y agoGreat Clojure feature: user=> (source even?) (defn even? "Returns true if n is even, throws an exception if n is not an integer" {:added "1.0" :static true} [n] (if (integer? n) (zero? (bit-and (clojure.lang.RT/uncheckedLongCast n) 1)) (throw (IllegalArgumentException. (str "Argument must be an integer: " n)))))