Showing posts with label ecmascript. Show all posts
Showing posts with label ecmascript. Show all posts

10 July 2010

Is Apple prepping a new programming language?

waffle → Surpass:

It is my belief that Apple is definitely working on a new language to surpass Objective-C as their intended, primary, publicly recommended programming language...

I’ve been thinking that it seems very strange that Apple’s primary language is (still) Smalltalk grafted onto C.

If he’s right, I hope that, like the A4 processor (which is ARM based) and Safari (which is HTML, CSS, and Javascript based), it isn’t a wholly new language.

Would Smalltalk be a hard sell to developers who have accepted Objective-C?

28 May 2010

iMac apps vs. iPad apps

In a comment to I’ve Changed My Mind About The iPad, Joeflambe wrote:

yuck, an iPad, really?

not a real computer since there are no real apps that you use daily on your current computer.

This seems like an interesting exercise. Let’s look at the most used apps on my iMac and whether there is an iPad equivalent.

  • Safari: Check

I have a Flash blocker installed on my iMac, so lack of Flash on the iPad isn’t much of a change for me.

  • Mail: Check
  • iChat: Check (AIM)
  • iCal: Check
  • Address Book: Check
  • iTunes: Check
  • iPhoto: Check
  • GarageBand: Check (StudioTrack)
  • Evernote: Check

Now, to be fair, the iPad equivalents of the above do not always have all the features of their Mac counterparts. The main functionality is there. The rest...well, the iPad isn’t even a year old yet.

  • Preview (as PDF reader): Check

The iPad has PDF reading built in, but no specific app for it. I do most of my PDF reading/referencing in GoodReader.

  • iWork: Check

Some people will say matter-of-factly that nobody is going to want to write a term paper or create a spreadsheet on an iPad. shrug

OK, Pages for iPad doesn’t have footnotes...yet. It can’t print directly from the iPad—though Jobs himself has allegedly acknowledged that printing is in the works. (Yes, I’ve seen the photocopier pic. I LOL’d...the first time.) With my Bluetooth keyboard, I can’t see why I wouldn’t write a lengthy document with Pages for iPad. Even if I currently have to move it to my iMac for the finishing touches.

The whole keyboard + iPad topic is fodder for its own post.

I’ve created a few spreadsheets on the iPad. No less complex than those I would have created on my iMac. Although, I’m not that much of a spreadsheet user.

The iPad actually seems like a fine platform for creating presentations. We’ll see how it goes when I next need to create one.

I recently bought this Mac app. In fact, I bought it—in part—to produce content for the excellent TabToolkit iPad/iPhone app. (The fact that I can’t use it for that at the moment could be another post.) This is another app—like Keynote—that might actually work as well, if not better, on the iPad.

Unfortunately, Apple’s rules don’t allow for an equivalent to DrScheme.

What if? Well, if it were a fairly straight port, it probably wouldn’t work very well without a keyboard. (Which is OK; I have one.) I can imagine a visual/touch interface for editing Scheme source code, though. (I’m imagining something like what I imagine the Viaweb editor was like.)

I think you could do a “DrEcmaScript” within Apple’s rules, though. As either an app (if it used WebKit) or a web app. I suppose a Scheme interpreter written in Javascript would be allowed, though perhaps not practical.

So...only two strikes.

It might be interesting to look through my iPad apps and see which ones don’t have Mac equivalents...or those for which I wouldn’t want a Mac equivalent.

Is the iPad going to replace my iMac? No, I don’t think so. Not anytime soon. It can, I think, come astonishingly close, however. There is a lot of overlap between the Mac, iPad, and iPhone; but there’s also enough outside those overlaps to justify each. The overlaps mean more flexibility.

The iPad may not replace my iMac, but imagine an iPhone OS desktop device. That might. Of course, being a programmer, I might still want a Linux/FreeBSD system for tinkering. (One of the nice things—for me—about Mac OS X is that I can get to the BSD personality underneath it when I want to.) For getting things done, however, I’m happy to use “app consoles”.

08 May 2010

Scratch (no longer) on iDevices

More iPhone PL lockdown...Goodbye Scratch!

My wife has taught a couple of classes using Scratch with young kids, and to see the pride they feel at their creations is a marvelous thing. I think restricting their ability to share that feeling is really reprehensible. And the damage done to the programmers of tomorrow? Hard to say...

Damage?

Should Apple allow Scratch on iDevices? Yes, I think they should. Does the lack of Scratch on iDevices mean that kids are prevented from programming for iDevices? No. I looked over Scratch, and everything important that it does could be done in Javascript and work with Mobile Safari.

