Live data from Hacker News

Learn from Haskell - Functional, Reusable JavaScript

seanhess.github.com

21–30 of 50 posts

Re: Learn from Haskell - Functional, Reusable JavaScript

#21
post #14

Earlier quoted context omitted.

The biggest idea is probably laziness. In Haskell, everything is basically a function. Even the number 3 is really just a function that if and when evaluated will return the value 3. I used that concept to to simply some JavaScript UI code a while back. I had SELECT element that I needed to populate dynamically but wanted the last option to invoke an action that allowed the user to add another option choice 1 choice…

'In Haskell, everything is a function' is absolutely wrong. People who don't know much about Haskell often say this. There's a post by Conal Elliott about this incorrect meme: http://conal.net/blog/posts/everything-is-a-function-in-hask... It's simply not true. In fact, because Haskell is statically typed, it's very easy to tell what's a function and what's not: If its type doesn't have an -> in it, it's not a functi…

... I know that 3 is not literally function in the Haskell type system but it's an easy way to explain it to people less familiar with the language (not that I assume freyrs3 is one of those).

Also, while it might be true that "people who don't know much about Haskell" often say that everything is a function, it's also the case that people who do know a lot about the language find the notion to be pedagogically useful: On page 13 of The Haskell School of Expression, Paul Hudak offers that pi (which is of type Floating a => a and thus clearly not a function) "can be thought of as a function with no arguments".

Re: Learn from Haskell - Functional, Reusable JavaScript

#22
post #11

One might as well say "Learn from Lisp" or "Learn from ML" as there really isn't anything Haskell specific about these kind of higher order functions. If we want to learn from Haskell, the big ideas are functional purity and its type system.

The biggest idea is probably laziness. In Haskell, everything is basically a function. Even the number 3 is really just a function that if and when evaluated will return the value 3. I used that concept to to simply some JavaScript UI code a while back. I had SELECT element that I needed to populate dynamically but wanted the last option to invoke an action that allowed the user to add another option choice 1 choice…

I think by function you really mean "thunk", which is just the name for an unevaluated expression. A thunk is really just an implementation detail while a function has important semantic meaning (it is a mapping from values of one type to values of another).

A thunk can be thought of as a procedure with no arguments (taking some terminology from SICP), and in JavaScript procedures are called "functions", but using JavaScript's terminology in a Haskell context gets confusing.

Additionally, it wouldn't even be correct to say all Haskell values are thunks. You actually have a fair bit of control over this and can have strict, unboxed elements and the like with GHC. A different compiler could implement the non-strict semantic in a different way.

Re: Learn from Haskell - Functional, Reusable JavaScript

#24
post #22

Earlier quoted context omitted.

The biggest idea is probably laziness. In Haskell, everything is basically a function. Even the number 3 is really just a function that if and when evaluated will return the value 3. I used that concept to to simply some JavaScript UI code a while back. I had SELECT element that I needed to populate dynamically but wanted the last option to invoke an action that allowed the user to add another option choice 1 choice…

I think by function you really mean "thunk", which is just the name for an unevaluated expression. A thunk is really just an implementation detail while a function has important semantic meaning (it is a mapping from values of one type to values of another). A thunk can be thought of as a procedure with no arguments (taking some terminology from SICP), and in JavaScript procedures are called "functions", but using Ja…

I meant function in the same sense that JavaScript means function which is not the same as what Haskell considers to be a function (since JS functions don't have to return a value). I should have been more precise.

Re: Learn from Haskell - Functional, Reusable JavaScript

#25
post #10
post #4

One bit of trouble I have with functional style programming is something like the following example: if I have an array of people with firstName and lastName properties, how would I go about returning the person with the longest full name without adding any properties directly to the objects? It is a simple map then reduce to return the maximum length, a fairly trivial modification thereof to return the full name, bu…

You don't need a map, just a reduce (foldl, really). Using underscore, no double calculation: _.reduce(arr, function(p1, p2) { var len = p2.firstName.length + p2.lastName.length; return (len > p1[1]) ? [p2, len] : p1; }, [null, -1]);

Also using underscore, with sortBy:

    _.chain(people)
    .sortBy(function(person) { 
      return person.firstName.length + person.lastName.length; })
    .last()
    .value()

Re: Learn from Haskell - Functional, Reusable JavaScript

#26
post #23
post #18

I like this style of programming, but isn't there cause for concern in having such a deep call stack in JS?

Yes. V8 and other JS compilers optimize tail calls, but it's not guaranteed by the standard. (i think IE doesn't, for example)

I'm not sure where you got this idea. V8 does not optimize tail calls. It's easy to see this in the Chrome console, I just entered this now:

  var a = function(x) { if (x == 0) return 'done'; return a(x-1); };
  a(100000);
  RangeError: Maximum call stack size exceeded
http://code.google.com/p/v8/issues/detail?id=457

Re: Learn from Haskell - Functional, Reusable JavaScript

#27
post #25
post #10

Earlier quoted context omitted.

You don't need a map, just a reduce (foldl, really). Using underscore, no double calculation: _.reduce(arr, function(p1, p2) { var len = p2.firstName.length + p2.lastName.length; return (len > p1[1]) ? [p2, len] : p1; }, [null, -1]);

Also using underscore, with sortBy: _.chain(people) .sortBy(function(person) { return person.firstName.length + person.lastName.length; }) .last() .value()

FYI, this isn't as general, but I think it shows off underscore quite nicely:

  _(people).max(function(person) {
    return person.firstName.length + person.lastName.length;
  });

Re: Learn from Haskell - Functional, Reusable JavaScript

#28
I'm been experimenting with UHC's js backend. One of the things that struck me was that in Haskell if I have a function like:

    forever :: Monad m => m a -> m a
I can use it for either asynchronous or synchronous javascript, e.g

    forever :: IO a -> IO a
    forever :: ContT r IO a -> ContT r IO a
The same function can take a synchronous js function or an async js function and run it in an infinite loop. For this reason, I think that if you want to learn from Haskell in writing reusable js then you should look into monads.

Re: Learn from Haskell - Functional, Reusable JavaScript

#30
post #4

One bit of trouble I have with functional style programming is something like the following example: if I have an array of people with firstName and lastName properties, how would I go about returning the person with the longest full name without adding any properties directly to the objects? It is a simple map then reduce to return the maximum length, a fairly trivial modification thereof to return the full name, bu…

You wouldn't use map or reduce, you'd just use recursion with an accumulator. If you absolutely insist on map/reduce, you can just use reduce, where your binary function returns whichever object has a longer full name.

Well, reduce is a recursion with an accumulator. Did you had something special in mind?
Post reply on HN