Skip to content

Commit

Permalink
Create readme.t.js.
Browse files Browse the repository at this point in the history
Start tracking th examples in the `README.md` in a proof test.
  • Loading branch information
flatheadmill committed Nov 3, 2015
1 parent 92772d5 commit b20b03b
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 2 deletions.
71 changes: 69 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,9 @@ var reduce = cadence(function (async) {
var index = 0
async(function (sum) {
// ^^^ initial argument or result of last iteration.
return sum + index++

if [ index == 5 ] return [ async.break, sum ]
else return sum + index++

})(0)
// ^ initial argument.
Expand Down Expand Up @@ -1411,4 +1413,69 @@ squares(function (error, array) {

### Loops Nested in Cadences

All of the above loop examples can be used in any **step** in any **cadence**.
All of the above loop examples can be used in any **step** in any **cadence**,
not just the function body.

Loops can contain loops.

```
var multiply = cadence(function (async, matrix) {
async(function () {
var i = 0, j = 0
async(function () {
if (i == matrix.length) return [ async.break ]
async(function () {
if (j == matrix[i].length) return [ async.break ]
echo(matrix[i][j] * 5, async())
}, function (value) {
matrix[i][j] = value
j++
})()
}, function () {
j = 0
i++
})()
}, function () {
return [ matrix ]
})
})
multiply([[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]], function (error, matrix) {
if (error) throw error
deepEqual(matrix, [[ 5, 10, 15 ], [ 20, 25, 30 ], [ 35, 40, 45 ]])
})
```

The above is a contrived example that multiples each element in an array of
arrays by five. It is much easier to express using `async.map`.

```
var multiply = cadence(function (async, matrix) {
async.map(function (array) {
async.map(function (value) {
echo(value * 5, async())
})(array)
})(matrix)
})
multiply([[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]], function (error, matrix) {
if (error) throw error
deepEqual(matrix, [[ 5, 10, 15 ], [ 20, 25, 30 ], [ 35, 40, 45 ]])
})
```

### Loop Labels


### What's Missing?

* Quafliied catch blocks.
* Details of catch, I believe.
* Finalizers; how to use them, where to use them.

### Accompanying Documents and Apendexes

* The Rules of Cadence
* Design Decisions
* Thinking in Cadence ~ Asynchronous Functions, Error-First Callbacks,
Asynchrnous Returns, Asynchronous Stacks, Turning the Corner.
58 changes: 58 additions & 0 deletions t/cadence/readme.t.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require('proof')(2, require('../..')(prove))

function prove (async, assert) {
var cadence = require('../..')

function echo (value, callback) {
setImmediate(callback, null, value)
}

var deepEqual = assert.deepEqual

async(function () {
var next = async()

var multiply = cadence(function (async, matrix) {
async(function () {
var i = 0, j = 0
async(function () {
if (i == matrix.length) return [ async.break ]
async(function () {
if (j == matrix[i].length) return [ async.break ]
echo(matrix[i][j] * 5, async())
}, function (value) {
matrix[i][j] = value
j++
})()
}, function () {
j = 0
i++
})()
}, function () {
return [ matrix ]
})
})

multiply([[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]], function (error, matrix) {
if (error) throw error
deepEqual(matrix, [[ 5, 10, 15 ], [ 20, 25, 30 ], [ 35, 40, 45 ]])
next()
})
}, function () {
var next = async()

var multiply = cadence(function (async, matrix) {
async.map(function (array) {
async.map(function (value) {
echo(value * 5, async())
})(array)
})(matrix)
})

multiply([[ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]], function (error, matrix) {
if (error) throw error
deepEqual(matrix, [[ 5, 10, 15 ], [ 20, 25, 30 ], [ 35, 40, 45 ]])
next()
})
})
}

0 comments on commit b20b03b

Please sign in to comment.