March 03, 2013
As you probably know, I am running a Kickstarter project to create Introduction to Clojure videos. The project is still going (it runs until March 14, 2013). Please support it if you want the videos.
I know that there are many great videos out there that teach the basics of Clojure. Allow me to present my selection.
Jim Slaterry is creating video walkthroughs of the Clojure Koans.
Full Disclojure was a long series of screencasts by Sean Devlin that dives deep into Clojure. There are many lessons about Clojure and functional programming, screencasts explaining how to install editors, and a few explorations of katas. Most of the material should still be current.
Brian Will created a series of videos introducing Clojure back in 2009 which explains the basics of the language. The basics have changed somewhat, but this is still a good way to get into the language.
PeepCode has a video for $12 for Clojure beginners.
Rich Hickey gave some talks a long time ago teaching Clojure. Again, a little old, but I learned a lot from them when they were new.
February 25, 2013
Paul Chiusano:
The real decrease in code size when using FP comes from there being exponentially more code reuse possible due to the compositionality of pure code.
I totally agree. John Carmack has it wrong.
February 22, 2013
If you went back to 1990 and asked a random programmer what an object was (as in OOP) what would he say? I bet he'd say something like "I don't know. I want to learn them but I don't know where to start." If you told him "Well, they're easy. Each object has a class. The class defines methods. The object encapsulates state . . .", then his eyes would glaze over. He receives a maelstrom of concepts and their relationships. In the end, he would learn nothing. It actually took a long time for OO to become the norm.
This is analagous to what is happening currently with monads. People want to learn, they get some high-concept explanation, they are unsatisfied. People try to explain. They really do. I still remember when I did not get monads, so I have a lot of sympathy.
Perhaps, like with OOP, it will take a generation to become understandable to the mainstream.
The best way I can think of to teach monads to a programmer is jQuery. You have to know jQuery to understand this example. And frankly, if you don't know jQuery, it is easier to go learn it and come back than to go read a monad tutorial.
Here's a (not-so-)secret: jQuery objects are a monad. When you do $('div')
, you get an object which "contains" all of the div
elements in the document. There are many methods on the jQuery object which modify the set of elements contained in the object and return it. That's what makes it a monad: the methods return a value of the same type.
Without the jQuery object, you have to follow (and re-follow--potentially repeating code) the logic of collections. The jQuery object controls the logic by which operations on the contained set of elements gets executed. You call a method, it changes all of the elements. If there is no element, nothing happens. If there is one element, it alone is changed. If there are many elements, they are all changed. And you don't need to know. If you call $('<div />')
, even though you are constructing an object with only one element, jQuery internally turns it into a list. jQuery is making sure you don't have to know if you have 0, 1, or many elements. And this turns out to be a useful abstraction.
This logic is simple. You write this code all the time when you are dealing with collections of objects. But you have to write this all the time, over and over. And you have to remember to write it. The jQuery object does that for you.
That is the job of the monad. It gives you a single place to express the logic for access (in this case, a kind of collection logic). Now we are going to make a monad and show how we can write this logic in only one place.
To make a monad, you actually need two things. One is typically called return
, but in OOP it's the constructor. We know that the jQuery constructor is the $
function.
The second thing you need is a way to chain methods. All you need is to return a value constructed with that constructor. Then you can chain. In Haskell, this is called bind
.
But I promised that monads got rid of repetition. If all of your jQuery methods have to contain this collection logic and return logic, how does that get rid of repetition? Well, if you did it the way it's done in Haskell, you'd get rid of the repetition.
Let's do it that way. Let's define a method on the jQuery called bind
.
jQuery.prototype.bind = function (f) {
return $(f(this.get()));
};
We construct a new jQuery object with the elements transformed by f
. So I can write:
function filter_divs(els) {
var arr = [];
var i;
for(i = 0; i < els.length; i++)
if(els[i].tagName === 'div')
arr.push(els[i]);
return arr;
}
$('.x').bind(filter_divs).addClass('hello');
Let's take it further. We can bind the function and store it in the prototype:
jQuery.prototype.bind_def = function (name, f) {
jQuery.prototype[name] = function () {
return $(f(this.get()));
};
}
Now we can call it like a method.
$.bind_def('filter_divs', filter_divs);
$('.x').filter_divs().addClass('hello');
So, now we can chain. We only have to write the logic once (in bind
or bind_def
). And we have a constructor. That's a monad.
Now, imagine other bits of logic you could build into a method chain. What happens if a method returns null
? If you call a method on it, it will throw an exception. You can capture the null check in a monad. That is called the Maybe Monad.
There are other useful monads, but they all are simply structured ways of chaining by giving controlled access to the internals of the object.
Thanks to Douglas Crockford for some of these ideas.
If you liked this essay, please check out the Clojure Gazette.
February 21, 2013
Douglas Crockford gives his version of the obligatory monad tutorial. His version is in Javascript with hopes of needed demystification.
I have to appreciate the freshness of his approach. He says a monad is just an object with certain properties (the monad laws). That's a great way to explain it in an object-oriented context. Objects are the unit of encapsulation. Monads serve the purpose of encapsulating state and allowing structured access to that state.
There is one thing that bothers me about his presentation. He uses mutable objects. I guess that is to be expected in Javascript. But mutating state in a monad seriously limits their possibilities, as mutation does in most uses.
Either way, it is an entertaining watch and a good explanation of monads. Perhaps the most down-to-earth I have seen.
February 21, 2013
I've been talking to James Ladd about this project and it looks really cool. If you don't know, Smalltalk was the language for which the term Object Oriented was coined. It has a rich history and is one of the most powerful languages ever written.
Redline is an implementation of Smalltalk for the JVM. James and his team have some big plans for Redline, but first they want to get to v1.0 with a robust, professional distribution. By running on the JVM, Redline will be able to tap into the amazing ecosystem that is growing around the JVM. That means that interop is incredibly easy between Smalltalk, Java, and Clojure.
Please check it out.
February 12, 2013
I would like to announce the launch of my Kickstarter project. It is a project to develop screencasts to introduce Clojure to programmers everywhere.
Video is a great way to learn programming. For one, you get to watch the program being developed organically, not as a static artifact (as in a book). Also, a voiceover gives a direct stream into the thought process of the programmer as he types. Finally, you can stop and replay as much as you want to learn at your own pace. Those things, combined with video's ability to engage a large part of your brain make it a great learning tool.
This blog was first created to host screencasts I made for Common Lisp, way back when. I transitioned to Clojure after watching a talk by Rich Hickey at Lisp50 in 2008. I was very impressed and have not looked back.
I have loved education ever since I taught math in the Peace Corps. I really enjoyed making those Common Lisp videos as well. I wanted to turn it into something like PeepCode, but my day job interceded and I didn't have the comfort level with Clojure that I did with Common Lisp. The videos got put on hold.
That was almost five years ago and I feel very confident with my ability to teach Clojure. Plus, video production has advanced a lot. I will be able to produce very high-quality videos much more easily than five years ago. And Kickstarter has provided a nearly risk-free way to test the market.
If this project does well, I hope to make it into a profitable business and produce more and more videos, especially videos about deeper topics. But only if the project proves itself in the very large beginner's market. Let your friends know about this great opportunity to learn Clojure. You can find the Kickstarter project here.
My ultimate goal, if the project proves out, is to provide exceptional educational materials to build a strong developer culture. Clojure Gazette shares this same goal.
One hour of video will cost $5 to the Kickstarter supporters. After that, the price will go up to $12. This is a very good value for one hour of instructional video. There is also a company sponsorship level. See the Kickstarter page for more details. The project ends March 14, 2013.
Please support the project and let other people know.
Thanks!