Functional programming in mainstream languages - TechHui2024-03-29T04:54:27Zhttp://www.techhui.com/forum/topics/functional-programming-in?groupUrl=functionalprogramming&commentId=1702911%3AComment%3A49320&groupId=1702911%3AGroup%3A48625&feed=yes&xn_auth=noI do this also. When writing…tag:www.techhui.com,2009-09-08:1702911:Comment:493492009-09-08T19:23:04.503ZTim Dysingerhttp://www.techhui.com/profile/TimDysinger
I do this also. When writing imperative code I see if I can:<br />
<br />
1) try not to use bindings (variables) if I don't have to do so<br />
2) if I can't do #1 then I use single-assignment bindings, emulating erlang<br />
<br />
Often times, this results in tighter code w/ less side-effects.<br />
<br />
(It's simplistic and doesn't address mutable structures like Hash and Array. I know)<br />
<br />
Bottom line for me is "Just don't go there" :) I would rather just write real FP code.
I do this also. When writing imperative code I see if I can:<br />
<br />
1) try not to use bindings (variables) if I don't have to do so<br />
2) if I can't do #1 then I use single-assignment bindings, emulating erlang<br />
<br />
Often times, this results in tighter code w/ less side-effects.<br />
<br />
(It's simplistic and doesn't address mutable structures like Hash and Array. I know)<br />
<br />
Bottom line for me is "Just don't go there" :) I would rather just write real FP code. Nothing too complicated -- ju…tag:www.techhui.com,2009-09-08:1702911:Comment:493202009-09-08T01:28:10.451ZNate Sandershttp://www.techhui.com/profile/NateSanders
Nothing too complicated -- just that I tend to write methods/functions that are purely functional. They neither depend on external state, nor modify any external state. They can do their job solely through parameter input and value return. The fact that they're self-contained means that you can look simply at the text in front of you and figure out what's going on without looking other places to see how values are modified, and it also enables easy re-use since it's decoupled from other…
Nothing too complicated -- just that I tend to write methods/functions that are purely functional. They neither depend on external state, nor modify any external state. They can do their job solely through parameter input and value return. The fact that they're self-contained means that you can look simply at the text in front of you and figure out what's going on without looking other places to see how values are modified, and it also enables easy re-use since it's decoupled from other state.<br />
<br />
Obviously, it's tough to completely get away from stateful programming, but you can segregate things somewhat. I used to do this to some degree in C and C++ and Rich Hickey talks about "writing Java classes with 56 purely functional static methods" (paraphrased, but close), which I'm sure some of his co-workers looked strangely at. :)<br />
<br />
This style obviously ends up with you either making a lot of copies or going immutable like you mention and passing around refs/pointers without changing the targets.<br />
<br />
-- Nate<br />
<br />
<cite>Anders Conbere said:</cite><blockquote cite="http://www.techhui.com/forum/topics/functional-programming-in?groupUrl=functionalprogramming&page=1&commentId=1702911%3AComment%3A49311&x=1#1702911Comment49311"><div>Can you give some examples of what it means to write programs in a pure functional style in procedural or OO languages? Do you just mean that you focus on creating immutable data structures? And stateless objects?</div>
</blockquote> Can you give some examples of…tag:www.techhui.com,2009-09-07:1702911:Comment:493112009-09-07T17:17:45.500ZAnders Conberehttp://www.techhui.com/profile/AndersConbere
Can you give some examples of what it means to write programs in a pure functional style in procedural or OO languages? Do you just mean that you focus on creating immutable data structures? And stateless objects?
Can you give some examples of what it means to write programs in a pure functional style in procedural or OO languages? Do you just mean that you focus on creating immutable data structures? And stateless objects?