Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Now I'm no Erlang master, but why not do:

  X1 = baz(bar(foo(X))).
Or, if you want to be fancy:

  X1 = lists:foldl(fun (F, A) -> F(A) end, X, [fun foo/1, fun bar/1, fun baz/1]).
Obviously the fun thing about the second way is you could have code make up any arbitrary list of one argument functions to apply in succession. If you know what functions you are going to use ahead of time, then the first way is much clearer.


I have no idea. That's what I would've done.

The only thing I can think of is that the article's author doesn't want to have to move between the parentheses. His initial complaint was that adding a function 'fab' in between foo and bar means you have to rename everything. If you chain the functions in one long expression, you need to navigate to between bar and foo, type 'fab(', then add a separate parentheses onto the end.

It also gets really ugly when foo, bar, and baz are instead line-long expressions, which they often are:

    X1 = big_long_expression_containing_some_stuff_and_baz(
              big_long_expression_containing_some_stuffand_bar(
                     big_long_expression_containing_some_stuff_and_foo(X))).
While if you have a sane linebreaking policy (as both Haskell and Eve do), you can do:

   X = big_long_expression_containing_some_stuff_and_baz
     . big_long_expression_containing_some_stuff_and_bar
     . big_long_expression_containing_some_stuff_and_foo
     $ x


> It also gets really ugly when foo, bar, and baz are instead line-long expressions, which they often are

I sometimes use variables to "take a breather". You do some heavy, syntax and/or meaning laden work, and put the result of all of it in 'current_velocity' or whatever, and that conceptually helps you to move on to the next thing with the result of what you've just done firmly in mind. Single assignment doesn't prevent that, obviously, but sometimes functional code uses them less and just uses a bunch of nested functions. Those Haskell examples you posted seem like a nice idea, by the way.

http://journal.dedasys.com/articles/2007/01/17/clockwork-lan...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: