Leonardo numbers: Difference between revisions

Content added Content deleted
(Added Rust solution)
(→‎JS ES6: Tidied generator version, updated output.)
Line 1,020: Line 1,020:
while (true) {
while (true) {
yield x;
yield x;
[x, y] = [y, x + y + delta];
[x, y] = [y, delta + x + y];
}
}
}
}


// ----------------------- TEST ------------------------
// main :: IO ()
// main :: IO ()
const main = () => {
const main = () => {
Line 1,030: Line 1,031:
fibonacci = leo(0, 1, 0);
fibonacci = leo(0, 1, 0);


console.log(
return unlines([
unlines([
'First 25 Leonardo numbers:',
'First 25 Leonardo numbers:',
indentWrapped(take(25)(leonardo)),
twoLines(take(25, leonardo)),
'',
'',
'First 25 Fibonacci numbers:',
'First 25 Fibonacci numbers:',
indentWrapped(take(25)(fibonacci))
]);
twoLines(take(25, fibonacci))
])
);
};
};


// -------------------- FORMATTING ---------------------


// indentWrapped :: [Int] -> String
// FORMATTING -----------------------------------------
const indentWrapped = xs =>

// twoLines :: [Int] -> String
unlines(
map(x => '\t' + x.join(','))(
const twoLines = xs =>
unlines(map(
chunksOf(16)(
ns => '\t' + showJSON(ns),
map(str)(xs)
chunksOf(16, xs)
)
));
)
);

// GENERIC FUNCTIONS ----------------------------------


// ----------------- GENERIC FUNCTIONS -----------------


// chunksOf :: Int -> [a] -> [[a]]
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = (n, xs) =>
const chunksOf = n =>
enumFromThenTo(0, n - 1, xs.length - 1)
xs => enumFromThenTo(0)(n)(
.reduce(
xs.length - 1
(a, i) => a.concat([xs.slice(i, i + n)]),
).reduce(
(a, i) => a.concat([xs.slice(i, (n + i))]),
[]
[]
);
);


// enumFromThenTo :: Int -> Int -> Int -> [Int]
// enumFromThenTo :: Int -> Int -> Int -> [Int]
const enumFromThenTo = (x1, x2, y) => {
const enumFromThenTo = x1 =>
const d = x2 - x1;
x2 => y => {
return Array.from({
const d = x2 - x1;
length: Math.floor(y - x2) / d + 2
return Array.from({
}, (_, i) => x1 + (d * i));
length: Math.floor(y - x2) / d + 2
}, (_, i) => x1 + (d * i));
};
};


// map :: (a -> b) -> [a] -> [b]
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
const map = f =>
// The list obtained by applying f
// to each element of xs.
// (The image of xs under f).
xs => [...xs].map(f);


// showJSON :: a -> String
// str :: a -> String
const showJSON = x => JSON.stringify(x);
const str = x =>
x.toString();


// take :: Int -> [a] -> [a]
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
// take :: Int -> String -> String
const take = (n, xs) =>
const take = n =>
// The first n elements of a list,
'GeneratorFunction' !== xs.constructor.constructor.name ? (
// string of characters, or stream.
xs => 'GeneratorFunction' !== xs
.constructor.constructor.name ? (
xs.slice(0, n)
xs.slice(0, n)
) : [].concat.apply([], Array.from({
) : [].concat.apply([], Array.from({
Line 1,096: Line 1,106:
{{Out}}
{{Out}}
<pre>First 25 Leonardo numbers:
<pre>First 25 Leonardo numbers:
[1,1,3,5,9,15,25,41,67,109,177,287,465,753,1219,1973]
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]
3193,5167,8361,13529,21891,35421,57313,92735,150049


First 25 Fibonacci numbers:
First 25 Fibonacci numbers:
[0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
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>
987,1597,2584,4181,6765,10946,17711,28657,46368</pre>


=={{header|jq}}==
=={{header|jq}}==