{"id":54,"date":"2008-03-10T00:27:43","date_gmt":"2008-03-10T04:27:43","guid":{"rendered":"http:\/\/steve.vinoski.net\/blog\/2008\/03\/10\/damien-katz-criticizes-erlang\/"},"modified":"2008-03-10T01:42:32","modified_gmt":"2008-03-10T05:42:32","slug":"damien-katz-criticizes-erlang","status":"publish","type":"post","link":"https:\/\/steve.vinoski.net\/blog\/2008\/03\/10\/damien-katz-criticizes-erlang\/","title":{"rendered":"Damien Katz Criticizes Erlang"},"content":{"rendered":"<p><a href=\"http:\/\/damienkatz.net\/\">Damien Katz<\/a> has been using <a href=\"http:\/\/www.erlang.org\/\">Erlang<\/a> for <a href=\"http:\/\/couchdb.org\/\">CouchDB<\/a>, and has posted some <a href=\"http:\/\/damienkatz.net\/2008\/03\/what_sucks_abou.html\">criticisms of the language and runtime<\/a> based on his experiences with them. Erlang, like any language, has its issues, and Damien&#8217;s criticisms are mostly reasonable and are worth reading. Well, except for this one:<\/p>\n<blockquote><p><em>Immutable variables in Erlang are hard to deal with when you have code that tends to change a lot, like user application code, where you are often performing a bunch of arbitrary steps that need to be changed as needs evolve.<\/p>\n<p>In C, lets say you have some code:<\/p>\n<p>int f(int x) {<br \/>\n  x = foo(x);<br \/>\n  x = bar(x);<br \/>\n  return baz(x);<br \/>\n}<\/p>\n<p>And you want to add a new step in the function:<\/p>\n<p>int f(int x) {<br \/>\n  x = foo(x);<br \/>\n  x = fab(x);<br \/>\n  x = bar(x);<br \/>\n  return baz(x);<br \/>\n}<\/p>\n<p>Only one line needs editing,<\/p>\n<p>Consider the Erlang equivalent:<\/p>\n<p>f(X) -><br \/>\n  X1 = foo(X),<br \/>\n  X2 = bar(X1),<br \/>\n  baz(X2).<\/p>\n<p>Now you want to add a new step, which requires editing every variable thereafter:<br \/>\nf(X) -><br \/>\n  X1 = foo(X),<br \/>\n  X2 = fab(X1),<br \/>\n  X3 = bar(X2),<br \/>\n  baz(X3).<\/em><\/p><\/blockquote>\n<p>If you had to write that code the way Damien did, then yes, that would suck. But here&#8217;s a better way to write it:<\/p>\n<pre>f(X) -&gt;\r\n    lists:foldl(fun(F, Last) -&gt; F(Last) end,\r\n        X, [fun foo\/1, fun fab\/1, fun bar\/1, fun baz\/1]).<\/pre>\n<p>This code applies the anonymous function passed as the first argument to <code>lists:foldl<\/code> for each element of the third argument &mdash; the list of <code>fun<\/code>s, Erlang-speak for functions &mdash; starting with the value <code>X<\/code> as the value passed as <code>Last<\/code> and using the return value of <code>F(Last)<\/code> as the next value of <code>Last<\/code>. The overall return value of <code>lists:foldl<\/code> is the final value of <code>Last<\/code>. With this approach, modifying the functions applied to <code>X<\/code> also requires editing only a single line, specifically the list of <code>fun<\/code>s.<\/p>\n<p>In general, if you&#8217;re used to imperative languages and you find yourself writing step-by-step Erlang code like in the original example above, think functions and lists instead.<\/p>\n<p>P.S. If you&#8217;ve never seen it, read Damien&#8217;s <em><a href=\"http:\/\/damienkatz.net\/2006\/05\/signs_youre_a_c.html\">Signs You&#8217;re a Crappy Programmer (and don&#8217;t know it)<\/a><\/em>. One of my favorite blog postings, and I especially like the first two items in his list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Damien Katz has been using Erlang for CouchDB, and has posted some criticisms of the language and runtime based on his experiences with them. Erlang, like any language, has its issues, and Damien&#8217;s criticisms are mostly reasonable and are worth reading. Well, except for this one: Immutable variables in Erlang are hard to deal with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[58,4,80],"tags":[169,140,174],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-code","category-erlang","category-functional","tag-code","tag-erlang","tag-functional-programming"],"_links":{"self":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":0,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"wp:attachment":[{"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/steve.vinoski.net\/blog\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}