Voodoo Tiki God

I am Zero Cool

The Real Y-Combinator (in JavaScript)

I want to get to this eventually in Naked JavaScript, but for the time being I wanted to share a great little code bit with you in JavaScript. Also given Paul's recent post, I decided it would be good to continue the functional JavaScript by providing a different, way off accomplishing tail call optimization. As I mentioned previously, JavaScript has a strict limit on the call stack of around 1,000 entries (varies based on interpreter implementation) which commonly prevents any level of deep recursion, especially if writing portable JavaScript for multiple browsers. Paul's method of leveraging an accumulator wrapped in a closure is a great method for accomplishing a recursive task in a stack or memory constrained environment. So great in fact that the basic pattern of detecting repetition and translating it to a linear fashion is very similar to the underpinnings of what TraceMonkey automatically accomplishes using Trace Trees.

From Paul's description of the problem at hand, we are trying to find the odds of becoming the next mega-millionare. The following is the imperative solution provided by Paul using a variable to accumulate the odds at any given step in the process as the multiple of the previous value and the new expression result.

Paul presents the conversion of this code segment to a simple recursive function that will help us to find the value of by calling the function over and over with varying values defined within the function until a termination clause is reached. In this case the termination clause is when the variable n equals the value 0.

With the basic code from those two examples and a utility for the spell books of lambda calculus, we can actually create a recursive function and then apply it to the exercise. Mind you that this will still suffer from the aforementioned call stack limitations placed on the JavaScript VM due to its applicative nature, and realistically there are far better ways for obtaining the same result (one of which shown at the end), but to hell with all that.

By using a technique known as Fixed Point Combinator, and specifically a Y-Combinator we will not on obtain the result but build recursion without using a named function. To begin, a Fixed Point Combinator is a special type of higher order function that has the unique transitive property that for any function F and value V, the value of the Y-Combinator function G when passed the parameter F will return the value V which is the same value as when V is passed to F. Lost? Lets look at this description as a the transitive function definition:

F(G(F)) == G(F)

The definition of a combinator relies on the lazy evaluation of functions in order to allow a function to be defined in terms of itself, often times referred to as anonymous recursion. This requires a deep understanding or interest in lambda calculus and this function is deviously complex and compact. For more details about a Y-Combinator, which is a specific formula of a Fixed Point Combinator, look here. Let us look at it as a JavaScript function:

This definition is from The If Works and happens to be one of the most compact, but readable implementations I have seen and it even slips a couple extra JavaScript specific enhancements to make it even more flexible. The definition of this function is near exact to the formulaic definition described in Scheme from the link above with one key advantage. Using the arguments object in combination with the apply function that is available on functions, we can make this more flexible than the one described in The Little JavaScripter by Douglas Crockford by allowing it to handle any number of arguments passed in.

With this definition of a Y-Combinator, let us revise the original problem set using the code from the recursive definition. The most important part that we can take from the recursive definition is the conditional logic, in fact that is all we will be taking. The Y-Combinator version of this looks like this:

Which looks similar to the recursive definition and it really should, because all we have done is create recursion using anonymous functions instead of named functions. As I said earlier, this method is most likely not going to win you fame or fortune (but if it does I claim 5% of the winnings),  it is however a great example of the functional capabilities of the JavaScript programming language. All that said, here is probably the most terse definition of this function I could devise:

Filed under  //   computer science   javascript   js   nakedjavascript   recursion  

Paul Barry Shows How to Accomplish Tail Call Optimization in JavaScript

Local Polyglot Extraordinare, Paul Barry has been on a JavaScript spree of late, especially in showing how JavaScript compares to other full functional programming languages like Clojure, Haskell, and Erlang. In his most recent article, he goes about showing how to use the accumulator pattern, from the SICP book if I am not mistaken, to accomplish Tail Call Optimization in JavaScript. One of the things he doesn't mention and is a known issue with JavaScript in most interpreters is that after around 1,000 recursive calls the interpreter will exception out the execution regardless. This is done based purely on the call stack and not on memory usage in an (vain) attempt to be protective in nature due to the "constrained" client environment and operating sandbox of JavaScript. It more often then not becomes a pain in the tail (recursion) in the modern era where people have more than 64kb of memory. That said, Paul's technique is a great one that is widely applicable in many formats, not just JavaScript.

Here's hoping he continues to love the JS and what he is doing that has transpired these recent posts.

Filed under  //   javascript   js   local   novalang  

Naked JavaScript

I began writing on Posterous for a new personal project that I am currently working on which will provide a series of posts about "Naked JavaScript" or JavaScript without the DOM. The aspiration of this project is that I will post at least once a week (ideally twice a week) on the most interesting and hottest topics in JavaScript, specifically its use outside of the browser. I am just starting on this venture and want to fill the queue with interesting content and articles so that I can keep the hungry hordes of information goers (ahem, you) happy, so stay tuned to that feed as I am sure you will find some interesting items soon to come to it. 

Anyhow, so I began using Posterous for that and decided to use it for more personal things as well, I haven't completely switched over from my standard one, but I am getting very close. The fact that the iPhone 3GS can post photos and videos directly to the blog is a super sweet idea, and one that I am both excited and fretful about using. Anyhow stay tuned for future things to come and sorry about the crazy pictures and video posts earlier (since removed) I was testing out the functions and had no idea that it was posting to the various social networks (embarrassing). 

Filed under  //   javascript   js  

JavaScript: The Great Part

Laura and I just wrapped up JSConf 2009, which evidently went over very well. We couldn't be more happy with the way the conference went; the attendees, the speakers, the excitement, and the energy - it was the best conference we have ever been to, even though we spent most of the time running around. There was a lot of time and preparation that went into JSConf 2009 to ensure that each detail, each idea was carefully tailored to the community. For those of you that attended and those that were unable to, we will be posting the videos over the coming months - every presentation was amazing so be sure to check out all of them.

On to the focus of this post - the JavaScript community has long since taken a side saddle or back seat to other less capable and less prevalent languages. The presentations drew out the amazing genius and talent that happily and quietly thrives in the community. If you just look at the Track A sessions (listed on the web site) you will see that JavaScript spreads across mobile, data, desktop, testing, and its old familiar, the web. But its more than that, what you don't see on the web site (yet) is the wonderful Track B and Hacker Lounge items that happened during JSConf 2009. The presentations in Track B were easily all on par with Track A and covered an even wider range of topics from typography to programmatic music generation to server side JavaScript.

That all said, the most amazing part of the conference was not the presentations. At this conference you had some of the smartest people in the programming world talking about a language that is unfortunately thought of as a necessary evil. Everyone was jovial, welcoming, friendly, and communal it was truly a community. There was no dominant "rockstar" that parade around overly proud of themselves, despite the amazing things that every individual in attendance has (and will further) accomplish. That is the greatest part of the JavaScript community - it is truly a thriving community flush with talented people. This doesn't just include  the speakers and attendees. The sponsors of JSConf - R/GA, Mozilla, Joyent/Sun, and Yahoo! - were amazingly willing to turn down the "marketing" and instead embrace and grow the community. Their own spread of capabilities - Digital Agency, Web Company, Platform Providers, Search and Development Networks, respectively - shows the range and impact of JavaScript. The fact that there was 130 of the smartest, most driven, and widest ranging people and companies present at JSConf 2009 and each person you met was incredibly humble and friendly - that is how I know that this is the community I want to be a part of.

This was the first conference that actually turned and focused in on JavaScript, the programming language, so this was a unique experience and that may have a hand in the humility of all - most people in attendance barely knew more than 4 other people in attendance, so we all arrived forced to make new friends. There is something there, though, that makes me proud of this community; instead of shelling up attendees made a concerted effort to meet, greet, and build relationships with one another. This conference was more than just a single event or moment in time - we have started a revolution. We are building a better community because we, JavaScript developers, have the rare capacity to understand that there is more to learn from using many languages and concepts than to arrogantly assume ours is the best and only solution. I am not trying to decry the value, processes, or importance of other language communities, just making a stand that we should continue to grow the JS community with a focus on talent, humility, and cooperation.

We are JavaScript, we welcome you to join us for the ride!

Filed under  //   javascript   js   jsconf