Posts Tagged ‘functional’

Function-wrapping and BDD-style specs in javascript

Monday, November 17th, 2008

I’ve been spending too much time with javascript lately. It probably isn’t healthy for me. I’ll get back to my own Clojure project and paying Ruby work soon enough, but right now I’m working on an interface-intensive project, so I’m up to my neck to explicit returns and key:value notation.

I was looking into improving my code (less coupling, more encapsulation, greater readability for me when I inevitably come back to extend it), and that led to some playing around with call, apply, and a prototype.js library method, Function#wrap, that puts this sort of casting and scope-alteration to use to enable metaprogramming. I sensed I was skirting the edge of a real revelation about how functional programming meets OOP in languages like javascript. But that’s for another post. Right now, I’d like to bite off more than I can chew on a different topic: how straightforward it appears to be to implement some RSpec-like Behavior-Driven Development specification-testing magic on javascript objects using Function#wrap.

(more…)

More fun with linked lists: implementing each and sort

Thursday, July 31st, 2008

A little while ago I posted about my indulgent use of a linked list in a Ruby on Rails project I’m working on. The list allowed me to define the linked objects’ methods in the recursive case analysis style I’ve learned from programming in Scheme. I wrote that I was surprised that this decision hasn’t felt unnatural and has proven fairly easy to build upon. Each object in the list is also the head of the remainder of list (from that object to the end), so you can call methods on that single object and all the recursion necessary to find the information you want is abstracted behind the object’s interface.

Today I got a few minutes to play around with an idea I had while on vacation: implementing an each method on the grade range class so that I can include the Enumerable module, in case I wanted to use Ruby’s rich set of functional structures on the list. It turns out to be a piece of cake!

(more…)