CRC-32: Difference between revisions

Line 984:
'use strict';
 
// --------------------- CRC-32 ----------------------
const main = () =>
showHex(
crc32('The quick brown fox jumps over the lazy dog')
);
 
// crc32 :: String -> Int
const crc32 = str => {
 
// table :: [Int]
const table = enumFromTo(0)(255).map(
n => take(9,)(
iterate(
x => (
x & 1 ? z => 0xEDB88320 ^ z : id(
)(x z =>>> 1),0xEDB88320 ^ z
n ) : identity
)(x >>> 1)
)[8],(n)
enumFromTo(0, 255)[8]
);
return (
foldl(
(a, c) => (a >>> 8) ^ table[
(a ^ c.charCodeAt(0)) & 255
],
-1,
chars(str)
) ^ -1
);
return chars(str).reduce(
(a, c) => (a >>> 8) ^ table[
(a ^ c.charCodeAt(0)) & 255
foldl(],
],-1
) ^ -1;
};
 
// GENERIC ABSTRACTIONS ---------------------- TEST -----------------------
// main :: IO ()
const main = () =>
showHex(
crc32('The quick brown fox jumps over the lazy dog')
);
 
// --------------------- GENERIC ---------------------
 
// chars :: String -> [Char]
const chars = s => s.split('');
ns.toStringsplit(16'');
 
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.fromn => !isNaN(m) ? ({
length: 1 + n - mArray.from({
}, (_, i) => m length: 1 + i);n - m
}, (_, i) => m + i)
) : enumFromTo_(m)(n);
 
// foldl :: (a -> b -> a) -> a -> [b] -> a
const foldl = (f, a, xs) => xs.reduce(f, a);
 
// ididentity :: a -> a
const ididentity = x => x;
// The identity function.
)x;
 
 
// iterate :: (a -> a) -> a -> Gen [a]
function*const iterate(f, x)= f {=>
let// vAn =infinite list of repeated x;
while// (true)applications {of f to x.
function* yield(vx); {
let v = f(v)x;
} while (true) {
-1,yield(v);
}
charsv = f(strv);
}
return (};
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
 
// showHex :: Int -> String
const showHex = n =>
// Hexadecimal string for a given integer.
n.toString(16);
'0x' + n.toString(16);
 
 
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = (n, xs) =>
// The first n elements of a list,
xs.constructor.constructor.name !== 'GeneratorFunction' ? (
// string of characters, or stream.
xs => 'GeneratorFunction' !== xs
xs.constructor.constructor.name !== 'GeneratorFunction' ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
Line 1,069 ⟶ 1,080:
})();</lang>
{{Out}}
<pre>414fa3390x414fa339</pre>
 
=={{header|Jsish}}==
9,655

edits