Is Javascript slow on iDevices? Yes. Is it slow enough to make something like Scratch impractical? I doubt it.

07 May 2010

Apple’s mistakes

From the iPhone OS 4 SDK, section 3.3.1 of the iPhone Developer Program License Agreement:

3.3.1 — Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs. Applications must be originally written in Objective-C, C, C++, or JavaScript as executed by the iPhone OS WebKit engine, and only code written in C, C++, and Objective-C may compile and directly link against the Documented APIs (e.g., Applications that link to Documented APIs through an intermediary translation or compatibility layer or tool are prohibited).

This is one of the ways that Apple is keeping Flash off iDevices. It—intentionally—keeps more than Flash off of them. (See “Thoughts on Flash”.)

It is, unfortunately true, that cross-platform frameworks often have lousy user experiences. Not merely “un-Mac-like” but just plain bad. It’s true that they often only end up supporting the lowest common denominator. It doesn’t have to be like that, however. Cross platform frameworks can support platform-specific features. I’ve worked on ones that have. It is all too rare though.

There’s also the fact that some apps—especially some games—don’t really need standard user interface elements. Flash (and other options) can work well for these things. If anyone chooses to use Flash for another sort of app, they’ll quickly lose to non-Flash apps. I’m glad not to have Flash in Safari on my iPhone or my iPad, but I don’t mind Flash apps.

Section 3.3.1, however, doesn’t only ban cross-platform frameworks. It bans writing honest-to-goodness Cocoa Touch apps written in any but the specified languages. (“Platform Control” by Mark Bernstein.)

It has been suggested that a compromise might be to require any frameworks to be open source. (“A reconciling proposal” by Michel Fortin.) That way, any developer that needs access to a specific feature can add it to the framework. That’s a good idea. Yet, the truth is that such extensions to a framework can be a lot of work—especially for someone who normally only uses the framework.

Ian Bogost has written that “Flash is not a Right”. He is right, I suppose.

Putting aside rights, however, I have to wonder if this is really in Apple’s best interest. I have to wonder if this is really in the best interest of Apple’s customers. Apple’s biggest mistakes, I believe, are when they try to compete via the law. Whether through intellectual property or license agreements. They compete and win in the market. They don’t need to play the legal games.

12 November 2009

The problem with Javascript’s map

This is about Javascript programming. If you’re not interested in Javascript programming, bail now.

Say you have a Javascript array of strings that you want to convert to numbers.

['1', '2', '3'].map(parseInt) ⇒ [1, NaN, NaN]

Hmm. Why didn’t that work? Because parseInt takes an optional parameter (the radix)...

parseInt('ff', 16) ⇒ 255
parseInt('2', 1) ⇒ NaN // Because a radix of 1 always returns NaN
parseInt('3', 2) ⇒ NaN // Because 3 isn’t a valid binary digit

...and map passes an optional second parameter (the index).

[1, 2, 3].map(function(n, i) [n, i]) ⇒ [[1, 0], [2, 1], [3, 2]]

Occasionally it is very handy to have map give you that index. Often, however, I want to give map a function—like parseInt—that wasn’t explicitly designed to be used with map.

Here’s how to fix it.

['1', '2', '3'].map(function(_){return parseInt(_);});

Yuck. Not only is that verbose, it looks downright silly since the closure looks pointless.

Expression closures can address the verbosity...

array.map(function(_) parseInt(_));

..., but (currently) only Firefox versions 3.0 and later support that. Plus, it still obscures why you’re doing it.

In Scheme, I’d use cute from SRFI 26. It allows you to specify some parameters of a function. The “<>” is used for parameters that you don’t want to specify.

(map (cute string->number <> 10) '("1" "2" "3"))

Of course, while string->number takes an optional radix parameter like parseInt, Scheme’s map doesn’t pass the index, so this isn’t necessary.

Back to Javascript. A “unaryize” function looks nicer than an explicit closure and makes what you’re doing clearer.

function unaryize(f) {
return function(_) {
return f(_);
}
}

['1', '2', '3'].map(unaryize(parseInt)) ⇒ [1, 2, 3]

We could generalize unaryize to naryize.

function naryize(f, n) {
return function() {
return f.apply(null,
Array.prototype.slice.call(arguments, 0, n));
}
}

['1', '2', '3'].map(naryize(parseInt, 1)) ⇒ [1, 2, 3]

In practice, I’ve run into uses of unaryize several times, but I haven’t yet run into a need for naryize. Also, naryize is less efficient than unaryize.

What if we want to specify a radix? We could create a Javascript version of cute. I’ll use undefined instead of “<>” for the slot specifier.

function cute() {
var cutargs = Array.slice(arguments);
var f = cutargs.shift();
return function() {
var args = Array.slice(arguments);
var fargs = [];
var i;
for(i = 0; i < cutargs.length; ++i) {
if(undefined === cutargs[i]) {
fargs.push(args.shift());
} else {
fargs.push(cutargs[i]);
}
}
return f.apply(null, fargs);
};
}

['a', 'b', 'c'].map(cute(parseInt, undefined, 16)) ⇒ [10, 11, 12]

Again, I haven’t yet run into many cases where I’d want to use cute that unaryize doesn’t suffice. In this specific case, I think I’m happy with the explicit closure.

['a', 'b', 'c'].map(function(_){return parseInt(_, 16);});

Additional notes:

Array.map can be implemented in Javascript and added without any changes to the interpreter. I’m using Prototype’s.

Expression closures can’t be added without changes to the interpreter, but they can be faked with strings. See Functional Javascript

While cute can be implemented as a function, SRFI-26’s cut has to be a macro. Likewise, a cute macro is more efficient than the procedure implementation.

26 March 2009

Scheme in the browser (again)

I found myself reading “Popularity” over at Brendan’s Roadmap Updates...again.

This time, a comment by Mike Ivanov stood out to me:

It took almost a decade for ‘average developer’ to grok JavaScript. Now it is understood, at least. How much time would pass before Scheme achieved the same level of acceptance? I bet forever.

Which again has me questioning that. I can’t believe that anyone who has really “grokked” Javascript couldn’t have grokked Scheme. After all, Javascript essentially is Scheme with C syntax. Grokking the language behind that syntax is much more difficult than grokking the syntax.

I think that Javascript was in a unique position. I suspect almost no-one learned Javascript for its own sake. People learned Javascript because it was (essentially) the only game in town for the role it played. (Indeed, it is still too difficult, IMHO, to use Javascript outside that role.)

That’s certainly why I learned it. At the time, I would’ve much rather used Perl, but my customers had Javascript built into their browsers. I wasn’t going to ask them to install client-side Perl.

Likewise, I suspect that Javascript is one of those languages that was the first programming language for a lot of people. Those are people who aren’t coming in with syntax-bias.

Which may be pointless speculation, but that’s what thinking-out-loud is for. I still give thanks to Brendan that I’m required to regularly, at work, program in Scheme/Self even if it is with C syntax.

08 April 2008

Scheme in the browser

The creator of JavaScript on it’s creation:

Brendan’s Roadmap Updates: Popularity

As I’ve often said, and as others at Netscape can confirm, I was recruited to Netscape with the promise of “doing Scheme” in the browser.

Interesting. I wish he hadn’t also had the mandate to give it Java-esque syntax.

I think. Sometimes, though, I almost think that I enjoy JavaScript more than Scheme.

12 January 2008

Objective-C++

It has long seemed to me that C++ and Objective-C are very complimentary. It is interesting that they are actually very easy to combine. The result is known as “Objective-C++”. C++ is nice for “value” classes. i.e. Classes whose instances tend to the value of variables. Operator overloading is particularly nice for numeric classes or “smart” pointers. Exceptions are nice too. (Though I still wish C++ had “finally”. Yeah, I know the usual C++ alternative, but that doesn’t mean it can’t be convenient.) Objective-C is nice for “reference” classes. i.e. Classes whose instances tend to be referenced by pointer or reference variables. Add automatic garbage collection for the Objective-C objects, and you’ve got a really nice set of language features. Last I looked, just about all those pieces where there, but getting them to work together on whatever platform you’re on could be difficult. These days, though, I’m preferring vanilla C, Scheme, or even EcmaScript over C++ or Objective-C.

10 December 2007

Why I fear complexity in EcmaScript 4

Various thoughts I had whilst reading Brendan’s @media Ajax Keynote. I don’t see why every EcmaScript programmer can’t learn to use closures and prototypes. These are not difficult concepts, yet they are extremely powerful. Of course, if the new stuff is really less verbose and less error-prone, I’m all for it. It’s better, however, if any more concise and less error-prone elements are built on top of existing features rather than introducing new features. The thing that worries me about adding classes to EcmaScript is that it increases the complexity. An increase in complexity in software means an decrease in stability and a decrease in security. For EcmaScript, it also means raising the barrier for implementing and maintaining the language. Less complexity means that implementors can take time to do things like removing the memory leak bugs from their closure implementation rather than wasting time adding essentially duplicate features and, by the way, implementing those poorly as well. It’s not that I’m afraid I’ll be forced to use new features that I don’t want to. I want new features, and I want to use them! I’ve been writing software long enough to know the dangers of complexity, though. Features have to over justify the complexity they bring. Better to add macros that allow more concise syntax to use existing features than to add features because you don’t have macros. Putting more burden on people writing static analysis tools is exactly the wrong thing. EcmaScript needs to encourage lots of static analysis tools. And, by the way, the less complex they are, the better they will be as well. I’m all for evolution. I certainly think things can and should be made better. But I want it to be better. I think I mostly agree with Bredan except:
  • I think additional complexity needs huge justifications
  • I’m suspicious of claims that anything should be added because it will improve the sales pitch

19 October 2007

Inheritance is over-rated

Some more observations based on my experiences with EcmaScript (a.k.a. Javascript) as compared to C++ and Java:

(I suppose I could try to generalize and consider EcmaScript a representative of the Smalltalk tradition versus C++ and Java as representatives of the Simula tradition; but that probably opens a lot of other issues.)

It occurs to me that most classes I’ve written in C++ or Java have not needed inheritance. They provided only encapsulation. (Which I can do just fine in C—no object-orientation required.)

When I have needed inheritance, it has most often been for polymorphism. In EcmaScript, you don’t need inheritance for polymorphism.

(With C++, you can have polymorphism without inheritance, but that requires the complexity of templates. Likewise, in Java you can use reflection to achieve the same sorts of things, but you get more complexity with it.)

Sometimes I’ve abused inheritance in C++ or Java to work around limitations. Such as adding additional methods to an existing class. In EcmaScript, I can add methods to objects (or objects serving class-like roles) directly.

In EcmaScript, I only really need to use inheritance when I need to share an implementation, and that just doesn’t seem to come up as often in my experience.

(Moreover, I find closures—which EcmaScript has but C++ and Java lack—extremely useful.)

Now, there are—of course—trade-offs involved. EcmaScript isn’t all roses by any means. Give it a static debugger (like MrSpidey/MrFlow), and I think it could hold its own versus C++ or Java for many applications.

Give it hygienic macros (on the road-map for Javascript 3) and first-class continuations, and it starts to stack up well against Scheme as well.

08 October 2007

Speaking of IF and EcmaScript...

Robin Johnson has written a pretty decent text adventure engine in EcmaScript. I’m enjoying his Hamlet game.

EcmaScript, IF, and DSLs

Since I’ve been programming so much in EcmaScript (a.k.a. Javascript) at work, I decided to install the stand-alone version of Spidermonkey at home. That’s the EcmaScript implementation from Firefox. Partly because I’m interested in being able to write web applications that use the same language on both the browser-side and the server-side.

Of course, there are a few server-side EcmaScript solutions out there, but I’m used to rolling my own light-weight version when investigating such things.

I didn’t want to jump right into that, however, so I tried to come up with an idea for a command-line program I could start with. Spidermonkey comes with a command-line interpreter, but it is very minimal. Hardly more than readline and print.

So, I thought of text adventures (a.k.a. interactive fiction). I had the basics of Cloak of Darkness mocked up PDQ. It went faster and farther than I think any of my other attempts at such a program has gone in any language. My opinion of the EcmaScript is continuing to increase. I began to think I should break out some of my old text adventure ideas.

The thing is, though, I’d have to make some significant enhancements to Spidermonkey’s command-line interpreter to really make it practical for IF. It would make a lot more sense to just use browser-based EcmaScript (instead of command-line) or to use Inform, so that people besides just me could actually play it.

Inform is a DSL (domain specific language) for interactive fiction. That’s fancy computer geek jargon for a language specialized for a specific purpose.

C, C++, Java (no relation to Javascript), Perl, Python, Ruby, Lisp, and Scheme are general-purpose programming languages. They can be used to build programs for a wide range of applications. (Although, certainly, some are better for some applications than others.)

HTML and Postscript are domain specific languages. (Incidentally, HTML is not a programming language, but Postscript is.)

EcmaScript isn’t really a domain specific language, but as it is most widely and most easily used within web browsers, it often tends to be one in practice.

I’m all for DSLs, but the problem I have with most of them is that they’re wholly new languages. Ideally, a DSL—unless really simple—should be an extension or a subset of an existing language. This is the resistance I have towards using Inform.

The Lisp and Scheme advocates like to point out how they often extend their languages to create DSLs within them, which I’m finding to be a very compelling argument.