Elm Bloom filter implementation using Murmur3. It may not be the fastest implementation, but it is simple and easy to use. This blog post with rules of thumb for choosing m and k might be helpful.
Use it as follows:
import Bloom exposing (empty, add, test)
-- create an empty filter with m elements and k hashes
emptyFilter = empty 1000 4
-- add elements to the filter
filter =
List.foldr
add
emptyFilter
["foo", "bar", "baz", ... ]
-- check if elements are recognized by the filter
test "bar" filter == True
test "barr" filter == False
{ set : Array Basics.Int
, m : Basics.Int
, k : Basics.Int
}
The Filter struct holds an array containing the actual filter, but also the values for m and k (for simplicity).
empty : Basics.Int -> Basics.Int -> Filter
Creates an empty Filter, containing m elements and using k hashes.
import Bloom
Bloom.empty 10 3 |> Array.toList
-- {m=10, k=3, set=[0,0,0,0,0,0,0,0,0,0]}
add : String -> Filter -> Filter
Adds elements to an existing Filter.
import Bloom exposing (add, empty)
t = List.foldr add (empty 20 3) ["foo", "bar", "baz"]
test : String -> Filter -> Basics.Bool
Tests if a filter contains an element. By its probalistic nature this function may yield false positive results.
import Bloom exposing (add, empty)
-- create filter t
test "foo" t == True
test "fou" t == False
decoder : Json.Decode.Decoder Filter
JSON decoder for a Filter
encoder : Filter -> Json.Encode.Value
Encodes a filter into a JSON value.