CRC-32: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) m (→{{header|11l}}) |
(→{{header|JavaScript}}: Tidied.) |
||
Line 984: | Line 984: | ||
'use strict'; |
'use strict'; |
||
// --------------------- CRC-32 ---------------------- |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// crc32 :: String -> Int |
// crc32 :: String -> Int |
||
const crc32 = str => { |
const crc32 = str => { |
||
// table :: [Int] |
// table :: [Int] |
||
const table = map( |
const table = enumFromTo(0)(255).map( |
||
n => take(9 |
n => take(9)( |
||
iterate( |
iterate( |
||
x => ( |
x => ( |
||
x & 1 ? |
x & 1 ? ( |
||
z => 0xEDB88320 ^ z |
|||
) : identity |
|||
) |
)(x >>> 1) |
||
) |
)(n) |
||
)[8] |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
); |
); |
||
return chars(str).reduce( |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
}; |
}; |
||
// |
// ---------------------- TEST ----------------------- |
||
// main :: IO () |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// --------------------- GENERIC --------------------- |
|||
// chars :: String -> [Char] |
// chars :: String -> [Char] |
||
const chars = s => |
const chars = s => |
||
⚫ | |||
// enumFromTo :: Int -> Int -> [Int] |
// enumFromTo :: Int -> Int -> [Int] |
||
const enumFromTo = |
const enumFromTo = m => |
||
n => !isNaN(m) ? ( |
|||
Array.from({ |
|||
length: 1 + n - m |
|||
}, (_, i) => m + i) |
|||
) : enumFromTo_(m)(n); |
|||
// foldl :: (a -> b -> a) -> a -> [b] -> a |
|||
const foldl = (f, a, xs) => xs.reduce(f, a); |
|||
// |
// identity :: a -> a |
||
const |
const identity = x => |
||
// The identity function. |
|||
⚫ | |||
// iterate :: (a -> a) -> a -> Gen [a] |
// iterate :: (a -> a) -> a -> Gen [a] |
||
const iterate = f => |
|||
// An infinite list of repeated |
|||
// applications of f to x. |
|||
function* (x) { |
|||
v = |
let v = x; |
||
while (true) { |
|||
⚫ | |||
} |
|||
⚫ | |||
} |
|||
⚫ | |||
// map :: (a -> b) -> [a] -> [b] |
|||
const map = (f, xs) => xs.map(f); |
|||
// showHex :: Int -> String |
// showHex :: Int -> String |
||
const showHex = n => |
const showHex = n => |
||
// Hexadecimal string for a given integer. |
|||
⚫ | |||
'0x' + n.toString(16); |
|||
// take :: Int -> [a] -> [a] |
// take :: Int -> [a] -> [a] |
||
// take :: Int -> String -> String |
// take :: Int -> String -> String |
||
const take = |
const take = n => |
||
// The first n elements of a list, |
|||
⚫ | |||
// string of characters, or stream. |
|||
xs => 'GeneratorFunction' !== xs |
|||
⚫ | |||
xs.slice(0, n) |
xs.slice(0, n) |
||
) : [].concat.apply([], Array.from({ |
) : [].concat.apply([], Array.from({ |
||
Line 1,069: | Line 1,080: | ||
})();</lang> |
})();</lang> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre>0x414fa339</pre> |
||
=={{header|Jsish}}== |
=={{header|Jsish}}== |