diff --git a/authors.md b/authors.md index d008717..f48ecbd 100644 --- a/authors.md +++ b/authors.md @@ -17,6 +17,7 @@ The following people are totally rad and awesome because they have contributed r * Jason Giedymin *jasong@apache.org* * Phil Cohen *github@phlippers.net* * João Moreno *coffeecb @joaomoreno .com* +* Jeff Pickhardt *pickhardt (at) gmail (dot) com* * ...You! What are you waiting for? Check out the [contributing](/contributing) section and get cracking! # Developers diff --git a/chapters/arrays/zip-function.md b/chapters/arrays/zip-function.md new file mode 100644 index 0000000..80d8006 --- /dev/null +++ b/chapters/arrays/zip-function.md @@ -0,0 +1,30 @@ +--- +layout: recipe +title: Python-like zip function +chapter: Arrays +--- +## Problem + +You want to zip together multiple arrays into an array of arrays, similar to Python's zip function. Python's zip function returns an array of tuples, where each tuple contains the i-th element from each of the argument arrays. + +## Solution + +Use the following CoffeeScript code: + +{% highlight coffeescript %} +# Usage: zip(arr1, arr2, arr3, ...) +zip = () -> + lengthArray = (arr.length for arr in arguments) + length = Math.max.apply(Math, lengthArray) + argumentLength = arguments.length + results = [] + for i in [0...length] + semiResult = [] + for arr in arguments + semiResult.push arr[i] + results.push semiResult + return results + +zip([0, 1, 2, 3], [0, -1, -2, -3]) +# => [[0, 0], [1, -1], [2, -2], [3, -3]] +{% endhighlight %} diff --git a/chapters/classes_and_objects/type-function.md b/chapters/classes_and_objects/type-function.md new file mode 100644 index 0000000..6acf6f5 --- /dev/null +++ b/chapters/classes_and_objects/type-function.md @@ -0,0 +1,35 @@ +--- +layout: recipe +title: A CoffeeScript type function +chapter: Classes and Objects +--- +## Problem + +You'd like to know the type of a function without using typeof. (See http://javascript.crockford.com/remedial.html for more information on why typeof is pretty inferior.) + +## Solution + +Use the following function: + +{% highlight coffeescript %} +type = (obj) -> + if obj == undefined or obj == null + return String obj + classToType = new Object + for name in "Boolean Number String Function Array Date RegExp".split(" ") + classToType["[object " + name + "]"] = name.toLowerCase() + myClass = Object.prototype.toString.call obj + if myClass of classToType + return classToType[myClass] + return "object" +{% endhighlight %} + +## Discussion + +This function was modeled on jQuery's $.type function. (http://api.jquery.com/jQuery.type/) + +Note that, as an alternative to type checking, you can often use duck typing and the existential operator together to eliminating the need to examine an object's type, in certain cases. For example, here is exception-free code that pushes an element to an array, if myArray is in fact an array (or array-like, with a push function), and does nothing otherwise. + +{% highlight coffeescript %} +myArray?.push? myValue +{% endhighlight %} diff --git a/chapters/math/random-integer.md b/chapters/math/random-integer.md new file mode 100644 index 0000000..bcc645c --- /dev/null +++ b/chapters/math/random-integer.md @@ -0,0 +1,31 @@ +--- +layout: recipe +title: A random integer function +chapter: Math +--- +## Problem + +You'd like to get a random integer between two integers, inclusive. + +## Solution + +Use the following function. + +{% highlight coffeescript %} +randomInt = (lower, upper=0) -> + start = Math.random() + if not lower? + [lower, upper] = [0, lower] + if lower > upper + [lower, upper] = [upper, lower] + return Math.floor(start * (upper - lower + 1) + lower) + +(randomInt(1) for i in [0...10]) +# => [0,1,1,0,0,0,1,1,1,0] + +(randomInt(1, 10) for i in [0...10]) +# => [7,3,9,1,8,5,4,10,10,8] + +## Discussion + +Questions?