2009-05-24

JavaScript & REBOL

After working with both languages a great deal, I’ve come to the realization that JavaScript is the language in common use that’s most akin to REBOL. There’s still a very wide gulf, but there are enough similarities to make it worth a mention. (Not much more than a mention, though.)

At first glance, JavaScript and REBOL don’t look much alike, even when the code is doing the same thing.

// JavaScript
function foo(s) {
 return s + 'foo';
}
; REBOL
foo: func [s [string!]] [
 rejoin [s "foo"]
]

We can start to make them look a little more similar if we put the JavaScript inside an object literal and the REBOL inside a block. We’ll also omit the type specifier from the REBOL function, since JavaScript has no equivalent.

{
 foo: function(s) {
  return s + 'foo';
 }
}
[
 foo: func [s] [
  rejoin [s "foo"]
 ]
]

(I should point out that a block and an object literal are not equivalent, but their capabilities overlap a little bit. REBOL’s blocks are much more powerful.)

And that’s about as far as we can go with that. Anyway, it’s not lexical similarity that concerns me here, but functional similarity. For instance, both languages are scripting languages in the sense that they are not (usually) compiled. No big deal there. That’s true of a long list of languages.

However, both languages are prototype languages, and that’s certainly not true of a lot of languages.

function Foo() {
 this.watusi = 3;
}

function Bar() {
 this.watusi = 4;
}

Bar.prototype = Foo;

In REBOL, it’s similar, although in my opinion slightly more elegant, even if its syntax looks a bit strange to the curly bracers.

foo: make object! [watusi: 3]
bar: make foo [watusi: 4]

The serialized form of both languages is the language itself. In JavaScript, the JSON format is often used as a way to communicate between client and server. A very similar thing can be done with REBOL, although it has no specific name. (There really isn’t any need to have a special name or notation in REBOL. It’s just REBOL. In fact, being used for messaging is one of the fundamental things REBOL was designed to do.)

{ // JavaScript object literal
 orderno: 37,
 items: [22, 873, 14] // A list of order item numbers
}

What you see above is exactly what the server would transmit. We’d use it in JavaScript by assigning it to a variable.

var order = getOrder(); // getOrder() returns the object literal specified above
alert(order.orderno);

Here’s one REBOL equivalent (of a great many). Here’s what we’d transmit:

orderno: 37
items: [22 873 14]

And here’s how we might use it:

order: get-order
print order/orderno

The reason I didn’t place the REBOL “object literal” inside a block is because it isn’t necessary. When the string is read from the server, the LOAD function will automatically place it inside a block. GET-ORDER would be implemented something like this:

get-order: does [
 make object! load http://localhost:999/foo
]

In this particular case I tried to make the REBOL look as close to the JavaScript as I could to emphasize their similarities. I would probably implement the above very differently in REBOL under ordinary circumstances, although there’s nothing wrong with how it was done here. Because REBOL has DSLs and the evaluation of blocks is deferred (unlike JavaScript’s object literals), we have quite a few more options.

Another similarity is that both languages allow functions to be passed as arguments.

function callF(f) {
 f();
}

callF(function(){
 alert('called!');
});
call-f: func [f [any-function!]] [
 f
]

call-f does [print "called!"]

Both languages are amazingly nimble, although in a flexibility contest REBOL would win easily. It can do everything JavaScript can do and then some. Everything except manipulate the DOM, that is.

