[Haskell-cafe] Bloom Filter

Donald Bruce Stewart dons at cse.unsw.edu.au
Mon Apr 30 22:45:29 EDT 2007


ajb:
> Quoting tom <tom at almostobsolete.net>:
> This looks cool:
> 
>     bytes2int = foldr ((. (256 *)) . (+)) 0 . (map toInteger)
> 
> but I'm not smart enough to parse it.  This is both more readable and
> shorter:
> 
>     bytes2int = foldr (\x r -> r*256 + fromInteger x) 0
> 
> Integer log2's are probably better done using integers only, or at least
> abstracted out into a separate function.

Reminds me of this code from Data.Binary:

    unroll :: Integer -> [Word8]
    unroll = unfoldr step
      where
        step 0 = Nothing
        step i = Just (fromIntegral i, i `shiftR` 8)

    roll :: [Word8] -> Integer
    roll   = foldr unstep 0
      where
        unstep b a = a `shiftL` 8 .|. fromIntegral b

Which is a bit stream-fusion inspired, I must admit.

-- Don


More information about the Haskell-Cafe mailing list