The bit-stream package

[ Tags: benchmark, bsd3, library, unclassified ] [ Propose Tags ]

[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.1, 0.1.0.2
Dependencies base (>=4.5 && <5), bit-stream, ghc-prim, transformers, vector [details]
License BSD3
Copyright 2017 Bodigrim
Author Bodigrim
Maintainer andrew.lelechenko@gmail.com
Home page https://github.com/Bodigrim/bit-stream#readme
Source repository head: git clone https://github.com/Bodigrim/bit-stream
Uploaded Tue Aug 8 18:03:22 UTC 2017 by Bodigrim
Distributions NixOS:0.1.0.2, Stackage:0.1.0.2
Executables find-foo
Downloads 158 total (22 in the last 30 days)
Rating 0.0 (0 ratings) [clear rating]
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-08-08 [all 1 reports]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for bit-stream-0.1.0.2

[back to package description]

bit-stream

Lazy, infinite, compact stream of Bool with O(1) indexing. Most useful for memoization of predicates.

Example 1

Consider following predicate:

isOdd :: Word -> Bool
isOdd 0 = False
isOdd n = not (isOdd (n - 1))

Its computation is expensive, so we'd like to memoize its values into BitStream using tabulate and access this stream via index instead of recalculation of isOdd:

isOddBS :: BitStream
isOddBS = tabulate isOdd

isOdd' :: Word -> Bool
isOdd' = index isOddBS

We can do even better by replacing part of recursive calls to isOdd by indexing memoized values. Write isOddF such that isOdd = fix isOddF:

isOddF :: (Word -> Bool) -> Word -> Bool
isOddF _ 0 = False
isOddF f n = not (f (n - 1))

and use tabulateFix:

isOddBS :: BitStream
isOddBS = tabulateFix isOddF

isOdd' :: Word -> Bool
isOdd' = index isOddBS

Example 2

Define a predicate, which checks whether its argument is a prime number by trial division.

isPrime :: Word -> Bool
isPrime n
  | n < 2     = False
  | n < 4     = True
  | even n    = False
  | otherwise = and [ n `rem` d /= 0 | d <- [3, 5 .. ceiling (sqrt (fromIntegral n))], isPrime d]

Convert it to unfixed form:

isPrimeF :: (Word -> Bool) -> Word -> Bool
isPrimeF f n
  | n < 2     = False
  | n < 4     = True
  | even n    = False
  | otherwise = and [ n `rem` d /= 0 | d <- [3, 5 .. ceiling (sqrt (fromIntegral n))], f d]

Create its memoized version for faster evaluation:

isPrimeBS :: BitStream
isPrimeBS = tabulateFix isPrimeF

isPrime' :: Word -> Bool
isPrime' = index isPrimeBS