33 comments:

  1. Some REBOLers comment here:
    http://www.rebol.org/aga-display-posts.r?post=r3wp424x4064

    ReplyDelete
  2. Wow amazing article, I have added your blog to my rebol's blog:
    http://reboltutorial.com/rebol-network/

    It's a beginner's blog for beginners :)

    ReplyDelete
  3. "However, both languages are prototype languages, and that’s certainly not true of a lot of languages."

    Lua is a lot like JavaScript.

    ReplyDelete
  4. Yes, Lua is an interesting language. I like it. But it's still true that not very many languages are prototype languages.

    ReplyDelete
  5. but what good is this rebol thing ? its not like they can take a generic JW Player
    http://www.longtailvideo.com/support/jw-player-setup-wizard?example=247

    and HD stream and control that H.264 video content like the JavaScript generating page can from their rebol web one liners or even their pro web server things.

    and even IF they could somehow cobble something together to wrap around the stream H.264 jw-player , it seems the few rebol dev's capable have ZERO Interest in anything remotely graphical or video streaming related judging by the Total lack of any example video scripting apps.

    how many years have they been around ? and not one simple fully working streaming video plug-in or stand alone example to be found, were as every one and his dog knows you want Food embedded HD video on your site, use jw-player and JavaScript.

    didn't their rebol leader and his mate Dave something or other once make the worlds best home video hardware and software OS or something,shame their users cant even bother to write a few working video apps like every one else on the net seems to want to use....

    ReplyDelete
  6. i guess from the masses of reply's then, that rebol's just a page filler for some extra cash, and everyone just continues to use JavaScript to make their real cash ;)

    ReplyDelete
  7. I think you've missed the point, Mr. Anonymous. I'm not comparing REBOL and JavaScript in terms of their market success or employment potential. I'm comparing them syntactically, purely for language enthusiasts. Believe it or not, there are people who like programming languages merely because they are interesting in some way.

    So this isn't about "cash". I know a number of languages with which I can and do make plenty of money: C#, JavaScript, Objective-C, and so on. While I hope that I can one day make money with REBOL, it will never be as a client-side browser scripting language. I like REBOL because I find its syntax interesting. That's it.

    ReplyDelete
  8. oh well that's fine then, in that context yes it seems interesting.

    i thought id go and actually look at what scripts are available as it was today.

    its shame most/many example scripts fail in some nasty way when you open rebol view (any version available) shell window and

    do http:// a direct url to most of the scripts found here though
    http://www.rebol.org/st-topic-details.r?tag=//ui

    i would have thought someone that cared would have fixed that basic problem at some point.

    do http://www.rebol.org/view-script.r?script=area-scroll-style.r

    as just one example, it doest look good when simple example scripts fail, or worse crash the rebol view script interpreter.

    p.s i do like your work, and don't mean to offend if i did.

    ReplyDelete
  9. i came across this while trying to find a simple connect and annonimous chat IRC channel were rebol people might hang out.

    its odd but there isn't one, and you need to register for all the chat options i could find, very odd, but no matter.

    http://github.com/jankom/RebToStatic

    an alpha/beta Compiler from Rebol to JavaScript. — Read more
    some readers might be interested in it here i assume.

    ReplyDelete
  10. : COMPLETELY RENEWED!Party App gives you the complete dutch Party agenda on your iOS device. downloadthefilefast.date From Tag Games: At the end of a hard week have you ever wanted to just let loose and smash some stuff up.

    ReplyDelete
  11. Please note:Paying via InApp-Purchase for the full version is a one-time payment. Visit my site IF YOU LOVE THIS GAME AND WANT TO SEE MORE PLEASE RATE US.

    ReplyDelete
  12. Set the drumming timer for up to infinity and listen in meditation or while performing everyday tasks. download pdf - If you plan to take a taxi and need to figure out how much the price is, we have this function included as well.

    ReplyDelete
  13. Receive high street discounts for regular trackingAWESOME PHOTO COMPARISON TOOLS:- Try on your before photo for size. download software It's easy to view a video in one page while reading an article in the other.

    ReplyDelete
  14. This is really great work. DO My C programming Homework Thank you for sharing such a useful information here in the blog.

    ReplyDelete
  15. Really i appreciate the effort you made to share the knowledge. This is really a great stuff for sharing. Keep it up. write my essay for me Thanks for sharing.

    ReplyDelete
  16. Oddly enough, the sidebar didn't get the same treatment as the rest of the browser. download videos The search result would give information about their address, phone no, rating and URL (if one has).

    ReplyDelete
  17. Hi dear, I like your post so much, and it is very good. Hope that we can communicate with each other. I want to introduce an aso services, I try it and I feel it is so good to us beginners.

    ReplyDelete
  18. Enabled the ability to delete messages, items on your watchlist or offers. downlodable ebooks At first the police -- and even the women -- refuse to take the threats seriously.

    ReplyDelete
  19. In addition, the current LIFEmee website can be used simultaneously with the new iphone application. shotokan karate syllabus pdf What's more, C#--the language designed from the ground up for programming the Microsoft .

    ReplyDelete
  20. It seems simple but some of the combos are harder than you might think. download torrent As the most notable commander, you have to face them.

    ReplyDelete
  21. Aesops classic bedtime fable, The Ant and the Grasshopper, teaches kids important lessons of hard work paying off. downloadgoodfilesonline.top After completing each level, feed your hungry frog as many flies as you can in 30 seconds.

    ReplyDelete
  22. The longer you drag, the further the tongue will stick out. Visit my site Written with the latest, most advance programming languages and techniques, including Microsoft .

    ReplyDelete
  23. No unreadable text, no endless scrolling on a tiny screen. downloadfastplease.xyz Selected as one of Travel and Leisures "Best Travel Apps" of 2010.

    ReplyDelete
  24. Higher percentages will unlock silver and gold achievements. http://gooddownloadtorrenttoday.us Section Bookmarks: You can save 12 sections of your choice; like skipping the bridge passages or setting the section that you want to learn.

    ReplyDelete
  25. From Tarequl Hasan: 80s Music Radio Recorder offers the best 80s music available in the world. downloadfromthisblog.gdn Dozens of different enemies and objects to interact with.

    ReplyDelete
  26. Movement: Will only continue execution when the device is tilted or accelerated in any direction. topdownloadwarezcatalog.us 2 - Added zombie mode - Removed drunken man - Reduced Lite version&'s limit.

    ReplyDelete
  27. The Godking's successful coup has left Kylar's master, Durzo, and his best friend, Logan, dead. downloadtorrentfromnora.online Le club investit le Stade Yves du Manoir, le stade Sabath tire sa rvrence la premire, mais continuera daccueillir les autres quipes du MHR.

    ReplyDelete