CRC-32: Difference between revisions
→{{header|JavaScript}}: Tidied.
Alextretyak (talk | contribs) m (→{{header|11l}}) |
(→{{header|JavaScript}}: Tidied.) |
||
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 ?
)(x >>> 1)
)
);▼
return (▼
foldl(▼
(a, c) => (a >>> 8) ^ table[▼
(a ^ c.charCodeAt(0)) & 255▼
],▼
-1,▼
chars(str)▼
) ^ -1▼
);
return chars(str).reduce(
};
//
// main :: IO ()
▲ const main = () =>
▲ showHex(
▲ crc32('The quick brown fox jumps over the lazy dog')
▲ );
// --------------------- GENERIC ---------------------
// chars :: String -> [Char]
const chars = s =>
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo =
}, (_, i) => m + i)
) : enumFromTo_(m)(n);
//
const
// The identity function.
// iterate :: (a -> a) -> a -> Gen [a]
function*
let v =
}
// 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 =
// The first n elements of a list,
xs.constructor.constructor.name !== 'GeneratorFunction' ? (▼
// string of characters, or stream.
xs => 'GeneratorFunction' !== xs
xs.slice(0, n)
) : [].concat.apply([], Array.from({
Line 1,069 ⟶ 1,080:
})();</lang>
{{Out}}
<pre>
=={{header|Jsish}}==
|