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.

45 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. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. Pull in people for work or play as and when you need them. http://topdownloadfilesdatabase.us It provides children with the possibility to express their ideas and feelings.

    ReplyDelete
  28. We find lots of learning after reading this very useful article .

    ReplyDelete
  29. just information we only provide information for those who need it. tree service

    ReplyDelete
  30. 20 public general, science and technology university is published the guccho university admission result
    online on gstadmission.ac.bd. Students will need ssc and hsc roll number, board, passing year information to check the result.

    ReplyDelete
  31. National University Honours 4th Year Result 2021 has Been Published very soon. As a NU 4th Year Student, You Might be Search For NU Honours 4th Year Result their official website & our website available it.

    ReplyDelete
  32. DSHE is published the All Subject Class 10 Assignment Answer 2022 for all week on assignment.examresulthub.com.
    Want to check the all assignment work and answer, make sure to follow the below subjectwise links:

    DSHE is published the SSC Assignment Answer 2021 for Class 10 for three weeks. lets check the assignment work with answer.

    ReplyDelete
  33. Thanks for sharing the article.

    ReplyDelete
  34. Jackpot City choegomachine.com accepts all major bank cards, properly as|in addition to} e-Wallets like Skrill and Neteller. Payouts are quick by trade requirements, whereas each transaction use 128-bit SSL encryption know-how. There’s no better way to improve your bankroll than utilizing a bonus at an online on line casino.

    ReplyDelete
  35. In case the purpose complete of the first two playing cards is the same as} eight or 9, the Player who hits this score is the one who 1xbet wins the hand. Once the vendor locations two playing cards on every half of} the desk, it’s time to calculate the points and determine the value of every hand. If a hand exceeds 10, 10 is then subtracted from the hand. Neither the Dealer nor the participant determine if a third card is drawn.

    ReplyDelete
  36. carbontvpbr is the go-to destination for PBR fans who seek an unmatched and comprehensive viewing experience. With its thrilling collection of videos, highlights, documentaries, and series, Carbon TV offers an all-encompassing platform that encapsulates the thrilling world of bull riding. Whether you're a devoted follower of PBR or a curious newcomer, Carbon TV guarantees an exhilarating and captivating journey into the world of Professional Bull Riding.

    ReplyDelete
  37. Looks like a pretty cool place to visit.

    ReplyDelete