Skip to content

Commit 4a60d85

Browse files
committed
Merge pull request coffeescript-cookbook#34 from pickhardt/master
Added a zip function, a randomInt function, and a type function
2 parents 8f92af2 + b6b6ee7 commit 4a60d85

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

authors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The following people are totally rad and awesome because they have contributed r
1717
* Jason Giedymin *[email protected]*
1818
* Phil Cohen *[email protected]*
1919
* João Moreno *coffeecb @joaomoreno .com*
20+
* Jeff Pickhardt *pickhardt (at) gmail (dot) com*
2021
* ...You! What are you waiting for? Check out the [contributing](/contributing) section and get cracking!
2122

2223
# Developers

chapters/arrays/zip-function.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
layout: recipe
3+
title: Python-like zip function
4+
chapter: Arrays
5+
---
6+
## Problem
7+
8+
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.
9+
10+
## Solution
11+
12+
Use the following CoffeeScript code:
13+
14+
{% highlight coffeescript %}
15+
# Usage: zip(arr1, arr2, arr3, ...)
16+
zip = () ->
17+
lengthArray = (arr.length for arr in arguments)
18+
length = Math.max.apply(Math, lengthArray)
19+
argumentLength = arguments.length
20+
results = []
21+
for i in [0...length]
22+
semiResult = []
23+
for arr in arguments
24+
semiResult.push arr[i]
25+
results.push semiResult
26+
return results
27+
28+
zip([0, 1, 2, 3], [0, -1, -2, -3])
29+
# => [[0, 0], [1, -1], [2, -2], [3, -3]]
30+
{% endhighlight %}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
layout: recipe
3+
title: A CoffeeScript type function
4+
chapter: Classes and Objects
5+
---
6+
## Problem
7+
8+
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.)
9+
10+
## Solution
11+
12+
Use the following function:
13+
14+
{% highlight coffeescript %}
15+
type = (obj) ->
16+
if obj == undefined or obj == null
17+
return String obj
18+
classToType = new Object
19+
for name in "Boolean Number String Function Array Date RegExp".split(" ")
20+
classToType["[object " + name + "]"] = name.toLowerCase()
21+
myClass = Object.prototype.toString.call obj
22+
if myClass of classToType
23+
return classToType[myClass]
24+
return "object"
25+
{% endhighlight %}
26+
27+
## Discussion
28+
29+
This function was modeled on jQuery's $.type function. (http://api.jquery.com/jQuery.type/)
30+
31+
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.
32+
33+
{% highlight coffeescript %}
34+
myArray?.push? myValue
35+
{% endhighlight %}

chapters/math/random-integer.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
layout: recipe
3+
title: A random integer function
4+
chapter: Math
5+
---
6+
## Problem
7+
8+
You'd like to get a random integer between two integers, inclusive.
9+
10+
## Solution
11+
12+
Use the following function.
13+
14+
{% highlight coffeescript %}
15+
randomInt = (lower, upper=0) ->
16+
start = Math.random()
17+
if not lower?
18+
[lower, upper] = [0, lower]
19+
if lower > upper
20+
[lower, upper] = [upper, lower]
21+
return Math.floor(start * (upper - lower + 1) + lower)
22+
23+
(randomInt(1) for i in [0...10])
24+
# => [0,1,1,0,0,0,1,1,1,0]
25+
26+
(randomInt(1, 10) for i in [0...10])
27+
# => [7,3,9,1,8,5,4,10,10,8]
28+
29+
## Discussion
30+
31+
Questions?

0 commit comments

Comments
 (0)