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';
 
// ------------------- HUMBLE NUMBERS -------------------
 
// humbles :: () -> [Int]
Line 1,835:
};
 
// ------------------------ TEST ------------------------
// main :: IO ()
const main = () => {
Line 1,863:
 
 
// ----------------- GENERIC FUNCTIONS- -----------------
 
// chunksOf :: Int -> [a] -> [[a]]
const chunksOf = n => xs =>
xs => enumFromThenTo(0)(n)(
xs.length - 1
).reduce(
Line 1,873:
[]
);
 
 
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
const compose = (...fs) =>
x// =>A fs.reduceRight((a,function f)defined =>by f(a),the x);right-to-left
// composition of all the functions in fs.
constfs.reduce(
(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 => 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));
};
};
 
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m => n =>
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];
})() : [];
returnconst v = go(list(xs));
return 'string' === typeof xs ? (
v.map(x => x.join(''))
) : v;
};
 
// justifyRight :: Int -> Char -> String -> String
const justifyRight = n => cFiller => s =>
n// >The string s.length, preceded by enough ?padding (with
// the character c) to reach the string length n.
s.padStart(n, cFiller)
c => s => lngn => Maths.min(length(xs), length? (ys)),
s.padStart(n, cFillerc)
) : s;
 
Line 1,958 ⟶ 1,978:
) : Infinity;
 
 
// map :: (a -> b) -> [a] -> [b]
const// maplist =:: fStringOrArrayLike b => xsb =-> [a]
const list = (Array.isArray(xs) ? (=>
// xs itself, if it is an Array,
// or an Array derived from xs.
Array.isArray(xs) ? (
xs
) : xsArray.splitfrom('')).map(fxs);
 
 
// 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 => xs => ys => {
// 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 => asys => take(lng)(xs),{
bsconst n = takeMath.min(lnglength(xs), length(ys));
return Array.fromInfinity > n ? ({
length: lng (([as, bs]) => Array.from({
}, (_, i) => f(as[i])( length: n
bs }, (_, i) => f(as[i])(
)); bs[i]
)))([xs, ys].map(
};
compose(take(n), list)
))
) : zipWithGen(f)(xs)(ys);
};
 
// MAIN ---
9,655

edits