Pages

Saturday, August 21, 2010

λ as a variable name in JavaScript

JavaScript is a functional language, or at least has very functional roots we could all probably agree.

Functional languages are based on lambda calculus and the CS literature often uses examples where a lambda expression is called simply, "lambda."

In JavaScript, one common use of lambdas is when you register a callback function with some asynchronous operation like an ajax request. Another is for operations on collections of objects like visiting every item in an array.

I wondered if one could use the lambda character, λ, as a variable name in javascript. It would be more compact (one character instead of five six) and (more importantly) would look cool. Turns out you can do this!

See it run here.



Notice that the character encoding is set to UTF-16. If you don't do this you'll get errors. Since the encoding is set that way, I don't even use html entities like λ to write λ :)

Note: I haven't tried this in any version of IE but it appears to work in Chrome, Safari and Firefox.

Now, how do you actually type λ into a text editor? That took some digging. Here's what I did on OSX:

  1. Open System Preferences -> Language and Text -> Input Sources
  2. Scroll down to "Unicode Hex Input" and make sure that checkbox is checked.
  3. Open the US Flag (or nationality of your locale :) icon in the upper right of the OSX menu bar and switch to "Unicode Hex Input" (the "U+" icon)
  4. Now whenever I want a λ I can just hold down the Option key while I type 03bb and blammo I get a λ. Which by the way is still five key presses but looks way cooler than "lambda" and takes up less space on the screen.

I have no idea what you have to do on linux or windows.

You're right, this isn't very practical. And setting your encoding from UTF-8 to UTF-16 like, doubles the bytes you have to transmit. But you ARE gzipping all of your http responses aren't you? AREN'T YOU?

19 comments:

  1. It works in IE >= 6 too. I've tried it some time ago.

    What I did to type it faster was to create a TextMate snippet triggered by CMD+\, which is the same shortcut used in DrRacket, and which also resembles the way lambda functions are written in Haskell.

    Anyway, I've never used it in an actual project. It was just for fun.

    ReplyDelete
    Replies
    1. A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.


      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
      Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

      Delete
  2. Software application devices beep.com/ that are paid in Google Play Store are offered by TuTuApp Vip Apk Know More for free We'll be discussing Guide to Download Tutu Helper Android App, APK, iOS the procedure of downloading tutuapp vip apk Tutu Helper App on Android and also Tutu Helper VIP App iphone devices.

    ReplyDelete
  3. Whether you just intend to set up inube.com promptly at a campsite or obtain an Click Here extremely rapid entering your Outdoor Grill 2017: Guides, Top Picks and Reviews lawn, the Coleman Road Trip is Outdoor Grills developed to configuration Best Outdoor Grills Reviews swiftly. If you have restricted space.

    ReplyDelete
  4. This information is very useful for me. thanks for sharing
    TUTUApp Free

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. tutu vip

    ReplyDelete


  7. Tutu Helper is the one of the best ios,android App store to get the tons of free app and game. Here the latest version of TutuApp of free.
    Tutu Helper Apk
    TutuApp free
    tutuapp pokemon go

    ReplyDelete

  8. Tweakbox App is the one of the best ios,android App store to get the tons of free app and game. Here the latest version of tweakbox of free.
    Tweakbox Apk
    Tweakbox for ios
    Tweakbox for android

    ReplyDelete

  9. Whatsapp plus is one of the best mod app for official whatsapp for free, get unlimited features of

    whatsapp plus for free. Here Click to download the latest version whatsapp plus apk.
    WhatsApp Plus APK
    WhatsApp Plus Download

    ReplyDelete
  10. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.
    360digitmg

    ReplyDelete