September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,12 +1,19 @@
|
|||
function nreps(s, n) {
|
||||
var o = '';
|
||||
if (n < 1) return o;
|
||||
while (n > 1) {
|
||||
if (n & 1) o += s;
|
||||
n >>= 1;
|
||||
s += s;
|
||||
}
|
||||
return o + s;
|
||||
}
|
||||
(() => {
|
||||
'use strict';
|
||||
|
||||
nreps('ha', 50000);
|
||||
// replicate :: Int -> String -> String
|
||||
const replicate = (n, s) => {
|
||||
let v = [s],
|
||||
o = [];
|
||||
if (n < 1) return o;
|
||||
while (n > 1) {
|
||||
if (n & 1) o = o + v;
|
||||
n >>= 1;
|
||||
v = v + v;
|
||||
}
|
||||
return o.concat(v);
|
||||
};
|
||||
|
||||
|
||||
return replicate(5000, "ha")
|
||||
})();
|
||||
|
|
|
|||
27
Task/Repeat-a-string/JavaScript/repeat-a-string-4.js
Normal file
27
Task/Repeat-a-string/JavaScript/repeat-a-string-4.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// repeat :: Int -> String -> String
|
||||
const repeat = (n, s) =>
|
||||
concat(replicate(n, s));
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ------------------------------------------------------
|
||||
|
||||
// concat :: [[a]] -> [a] | [String] -> String
|
||||
const concat = xs =>
|
||||
xs.length > 0 ? (() => {
|
||||
const unit = typeof xs[0] === 'string' ? '' : [];
|
||||
return unit.concat.apply(unit, xs);
|
||||
})() : [];
|
||||
|
||||
// replicate :: Int -> a -> [a]
|
||||
const replicate = (n, x) =>
|
||||
Array.from({
|
||||
length: n
|
||||
}, () => x);
|
||||
|
||||
|
||||
// TEST -------------------------------------------------------------------
|
||||
return repeat(5, 'ha');
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue