Live data from Hacker News

Kal – a clean JavaScript alternative without callbacks

rzimmerman.github.io

41–50 of 130 posts

Re: Kal – a clean JavaScript alternative without callbacks

#41
It's remarkable how similar this code looks to the monadic way of doing it.

For this Kal code...

    task getUserFriends(userName)
      wait for user from db.users.findOne {name:userName}
      wait for friends from db.friends.find {userId:user.id}
      if user.type is 'power user'
        for parallel friend in friends
          wait for friendsOfFriend from db.friends.find friend
          for newFriend in friendsOfFriend
            friends.push newFriend unless newFriend in friends
      return friends
Here's a pseudo-ish implementation in pseudo-ish Haskell.

  getUserFriends userName = do
    user 
I like the concept of Kal, and I look forward to seeing what people do with it. Regarding syntax, I have to admit that I share the opinions of a few others in terms of preferring symbols over so many keywords, but that's a minor nitpick. Great job!

Re: Kal – a clean JavaScript alternative without callbacks

#43
post #19

Excellent work. Especially useful since it appears to throw exceptions. That means it's a step up from Iced Coffeescript. The syntax is a little verbose and might be shortened to something like C#: `user = await db.users.findOne {name:userName}`. Would be a lot more clear to me.

Definitely play with the exception handling. Latent bugs aside, asynchronous statements (wait fors) work in any crazy horrible nested combination of ifs, fors (parallel or series), and trys that you can think of. Take a look at the JS output with: kal -o output.js -f beautify input.kal to see the sausages being made. The http server demo is a good example ( https://github.com/rzimmerman/kal/blob/master/examples/async…

A side-by-side line-by-line interactive display would be wonderful in writing Kal. Reading the JS explains what it's doing quite well, except that the Kal compiler uses way too many compound comma-separated statements instead of semicolons.

Re: Kal – a clean JavaScript alternative without callbacks

#44
post #40

Is it possible to cut out some of the explanatory comments in the examples or to consolidate them somehow? It's difficult to see at a glance how the code looks, especially without any syntax highlighting at this stage

I added syntax highlighting to the github landing page manually for now. Try the .tmbundle if your editor supports it. It's not full featured but it gets the colors going. I'm working on getting it into pygments.

Re: Kal – a clean JavaScript alternative without callbacks

#48
post #47

Does anyone know if there is a JS dialect out there that adds support for callback-less async code without also adding a bunch of other coffeescripty syntax changes at the same time?

That would be TameJS: https://github.com/maxtaco/tamejs

Re: Kal – a clean JavaScript alternative without callbacks

#49
For the curious, the equivalent example from the OP using promises[1]:

  function getUserFriends(userName) {
    return db.users.findOne({ name: userName }).then(function (user) {
      return db.friends.find({ userId: user.id });
    });
  }
[1]: http://promises-aplus.github.io/promises-spec/
Post reply on HN