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] = [y, delta + x + y]; |
||
} |
} |
||
} |
} |
||
⚫ | |||
// main :: IO () |
// main :: IO () |
||
const main = () => { |
const main = () => { |
||
Line 1,030: | Line 1,031: | ||
fibonacci = leo(0, 1, 0); |
fibonacci = leo(0, 1, 0); |
||
return unlines([ |
|||
'First 25 Leonardo numbers:', |
|||
indentWrapped(take(25)(leonardo)), |
|||
'', |
|||
'First 25 Fibonacci numbers:', |
|||
indentWrapped(take(25)(fibonacci)) |
|||
⚫ | |||
twoLines(take(25, fibonacci)) |
|||
]) |
|||
⚫ | |||
}; |
}; |
||
⚫ | |||
// indentWrapped :: [Int] -> String |
|||
⚫ | |||
⚫ | |||
unlines( |
|||
map(x => '\t' + x.join(','))( |
|||
⚫ | |||
chunksOf(16)( |
|||
map(str)(xs) |
|||
) |
|||
) |
) |
||
⚫ | |||
⚫ | |||
// ----------------- GENERIC FUNCTIONS ----------------- |
|||
// chunksOf :: Int -> [a] -> [[a]] |
// chunksOf :: Int -> [a] -> [[a]] |
||
const chunksOf = |
const chunksOf = n => |
||
enumFromThenTo(0 |
xs => enumFromThenTo(0)(n)( |
||
. |
xs.length - 1 |
||
).reduce( |
|||
(a, i) => a.concat([xs.slice(i, (n + i))]), |
|||
[] |
[] |
||
); |
); |
||
// enumFromThenTo :: Int -> Int -> Int -> [Int] |
// enumFromThenTo :: Int -> Int -> Int -> [Int] |
||
const enumFromThenTo = |
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] |
// map :: (a -> b) -> [a] -> [b] |
||
const map = |
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 |
const str = x => |
||
x.toString(); |
|||
// take :: Int -> [a] -> [a] |
// take :: Int -> [a] -> [a] |
||
// take :: Int -> String -> String |
// take :: Int -> String -> String |
||
const take = |
const take = n => |
||
// The first n elements of a list, |
|||
⚫ | |||
// string of characters, or stream. |
|||
xs => 'GeneratorFunction' !== xs |
|||
⚫ | |||
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 |
|||
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 |
|||
987,1597,2584,4181,6765,10946,17711,28657,46368</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |