CF Summit notes - Open Yourself to Closures, Adam Tuttle

October 29, 2013

(I've seen Adam give several talks at different conferences. He always does a great job of delivering the info with both ColdFusion examples as well as info on how to apply the concepts to other platforms if need be. This talk was no exception. )


Open Yourself to Closures - Adam Tuttle

(Adam was thinking ahead! He put his preso on a website so people in the back of the conference room can still follow along, even if the projection screens are too far away! nice touch!)

http://tinurl.com/donate-adam
Extra Life charity - shameless plug to donate to Adam's cause

closure vs a callback
a closure is not a callback
a callback is not a closure
but they're very similar

important part - once you understand callbacks, it's easy to understand closures

jQuery async callback

that's a callback
do something and when you're done, run this function that i'm passing you

node.js async callback to read a file


CF has had callbacks since CF 5

in CF10 they added anonymous functions
so you can declare a callback in line, you don't have to declare it FIRST

ArrayEach( [1,2,3] ).function(e){ ... }

runs that function once for EACH thing in the array

JavaScript has closures because Scheme has closures. Scheme inspired JavaScript.
continuation-passing style

can use a closure as a callback to solve some problems that other languages have
in CF, it's mostly to make code cleaner
CF doesn't have as many problems where closures are NECESSARY to solve

so what exactly is a closure?
a special feature of ALL functions
all UDFs are closures (in a language that SUPPORTS closures)
a closure is a function that's pass as a argument to another function
-that always remembers its CONTEXT

closure ex:
map -- functional programming concept
takes every element in the array that's mapped, executes the callback on each of them
so the result is a NEW array the with results of the map() function
map() is the most basic functional programming concept
CF didn't have a map function until CF11 (map and reduce are coming in that version, but it may have a different name, we don't know for sure yet)

if we take that same concept and apply closures to it, we can generalize it
closures require "scope chain" --
they REQUIRE you to make UNscoped variable references

...i CANT write arguments.str here. i HAVE to write an UNscoped variable for it to work

return name; -- uses scope chain
return arguments.name; -- does NOT use scope chain

CF10 closure functions (don't NEED to use closures with them but thats how they're labeled in the documentation)
arrayEach(), structEach()
arrayFilter()
etc

curry-
one of the medium/hard concepts for closures
we have a function, sometimes we want to hard-code those arguments in another function

some libraries for functional programming in CF
Sesame by Mark Mandel

Underscore.js has a wrapper called Underscore.cfc
not complete yet but pretty darn close
80 methods for functional programming

"if you're passing a function as an argument", that's functional programming"
that's jQuery in a nutshell too
if you're doing jQuery you're doing functional programming

wrapping up:
closures are just functions
all functions are closures
callbacks are not the same thing
if you're not using variables outside of the function ITSELF, you can't really consider it a "closure"
remember the scope chain

http://fusiongrokker.com/p/closures/
for the slide deck
its also on github