Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
29
Task/Letter-frequency/JavaScript/letter-frequency-1.js
Normal file
29
Task/Letter-frequency/JavaScript/letter-frequency-1.js
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
(function(txt) {
|
||||
|
||||
var cs = txt.split(''),
|
||||
i = cs.length,
|
||||
dct = {},
|
||||
c = '',
|
||||
keys;
|
||||
|
||||
while (i--) {
|
||||
c = cs[i];
|
||||
dct[c] = (dct[c] || 0) + 1;
|
||||
}
|
||||
|
||||
keys = Object.keys(dct);
|
||||
keys.sort();
|
||||
return keys.map(function (c) { return [c, dct[c]]; });
|
||||
|
||||
})("Not all that Mrs. Bennet, however, with the assistance of her five\
|
||||
daughters, could ask on the subject, was sufficient to draw from her\
|
||||
husband any satisfactory description of Mr. Bingley. They attacked him\
|
||||
in various ways--with barefaced questions, ingenious suppositions, and\
|
||||
distant surmises; but he eluded the skill of them all, and they were at\
|
||||
last obliged to accept the second-hand intelligence of their neighbour,\
|
||||
Lady Lucas. Her report was highly favourable. Sir William had been\
|
||||
delighted with him. He was quite young, wonderfully handsome, extremely\
|
||||
agreeable, and, to crown the whole, he meant to be at the next assembly\
|
||||
with a large party. Nothing could be more delightful! To be fond of\
|
||||
dancing was a certain step towards falling in love; and very lively\
|
||||
hopes of Mr. Bingley's heart were entertained.");
|
||||
5
Task/Letter-frequency/JavaScript/letter-frequency-2.js
Normal file
5
Task/Letter-frequency/JavaScript/letter-frequency-2.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
[[" ", 121], ["!", 1], ["'", 1], [",", 13], ["-", 3], [".", 9], [";", 2],
|
||||
["B", 3], ["H", 2], ["L", 2], ["M", 3], ["N", 2], ["S", 1], ["T", 2], ["W", 1],
|
||||
["a", 53], ["b", 13], ["c", 17], ["d", 29], ["e", 82], ["f", 17], ["g", 16], ["h", 36],
|
||||
["i", 44], ["j", 1], ["k", 3], ["l", 34], ["m", 11], ["n", 41], ["o", 40], ["p", 8],
|
||||
["q", 2], ["r", 35], ["s", 39], ["t", 55], ["u", 20], ["v", 7], ["w", 17], ["x", 2], ["y", 16]]
|
||||
129
Task/Letter-frequency/JavaScript/letter-frequency-3.js
Normal file
129
Task/Letter-frequency/JavaScript/letter-frequency-3.js
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
|
||||
// charCounts :: String -> [(Char, Int)]
|
||||
const charCounts = s =>
|
||||
sortBy(flip(comparing(snd)))(
|
||||
Object.entries(
|
||||
chars(s).reduce(
|
||||
(a, c) => (
|
||||
a[c] = 1 + (a[c] || 0),
|
||||
a
|
||||
), {}
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// ----------------------- TEST -----------------------
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
either(msg => msg)(
|
||||
compose(
|
||||
unlines,
|
||||
map(JSON.stringify),
|
||||
charCounts
|
||||
)
|
||||
)(readFileLR('~/Code/charCount/miserables.txt'));
|
||||
|
||||
|
||||
// -----------------GENERIC FUNCTIONS -----------------
|
||||
|
||||
// Left :: a -> Either a b
|
||||
const Left = x => ({
|
||||
type: 'Either',
|
||||
Left: x
|
||||
});
|
||||
|
||||
|
||||
// Right :: b -> Either a b
|
||||
const Right = x => ({
|
||||
type: 'Either',
|
||||
Right: x
|
||||
});
|
||||
|
||||
|
||||
// chars :: String -> [Char]
|
||||
const chars = s =>
|
||||
s.split('');
|
||||
|
||||
|
||||
// comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
const comparing = f =>
|
||||
x => y => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : (a > b ? 1 : 0);
|
||||
};
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
fs.reduce(
|
||||
(f, g) => x => f(g(x)),
|
||||
x => x
|
||||
);
|
||||
|
||||
// either :: (a -> c) -> (b -> c) -> Either a b -> c
|
||||
const either = fl =>
|
||||
fr => e => 'Either' === e.type ? (
|
||||
undefined !== e.Left ? (
|
||||
fl(e.Left)
|
||||
) : fr(e.Right)
|
||||
) : undefined;
|
||||
|
||||
|
||||
// flip :: (a -> b -> c) -> b -> a -> c
|
||||
const flip = f =>
|
||||
1 < f.length ? (
|
||||
(a, b) => f(b, a)
|
||||
) : (x => y => f(y)(x));
|
||||
|
||||
|
||||
// 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 => (
|
||||
Array.isArray(xs) ? (
|
||||
xs
|
||||
) : xs.split('')
|
||||
).map(f);
|
||||
|
||||
|
||||
// readFileLR :: FilePath -> Either String IO String
|
||||
const readFileLR = fp => {
|
||||
const
|
||||
e = $(),
|
||||
ns = $.NSString
|
||||
.stringWithContentsOfFileEncodingError(
|
||||
$(fp).stringByStandardizingPath,
|
||||
$.NSUTF8StringEncoding,
|
||||
e
|
||||
);
|
||||
return ns.isNil() ? (
|
||||
Left(ObjC.unwrap(e.localizedDescription))
|
||||
) : Right(ObjC.unwrap(ns));
|
||||
};
|
||||
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => tpl[1];
|
||||
|
||||
|
||||
// sortBy :: (a -> a -> Ordering) -> [a] -> [a]
|
||||
const sortBy = f =>
|
||||
xs => xs.slice()
|
||||
.sort((a, b) => f(a)(b));
|
||||
|
||||
|
||||
// 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');
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
19
Task/Letter-frequency/JavaScript/letter-frequency-4.js
Normal file
19
Task/Letter-frequency/JavaScript/letter-frequency-4.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
const letterfreq = text => [...text]
|
||||
.reduce(
|
||||
(a, c) => (a[c] = (a[c] || 0) + 1, a),
|
||||
{}
|
||||
);
|
||||
|
||||
return JSON.stringify(
|
||||
letterfreq(
|
||||
`remember, remember, the fifth of november
|
||||
gunpowder treason and plot
|
||||
I see no reason why gunpowder treason
|
||||
should ever be forgot`
|
||||
),
|
||||
null, 2
|
||||
);
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue