immutable_lru

Types

A dictionary of keys and values that follows the least recently updated cache replacement policy.

Any type can be used for the keys and values of a dict, but all the keys must be of the same type and all the values must be of the same type.

There is no guarantee of ordering of the collection.

pub opaque type LruCache(k, v)

Functions

pub fn clear(cache: LruCache(a, b)) -> LruCache(a, b)

Clear all entries in the cache.

The max of the cache is reused.

Examples

 let c =
   new(10)
   |> set("1", "a")
   |> set("2", "b")
   |> set("3", "c")

 let is_mem = has(c, "3")
 // is_mem == True

 let c = clear(c)

 let is_mem = has(c, "3")
 // is_mem == False
pub fn get(
  cache: LruCache(a, b),
  key: a,
) -> Result(#(LruCache(a, b), b), Nil)

Retrieve a value from the cache as a Result

Returns a tuple to allow for further reads from the updated cache

Examples

new()
 let c =
   new(10)
   |> set("a", 1)
   |> set("b", 2)
   |> set("c", 3)
 let #(c, val) = {
   case get(c, "a") {
     Ok(pairs) -> pairs
     Error(_) -> panic as "whoops"
   }
 }
 let t = val == 1
 // t == True
pub fn get_exn(c: LruCache(a, b), key: a) -> #(LruCache(a, b), b)

Retrieve a value from the cache or panic

Returns a tuple to allow for further reads from the updated cache

Examples

new()
 let c =
   new(10)
   |> set("a", 1)
   |> set("b", 2)
   |> set("c", 3)
 let #(c, val) = get_exn(c, "a")
 let t = val == 1
 // t == True
pub fn has(cache: LruCache(a, b), key: a) -> Bool

Check for membership in the cache

Examples

new()
|> set("a", 1)
|> set("b", 2)
|> has("b")
// -> True
pub fn new(max: Int) -> LruCache(a, b)

Creates a new empty cache that will have at most 2n entries and will be immutably updated with the LRU policy.

 let c =
   new(10)
   |> set("a", 1)
   |> set("b", 2)
   |> set("c", 3)
 let #(c, val) = {
   case get(c, "a") {
     Ok(pairs) -> pairs
     Error(_) -> panic as "whoops"
   }
 }
 let t = val == 1
 // t == True
pub fn set(
  cache: LruCache(a, b),
  key: a,
  value: b,
) -> LruCache(a, b)

Add an entry into the cache

Examples

new()
 let c =
   new(10)
   |> set("a", 1)
   |> set("b", 2)
   |> set("c", 3)
 let #(c, val) = get_exn(c, "a")
 let t = val == 1
 // t == True
Search Document