Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
10
Task/Thue-Morse/JavaScript/thue-morse-1.js
Normal file
10
Task/Thue-Morse/JavaScript/thue-morse-1.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(function(steps) {
|
||||
'use strict';
|
||||
var i, tmp, s1 = '0', s2 = '1';
|
||||
for (i = 0; i < steps; i++) {
|
||||
tmp = s1;
|
||||
s1 += s2;
|
||||
s2 += tmp;
|
||||
}
|
||||
console.log(s1);
|
||||
})(6);
|
||||
90
Task/Thue-Morse/JavaScript/thue-morse-2.js
Normal file
90
Task/Thue-Morse/JavaScript/thue-morse-2.js
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// thueMorsePrefixes :: () -> [[Int]]
|
||||
const thueMorsePrefixes = () =>
|
||||
iterate(
|
||||
ap(append)(
|
||||
map(x => 1 - x)
|
||||
)
|
||||
)([0]);
|
||||
|
||||
// ----------------------- TEST -----------------------
|
||||
const main = () =>
|
||||
// Fifth iteration.
|
||||
// 2 ^ 5 = 32 terms of the Thue-Morse sequence.
|
||||
showList(
|
||||
index(thueMorsePrefixes())(
|
||||
5
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS -----------------
|
||||
|
||||
// ap :: (a -> b -> c) -> (a -> b) -> a -> c
|
||||
const ap = f =>
|
||||
// Applicative instance for functions.
|
||||
// f(x) applied to g(x).
|
||||
g => x => f(x)(
|
||||
g(x)
|
||||
);
|
||||
|
||||
|
||||
// append (++) :: [a] -> [a] -> [a]
|
||||
// append (++) :: String -> String -> String
|
||||
const append = xs =>
|
||||
// A list or string composed by
|
||||
// the concatenation of two others.
|
||||
ys => xs.concat(ys);
|
||||
|
||||
|
||||
// index (!!) :: Generator (Int, a) -> Int -> Maybe a
|
||||
const index = xs =>
|
||||
i => (take(i)(xs), xs.next().value);
|
||||
|
||||
|
||||
// iterate :: (a -> a) -> a -> Gen [a]
|
||||
const iterate = f =>
|
||||
function*(x) {
|
||||
let v = x;
|
||||
while (true) {
|
||||
yield(v);
|
||||
v = f(v);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// map :: (a -> b) -> [a] -> [b]
|
||||
const map = f =>
|
||||
// The list obtained by applying f
|
||||
// to each element of xs.
|
||||
// (The image of xs under f).
|
||||
xs => xs.map(f);
|
||||
|
||||
|
||||
// showList :: [a] -> String
|
||||
const showList = xs =>
|
||||
'[' + xs.map(x => x.toString())
|
||||
.join(',')
|
||||
.replace(/[\"]/g, '') + ']';
|
||||
|
||||
|
||||
// take :: Int -> [a] -> [a]
|
||||
// take :: Int -> String -> String
|
||||
const take = n =>
|
||||
// The first n elements of a list,
|
||||
// string of characters, or stream.
|
||||
xs => 'GeneratorFunction' !== xs
|
||||
.constructor.constructor.name ? (
|
||||
xs.slice(0, n)
|
||||
) : [].concat.apply([], Array.from({
|
||||
length: n
|
||||
}, () => {
|
||||
const x = xs.next();
|
||||
return x.done ? [] : [x.value];
|
||||
}));
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue