Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions chapters/arrays/zip-function.md
Original file line number Diff line number Diff line change
@@ -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 %}
35 changes: 35 additions & 0 deletions chapters/classes_and_objects/type-function.md
Original file line number Diff line number Diff line change
@@ -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 %}
31 changes: 31 additions & 0 deletions chapters/math/random-integer.md
Original file line number Diff line number Diff line change
@@ -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?