Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed all {:lang="javascript"} to {:lang="js"} #93

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 19 additions & 19 deletions manuscript/markdown/Ideas/drunken.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ In [Tortoises, Hares, and Teleporting Turtles](#tortoises), we looked at the "To
1. The mechanism for iterating over a list.
2. The algorithm for detecting a loop in a list.

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
var LinkedList = (function() {

Expand Down Expand Up @@ -48,7 +48,7 @@ We then went on to discuss how to use [functional iterators](#functional-iterato

For example, here is a function that takes an array and returns a functional iterator over the array:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
function ArrayIterator (array) {
var index = 0;
Expand All @@ -64,7 +64,7 @@ Iterators allow us to write (or refactor) functions to operate on iterators inst

Now we'll refactor the Tortoise and Hare to use iterators instead of directly operate on linked lists. We'll add an `.iterator()` method to linked lists, and we'll rewrite our loop detector function to take an "iterable" instead of a list:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
LinkedList.prototype.iterator = function() {
var list = this;
Expand All @@ -77,8 +77,8 @@ LinkedList.prototype.iterator = function() {

function tortoiseAndHareLoopDetector (iterable) {
var tortoise = iterable.iterator(),
hare = iterable.iterator(),
tortoiseValue,
hare = iterable.iterator(),
tortoiseValue,
hareValue;
while (((tortoiseValue = tortoise()) != null) && ((hare(), hareValue = hare()) != null)) {
if (tortoiseValue === hareValue) {
Expand Down Expand Up @@ -121,7 +121,7 @@ Therefore, if we think of this as detecting whether the chequer revisits a squar

In essence, we're given an object that has a `.iterator()` method. That gives us an iterator, and each time we call the iterator, we get a direction. Here it is:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
var DIRECTION_TO_DELTA = {
N: [1, 0],
Expand All @@ -134,7 +134,7 @@ var Game = (function () {
function Game (size) {
var i,
j;

this.size = size
? Math.floor(Math.random() * 8) + 8
: size ;
Expand All @@ -146,16 +146,16 @@ var Game = (function () {
}
}
this.initialPosition = [
2 + Math.floor(Math.random() * (this.size - 4)),
2 + Math.floor(Math.random() * (this.size - 4)),
2 + Math.floor(Math.random() * (this.size - 4))
];
return this;
};

Game.prototype.contains = function (position) {
return position[0] >= 0 && position[0] < this.size && position[1] >= 0 && position[1] < this.size;
};

Game.prototype.iterator = function () {
var position = [this.initialPosition[0], this.initialPosition[1]];
return function () {
Expand All @@ -171,9 +171,9 @@ var Game = (function () {
}
}.bind(this);
};

return Game;

})();

var i = new Game().iterator();
Expand All @@ -198,7 +198,7 @@ Our goal is to transform the iteration of directions into an iteration that the

We'll use a `statefulMap`:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
function statefulMap (iter, binaryFn, initial) {
var state = initial;
Expand All @@ -222,7 +222,7 @@ function statefulMap (iter, binaryFn, initial) {
Here's how we use `statefulMap`:


{:lang="javascript"}
{:lang="js"}
~~~~~~~~
function RelativeIterator (directionIterator) {
return statefulMap(directionIterator, function (relativePositionStr, directionStr) {
Expand Down Expand Up @@ -255,7 +255,7 @@ i();

We're almost there! The refactored `tortoiseAndHareLoopDetector` expects an "iterable," an object that implements the `.iterator()` method. Let's refactor `RelativeIterable` to accept a game and return an iterable instead of accepting an iteration and returning an iteration:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
function RelativeIterable (game) {
return {
Expand Down Expand Up @@ -290,7 +290,7 @@ So. We can take a `Game` instance and produce an iterable that iterates over reg

Our refactored `tortoiseAndHareLoopDetector` takes an iterable and detects this for us. Writing a detector function is trivial:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
function terminates (game) {
return !tortoiseAndHareLoopDetector(RelativeIterable(game));
Expand All @@ -316,7 +316,7 @@ Can we also refactor the "Teleporting Turtle" algorithm to take an iterable? If

We start with:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
function teleportingTurtleLoopDetector (list) {
var i, rabbit, speed, turtle;
Expand Down Expand Up @@ -351,7 +351,7 @@ teleportingTurtleLoopDetector(list);

And refactor it to become:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
function teleportingTurtleLoopDetector (iterable) {
var i, rabbit, rabbitValue, speed, turtleValue;
Expand Down Expand Up @@ -387,7 +387,7 @@ teleportingTurtleLoopDetector(list);

Now we can plug it into our termination detector:

{:lang="javascript"}
{:lang="js"}
~~~~~~~~
function terminates (game) {
return !teleportingTurtleLoopDetector(RelativeIterable(game));
Expand Down