Leonardo numbers: Difference between revisions

→‎JS ES6: Tidied generator version, updated output.
(Added Rust solution)
(→‎JS ES6: Tidied generator version, updated output.)
Line 1,020:
while (true) {
yield x;
[x, y] = [y, xdelta + yx + deltay];
}
}
 
// FORMATTING ----------------------- TEST ------------------------
// main :: IO ()
const main = () => {
Line 1,030 ⟶ 1,031:
fibonacci = leo(0, 1, 0);
 
console.logreturn unlines([
unlines(['First 25 Leonardo numbers:',
'First indentWrapped(take(25 Leonardo numbers:')(leonardo)),
twoLines(take(25, leonardo))'',
'First 25 Fibonacci 'numbers:',
'First indentWrapped(take(25 Fibonacci numbers:',)(fibonacci))
]);
twoLines(take(25, fibonacci))
])
);
};
 
// GENERIC FUNCTIONS -------------------- FORMATTING ---------------------
 
// indentWrapped :: [Int] -> String
// FORMATTING -----------------------------------------
const twoLinesindentWrapped = xs =>
 
// twoLines :: [Int] -> Stringunlines(
map(x => '\t' + x.join(','))(
const twoLines = xs =>
unlines chunksOf(map16)(
ns => '\t' + showJSON map(str)(nsxs),
chunksOf(16, xs )
));
]);
 
// GENERIC FUNCTIONS ----------------------------------
 
// ----------------- GENERIC FUNCTIONS -----------------
 
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = (n, xs) =>
xs => enumFromThenTo(0, )(n - 1, xs.length - 1)(
xs.reduce(length - 1
(a, i) => a.concatreduce([xs.slice(i, i + n)]),
(a, i) => a.concat([xs.slice(i, (n + i))]),
[]
);
 
// enumFromThenTo :: Int -> Int -> Int -> [Int]
const enumFromThenTo = (x1, x2, y) => {
const dx2 => x2y -=> x1;{
return Array.from({ const d = x2 - x1;
length:return MathArray.floorfrom(y - x2) / d + 2{
}, (_, i) => x1 + length: Math.floor(dy *- ix2)); / d + 2
}, (_, i) => x1 + (d * i));
};
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// The list obtained by applying f
// to each element of xs.
// (The image of xs under f).
xs => [...xs].map(f);
 
// showJSONstr :: a -> String
const showJSONstr = x => JSON.stringify(x);
x.toString();
 
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = (n, xs) =>
// The first n elements of a list,
'GeneratorFunction' !== xs.constructor.constructor.name ? (
// string of characters, or stream.
xs => 'GeneratorFunction' !== xs
'GeneratorFunction' !== xs.constructor.constructor.name ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
Line 1,096 ⟶ 1,106:
{{Out}}
<pre>First 25 Leonardo numbers:
[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973]
[3193,5167,8361,13529,21891,35421,57313,92735,150049]
 
First 25 Fibonacci numbers:
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
[987,1597,2584,4181,6765,10946,17711,28657,46368]</pre>
 
=={{header|jq}}==
9,655

edits