Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Comma-quibbling/JavaScript/comma-quibbling-1.js
Normal file
13
Task/Comma-quibbling/JavaScript/comma-quibbling-1.js
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function quibble(words) {
|
||||
return "{" +
|
||||
words.slice(0, words.length-1).join(",") +
|
||||
(words.length > 1 ? " and " : "") +
|
||||
(words[words.length-1] || '') +
|
||||
"}";
|
||||
}
|
||||
|
||||
[[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]].forEach(
|
||||
function(s) {
|
||||
console.log(quibble(s));
|
||||
}
|
||||
);
|
||||
141
Task/Comma-quibbling/JavaScript/comma-quibbling-2.js
Normal file
141
Task/Comma-quibbling/JavaScript/comma-quibbling-2.js
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// ----------------- COMMA QUIBBLING -----------------
|
||||
|
||||
// quibble :: [String] -> String
|
||||
const quibble = xs =>
|
||||
1 < xs.length ? (
|
||||
intercalate(' and ')(
|
||||
ap([
|
||||
compose(
|
||||
intercalate(', '),
|
||||
reverse,
|
||||
tail
|
||||
),
|
||||
head
|
||||
])([reverse(xs)])
|
||||
)
|
||||
) : concat(xs);
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () =>
|
||||
unlines(
|
||||
map(compose(x => '{' + x + '}', quibble))(
|
||||
append([
|
||||
[],
|
||||
["ABC"],
|
||||
["ABC", "DEF"],
|
||||
["ABC", "DEF", "G", "H"]
|
||||
])(
|
||||
map(words)([
|
||||
"One two three four",
|
||||
"Me myself I",
|
||||
"Jack Jill",
|
||||
"Loner"
|
||||
])
|
||||
)
|
||||
));
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// ap (<*>) :: [(a -> b)] -> [a] -> [b]
|
||||
const ap = fs =>
|
||||
// The sequential application of each of a list
|
||||
// of functions to each of a list of values.
|
||||
// apList([x => 2 * x, x => 20 + x])([1, 2, 3])
|
||||
// -> [2, 4, 6, 21, 22, 23]
|
||||
xs => fs.flatMap(f => xs.map(f));
|
||||
|
||||
|
||||
// append (++) :: [a] -> [a] -> [a]
|
||||
const append = xs =>
|
||||
// A list defined by the
|
||||
// concatenation of two others.
|
||||
ys => xs.concat(ys);
|
||||
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
// A function defined by the right-to-left
|
||||
// composition of all the functions in fs.
|
||||
fs.reduce(
|
||||
(f, g) => x => f(g(x)),
|
||||
x => x
|
||||
);
|
||||
|
||||
|
||||
// concat :: [[a]] -> [a]
|
||||
// concat :: [String] -> String
|
||||
const concat = xs => (
|
||||
ys => 0 < ys.length ? (
|
||||
ys.every(Array.isArray) ? (
|
||||
[]
|
||||
) : ''
|
||||
).concat(...ys) : ys
|
||||
)(xs);
|
||||
|
||||
|
||||
// head :: [a] -> a
|
||||
const head = xs => (
|
||||
ys => ys.length ? (
|
||||
ys[0]
|
||||
) : undefined
|
||||
)(list(xs));
|
||||
|
||||
|
||||
// intercalate :: String -> [String] -> String
|
||||
const intercalate = s =>
|
||||
// The concatenation of xs
|
||||
// interspersed with copies of s.
|
||||
xs => xs.join(s);
|
||||
|
||||
|
||||
// list :: StringOrArrayLike b => b -> [a]
|
||||
const list = xs =>
|
||||
// xs itself, if it is an Array,
|
||||
// or an Array derived from xs.
|
||||
Array.isArray(xs) ? (
|
||||
xs
|
||||
) : Array.from(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);
|
||||
|
||||
|
||||
// reverse :: [a] -> [a]
|
||||
const reverse = xs =>
|
||||
'string' !== typeof xs ? (
|
||||
xs.slice(0).reverse()
|
||||
) : xs.split('').reverse().join('');
|
||||
|
||||
|
||||
// tail :: [a] -> [a]
|
||||
const tail = xs =>
|
||||
// A new list consisting of all
|
||||
// items of xs except the first.
|
||||
xs.slice(1);
|
||||
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs =>
|
||||
// A single string formed by the intercalation
|
||||
// of a list of strings with the newline character.
|
||||
xs.join('\n');
|
||||
|
||||
|
||||
// words :: String -> [String]
|
||||
const words = s =>
|
||||
// List of space-delimited sub-strings.
|
||||
s.split(/\s+/);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
9
Task/Comma-quibbling/JavaScript/comma-quibbling-3.js
Normal file
9
Task/Comma-quibbling/JavaScript/comma-quibbling-3.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function quibble(words) {
|
||||
var words2 = words.join()
|
||||
|
||||
var words3 = [...words2].reverse().join('');
|
||||
var res = words3.replace(",", " dna ");
|
||||
var words4 = [...res].reverse().join('');
|
||||
|
||||
return '{'+words4+'}';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue