Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
9
Task/Leonardo-numbers/JavaScript/leonardo-numbers-1.js
Normal file
9
Task/Leonardo-numbers/JavaScript/leonardo-numbers-1.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
const leoNum = (c, l0 = 1, l1 = 1, add = 1) =>
|
||||
new Array(c).fill(add).reduce(
|
||||
(p, c, i) => i > 1 ? (
|
||||
p.push(p[i - 1] + p[i - 2] + c) && p
|
||||
) : p, [l0, l1]
|
||||
);
|
||||
|
||||
console.log(leoNum(25));
|
||||
console.log(leoNum(25, 0, 1, 0));
|
||||
92
Task/Leonardo-numbers/JavaScript/leonardo-numbers-2.js
Normal file
92
Task/Leonardo-numbers/JavaScript/leonardo-numbers-2.js
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// leo :: Int -> Int -> Int -> Generator [Int]
|
||||
function* leo(L0, L1, delta) {
|
||||
let [x, y] = [L0, L1];
|
||||
while (true) {
|
||||
yield x;
|
||||
[x, y] = [y, delta + x + y];
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------- TEST ------------------------
|
||||
// main :: IO ()
|
||||
const main = () => {
|
||||
const
|
||||
leonardo = leo(1, 1, 1),
|
||||
fibonacci = leo(0, 1, 0);
|
||||
|
||||
return unlines([
|
||||
'First 25 Leonardo numbers:',
|
||||
indentWrapped(take(25)(leonardo)),
|
||||
'',
|
||||
'First 25 Fibonacci numbers:',
|
||||
indentWrapped(take(25)(fibonacci))
|
||||
]);
|
||||
};
|
||||
|
||||
// -------------------- FORMATTING ---------------------
|
||||
|
||||
// indentWrapped :: [Int] -> String
|
||||
const indentWrapped = xs =>
|
||||
unlines(
|
||||
map(x => '\t' + x.join(','))(
|
||||
chunksOf(16)(
|
||||
map(str)(xs)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// ----------------- GENERIC FUNCTIONS -----------------
|
||||
|
||||
// chunksOf :: Int -> [a] -> [[a]]
|
||||
const chunksOf = n =>
|
||||
xs => enumFromThenTo(0)(n)(
|
||||
xs.length - 1
|
||||
).reduce(
|
||||
(a, i) => a.concat([xs.slice(i, (n + i))]),
|
||||
[]
|
||||
);
|
||||
|
||||
// enumFromThenTo :: Int -> Int -> Int -> [Int]
|
||||
const enumFromThenTo = x1 =>
|
||||
x2 => y => {
|
||||
const d = x2 - x1;
|
||||
return Array.from({
|
||||
length: Math.floor(y - x2) / d + 2
|
||||
}, (_, i) => x1 + (d * i));
|
||||
};
|
||||
|
||||
// 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);
|
||||
|
||||
// str :: a -> String
|
||||
const str = x =>
|
||||
x.toString();
|
||||
|
||||
// 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];
|
||||
}));
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue