News for hackers. Stuff that matters.
Re:The death of if/else cascades, a lightweight alternative by iamaleksey on Sunday December 06, 2009 @12:30PM (#979842)
This is much better handled in languages with pattern matching. And you can match on arbitrary fields.

Erlang:

  surf(Channels) ->
    lists:foreach(fun process/1, Channels).
  
  process(#channel{genre = "football"} = Channel) ->
    record(Channel);
  
  process(#channel{genre = "comedy", repeat = false} = Channel) ->
    record(Channel);
  
  process(#channel{genre = "crime", show_title = "Cops"}) ->
    skip;
  
  process(#channel{genre = "crime"} = Channel) ->
    record(Channel);
  
  process(_) ->
    skip.
Full gist: http://gist.github.com/250304
Re:The death of if/else cascades, a lightweight alternative by daleharvey on Sunday December 06, 2009 @03:18PM (#980094)
the problem with pattern matching is that its hard to dynamically change the dispatch table, in javascript I can just do

  dispatch_table[newthing].handler = function() .....
Re:The death of if/else cascades, a lightweight alternative by silentbicycle on Sunday December 06, 2009 @03:33PM (#980115)
You can change the clause database in Prolog by asserta/assertz/retract, but that runs headfirst into the typical trade-offs involving mutable state, as does your Javascript example. Erlang also lets you change patterns by hot-loading code, but its design goes to great pains to do so with reasonable safety.

Also, pattern matching doesn't require all of the variables to be bound. A pattern may specify some values but just partially specify (a list with at least one remaining value) or constrain (any positive integer) others. This makes it vastly more flexible than just dispatching based on one value.

Re:The death of if/else cascades, a lightweight alternative by daleharvey on Sunday December 06, 2009 @05:07PM (#980232)
certainly, I am an erlang programmer and do love pattern matching, but as much as I pretend to hate javascript, I do love that modules/namespaces whatever are a first class citizen in a way they certainly arent with erlang, (they are at the vm level, but not so much the language, smerl is quite handy for that).

I should probably play with match specs more often

Re:The death of if/else cascades, a lightweight alternative by DougBTX on Sunday December 06, 2009 @06:08PM (#980318)
Which, if we were to take this example seriously, would be very important, since these rules would likely be user specified.
Re:The death of if/else cascades, a lightweight alternative by xtho on Monday December 07, 2009 @12:17AM (#980763)
How should a function be "user" specified? (But which user?) The set of eligible options is well defined at the time of writing the program.
Re:The death of if/else cascades, a lightweight alternative by wlievens on Tuesday December 08, 2009 @06:43AM (#983508)
But the way they are composed, and where and when they apply, is most likely not!
Re:The death of if/else cascades, a lightweight alternative by agbell on Sunday December 06, 2009 @03:07PM (#980074)
A pattern matching solution is much more readable and it forces you to build small functions if you are pattern matching in the function declaration.
Re:The death of if/else cascades, a lightweight alternative by silentbicycle on Sunday December 06, 2009 @03:18PM (#980092)
Right, AND being able to match on multiple fields and destructure them means that several awkward "which class owns the method" problems never arise in the first place.

(Multimethods are similar in many ways, though I find pattern matching far more useful.)