TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Clojure for the Brave and True – Functional Programming

162 pointsby nonrecursiveover 11 years ago

10 comments

taericover 11 years ago
Has there been any exploration into an idea where the problem with side effects is not that they make functions impure, but they are often against the metaphor of the instructions being given? That is, if the metaphor that one is trying to model is traditional mathematics, then of course side effects are terrible.<p>However, consider a program where the main metaphor is controlling something. Logo, for example. Few people argue, I would think, that the traditional imperative styling there hurts and confuses things.<p>More extreme, consider stack based languages. These are strictly based on the current state of the program, yet my understanding is if you can fit your mind to that metaphor, it works very very well.<p>Or, my favorite category, cookbooks. Look at traditional baking directions: &quot;Begin heating oven to XXX, mix dry ingredients in bowl, add butter, whip, ...&quot; Doesn&#x27;t get any more imperative than that, and yet people around the world often have great success replicating the desired results. (Granted, I often think that the difference in programming and teaching is that humans make an effort to understand what you were communicating, computers typically don&#x27;t.)<p>Does this make sense?
评论 #6368401 未加载
评论 #6367055 未加载
评论 #6368770 未加载
评论 #6366850 未加载
评论 #6367833 未加载
评论 #6366938 未加载
评论 #6367123 未加载
评论 #6369388 未加载
评论 #6368571 未加载
评论 #6369639 未加载
评论 #6367007 未加载
评论 #6370074 未加载
brudgersover 11 years ago
Rich Hickey has a great description of how to think about working with immutable values - what we usually want when we change a variable is simply the next value and so long as we are getting the value we expected, there&#x27;s no need to name it.<p>In other words if our current position in an array is 2, what we need to access the next position is the value 3. Creating a variable <i>int i=2;</i> and then mutating it <i>i++;</i> introduces the possibility of side effects. This not to say that at an abstraction layer below our programming language a register won&#x27;t get incremented, only that our brains don&#x27;t need to worry about the mechanisms most of the time if we use a functional language.
评论 #6367029 未加载
jafakuover 11 years ago
As someone who still doesn&#x27;t get functional programming, I would like to see a real-world example of how to deal with side effects. Eg: write something into a file or a DB. Because <i>of course</i> methods&#x2F;functions with no side effects are easier to deal with, it sounds good, but I don&#x27;t think it would be easy nor convenient to separate every side effect in a real-world program (as opposed to academic programs, where you just write a Fibonacci or whatever). Even in good OOP, you can have lots of side effects in a method.<p>Halp!
评论 #6366825 未加载
评论 #6367978 未加载
评论 #6366992 未加载
评论 #6366828 未加载
评论 #6369409 未加载
评论 #6366816 未加载
评论 #6367761 未加载
评论 #6366708 未加载
评论 #6368258 未加载
评论 #6367341 未加载
geuisover 11 years ago
I want to take a leap and talk about 2 core concepts that I got after spending last week learning Clojure. I&#x27;d love some comments about them.<p>I&#x27;ve been writing javascript for years but functional programming didn&#x27;t make sense until I realized:<p>1) It&#x27;s essentially inverted callbacks. You can read and reason about your code by working from the inside out. The results of a function are passed up to the outside.<p>2) When you stop modifying variables and just pass them around, it becomes a lot easier to think about code in abstract ways. There are of course some situations where modifying a variable is needed, but it really feels icky and you think twice about doing it.<p>The problem I found trying to learn this stuff is that once you learn to think functional its hard to think like someone who doesn&#x27;t get it. That makes it hard to translate concepts in ways that non-functional people can get easier. I&#x27;m keeping mental tabs on ideas as I understand them to hopefully help with this problem.<p>Also, macros. Clojure people love talking about them. They aren&#x27;t explained well most of the time though.<p>Simplest explanation is that you can write a bunch of your own functions, even ones that will override native functions, i.e macros. Since clojure is a compiled language, at compile time all the source code gets read in and pre-processed. When the compiler reaches some code that was defined in a macro, it keeps rewriting that code until all the macros are processed and the code has been re-written. Then everything is compiled to bytecode for the jvm.
评论 #6368576 未加载
评论 #6368577 未加载
alexvayover 11 years ago
I found LightTable* as a great way to grok functional programming - you can actually see how data moves around. Really brilliant.<p>*Lighttable IDE: <a href="http://www.chris-granger.com/lighttable/" rel="nofollow">http:&#x2F;&#x2F;www.chris-granger.com&#x2F;lighttable&#x2F;</a>
kineticfocusover 11 years ago
Just watched this decent intro released yesterday... (OSCON 2013: &quot;Functional Thinking&quot; - Neal Ford) <a href="http://www.youtube.com/watch?v=7aYS9PcAITQ" rel="nofollow">http:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=7aYS9PcAITQ</a>
username223over 11 years ago
&gt; It always returns the same result given the same arguments. This is call &quot;referential transparency&quot; and you can add it to your list of five-dollar programming terms.<p>&gt; It doesn&#x27;t cause any side effects, e.g. it doesn&#x27;t &quot;change the external world&quot; by changing external mutable objects or outputting to i&#x2F;o.<p>These have little to do with one another. &quot;sort :: [a] -&gt; [a]&quot; sorts a list, perhaps using quicksort. If you want to randomize the pivots, it&#x27;s &quot;sort :: [a] -&gt; IO [a]&quot;, and everything in your program that sorts things has a different type, even though it produces the same value.
gbogover 11 years ago
I have been tempted by functional programming, and it is interesting, but I&#x27;m not sure how would be the result for highly complex code.
评论 #6367811 未加载
pat_shaughnessyover 11 years ago
Another great post, Daniel... keep &#x27;em coming!
评论 #6366707 未加载
SeoxySover 11 years ago
The third example has an error: ((rand) &gt; 0.5) should say (&gt; (rand) 0.5)
评论 #6366869 未加载