Humble numbers: Difference between revisions
→{{header|JavaScript}}: updated primitives
(→{{header|C sharp|C#}}: added direct generatio logarithms version) |
(→{{header|JavaScript}}: updated primitives) |
||
Line 1,818:
'use strict';
// ------------------
// humbles :: () -> [Int]
Line 1,835:
};
// -----------------------
// main :: IO ()
const main = () => {
Line 1,863:
// ----------------- GENERIC FUNCTIONS
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = n
xs => enumFromThenTo(0)(n)(
xs.length - 1
).reduce(
Line 1,873:
[]
);
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
// composition of all the functions in fs.
(f, g) => x => f(g(x)),
x => x
// concat :: [[a]] -> [a]
Line 1,887 ⟶ 1,894:
return unit.concat.apply(unit, xs);
})() : [];
// enumFromThenTo :: Int -> Int -> Int -> [Int]
const enumFromThenTo = x1 =>
}, (_, i) => x1 + (d * i));
▲ };
};▼
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m
n => Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// fTable :: String -> (a -> String) -> (b -> String)
Line 1,917 ⟶ 1,928:
).join('\n');
};
// fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
Line 1,928 ⟶ 1,940:
};
// group :: Eq a => [a] -> [[a]]
const group = xs => {
// A list of lists, each containing only equal elements,
// such that the concatenation of these lists is xs.
const go = xs =>
0 < xs.length ? (() => {
Line 1,939 ⟶ 1,954:
) : [xs];
})() : [];
return 'string' === typeof xs ? (
v.map(x => x.join(''))
) : v;
};
// justifyRight :: Int -> Char -> String -> String
const justifyRight = n
// the character c) to reach the string length n.
s.padStart(n, cFiller)▼
) : s;
Line 1,958 ⟶ 1,978:
) : Infinity;
// map :: (a -> b) -> [a] -> [b]▼
const list =
// xs itself, if it is an Array,
// or an Array derived from xs.
Array.isArray(xs) ? (
xs
) :
▲ // 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
Line 1,994 ⟶ 2,025:
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = f =>
// Use of `take` and `length` here allows zipping with non-finite lists
▲ const
// i.e. generators like cycle, repeat, iterate.
▲ lng = Math.min(length(xs), length(ys)),
xs =>
return
)))([xs, ys].map(
▲ };
compose(take(n), list)
))
) : zipWithGen(f)(xs)(ys);
};
// MAIN ---
|