Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,14 @@
(function () {
return rng(1, Math.sqrt(100)).map(function (x) {
return x * x;
});
// rng(1, 20) --> [1..20]
function rng(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
})();

View file

@ -0,0 +1 @@
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

View file

@ -0,0 +1,9 @@
Array.apply(null, { length: 100 })
.map((v, i) => i + 1)
.forEach(door => {
var sqrt = Math.sqrt(door);
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
});

View file

@ -0,0 +1,9 @@
// Array comprehension style
[ for (i of Array.apply(null, { length: 100 })) i ].forEach((_, i) => {
var door = i + 1
var sqrt = Math.sqrt(door);
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
});

View file

@ -1,6 +1,41 @@
for (var door = 1; door <= 100; door++) {
var sqrt = Math.sqrt(door);
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
(function () {
return chain(
// 100 passes ...
rng(0, 99).reduce(function (a, _, i) {
return a.slice(0, i).concat(
a.slice(i).map(function (v, j) {
return (i + j + 1) % (i + 1) ? v : {
door: v.door,
open: !v.open
};
})
)
},
// 100 closed doors at start
Array.apply(null, Array(100)).map(function (x, i) {
return {
open: false,
door: i + 1
};
})),
// Filtering by chained function
function (door) {
return door.open ? [door] : [];
}
)
// Monadic bind (chain) for lists
function chain(xs, f) {
return [].concat.apply([], xs.map(f));
}
}
// range(1, 20) --> [1..20]
function rng(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
})();

View file

@ -1,9 +1 @@
Array.apply(null, { length: 100 })
.map(function(v, i) { return i + 1; })
.forEach(function(door) {
var sqrt = Math.sqrt(door);
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
});
[{"door":1, "open":true}, {"door":4, "open":true}, {"door":9, "open":true}, {"door":16, "open":true}, {"door":25, "open":true}, {"door":36, "open":true}, {"door":49, "open":true}, {"door":64, "open":true}, {"door":81, "open":true}, {"door":100, "open":true}]

View file

@ -1,3 +1,6 @@
for(var door=1;i<10/*Math.sqrt(100)*/;i++){
console.log("Door %d is open",i*i);
for (var door = 1; door <= 100; door++) {
var sqrt = Math.sqrt(door);
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
}

View file

@ -1,9 +1,3 @@
Array.apply(null, { length: 100 })
.map((v, i) => i + 1)
.forEach(door => {
var sqrt = Math.sqrt(door);
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
});
for(var door=1;i<10/*Math.sqrt(100)*/;i++){
console.log("Door %d is open",i*i);
}

View file

@ -1,9 +1,9 @@
// Array comprehension style
[ for (i of Array.apply(null, { length: 100 })) i ].forEach((_, i) => {
var door = i + 1
var sqrt = Math.sqrt(door);
Array.apply(null, { length: 100 })
.map(function(v, i) { return i + 1; })
.forEach(function(door) {
var sqrt = Math.sqrt(door);
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
});
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
});

View file

@ -0,0 +1,34 @@
(function () {
return chain(
rng(1, 100),
function (x) {
var root = Math.sqrt(x);
return root === Math.floor(root) ? inject(x) : fail();
}
);
/*************************************************************/
// monadic Bind/chain for lists
function chain(xs, f) {
return [].concat.apply([], xs.map(f));
}
// monadic Return/inject for lists
function inject(x) { return [x]; }
// monadic Fail for lists
function fail() { return []; }
// rng(1, 20) --> [1..20]
function rng(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
})();

View file

@ -0,0 +1 @@
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

View file

@ -0,0 +1,18 @@
(function () {
return rng(1, 100).filter(
function (x) {
var root = Math.sqrt(x);
return root === Math.floor(root);
}
);
// rng(1, 20) --> [1..20]
function rng(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
})();