Live data from Hacker News

Kal – a clean JavaScript alternative without callbacks

rzimmerman.github.io

61–70 of 130 posts

Re: Kal – a clean JavaScript alternative without callbacks

#61

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…

If you are looking for a syntax closer to Haskell's, you can take a look at LiveScript (http://livescript.net/)

eg.

    getUserFriends = (userName) ->
      user 
Specifically, the section on "backcalls": http://livescript.net/#backcalls

Re: Kal – a clean JavaScript alternative without callbacks

#62
post #46

I'm puzzled by the bootstrapping process. It's written in Kal itself, and you distributed compiled code as JS. Do you have a way of re-boostrapping from only the source in github?

The only way to re-bootstrap is to download a compiled (JS) release from npm, then use that to compile the .kal source in the git repository. Then you can use your newly compiled version of kal to recompile itself. This is how I create the npm releases. It's not turtles all the way down, though. If you look way back, the 0.1ish releases of the compiler were written in CoffeeScript. I ported the compiler source over f…

Interesting. It must get tricky when you make an incompatible change to the language. I guess you'd make the changes to the compiler still using the old language, update the test cases, make it stable, then change the compiler sources to reflect the new language, recompile the compiler, then check in the new compiler JS. With plenty of git checkpoints along the way.

Re: Kal – a clean JavaScript alternative without callbacks

#66
post #58

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…

This is beautiful work! I see small shades of inspiration from the .Net TPL[1] and the more recent async-await paradigm[2] in C#. But, this is way more concise and beautiful implementation :-) [1] - http://msdn.microsoft.com/en-us/library/vstudio/system.threa... [2] - http://msdn.microsoft.com/en-us/library/vstudio/hh191443.asp... and http://msdn.microsoft.com/en-us/library/vstudio/hh156528.asp...

Thanks! I was wondering if someone would make that comparison. I saw async-await a little after I started working on this and felt it was good confirmation that this was actually a reasonable idea.

Re: Kal – a clean JavaScript alternative without callbacks

#67
post #62

Earlier quoted context omitted.

The only way to re-bootstrap is to download a compiled (JS) release from npm, then use that to compile the .kal source in the git repository. Then you can use your newly compiled version of kal to recompile itself. This is how I create the npm releases. It's not turtles all the way down, though. If you look way back, the 0.1ish releases of the compiler were written in CoffeeScript. I ported the compiler source over f…

Interesting. It must get tricky when you make an incompatible change to the language. I guess you'd make the changes to the compiler still using the old language, update the test cases, make it stable, then change the compiler sources to reflect the new language, recompile the compiler, then check in the new compiler JS. With plenty of git checkpoints along the way.

One of the gems in CS literature illustrates this nicely: http://cm.bell-labs.com/who/ken/trust.html

Essentially self-compiling compilers can "remember" things that are nowhere to be found in the source code.

Re: Kal – a clean JavaScript alternative without callbacks

#68

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/

Promises + CoffeeScript really hits the sweet spot for me:

    getUserFriends = (userName) ->
      db.users.findOne(name: userName).then (user) ->
        db.friends.find userId: user.id
Or prezjordan's example:

    getUserFriends = (userName) ->
      db.users.findOne(name: userName)
        .then (user) ->
          db.friends.find userId: user.id
        .then(function (friends) ->
          somethingAsync(friends)
The concise function syntax and implicit "return" really helps.

If that's not good enough you can implement higher level control flow abstractions much more nicely than with raw callbacks, e.x. https://github.com/tlrobinson/q-step

    getUserFriends = (userName) ->
      QStep(
        ->
          db.users.findOne name: userName
        (user) ->
          db.friends.find userId: user.id
        (friends) ->
          somethingAsync friends
      )
Post reply on HN