some.md

@include "au"
@include "meta"
@include "list"


function Some(i,     most) {
  isa(Object(i))
  i.most= either(most,256) # keep up to `most` number of items
  has(i,"cache")             # i.cache holds the kept value
  i.n=0
  i.sorted=0
}

function SomeAdd(i,v,    added,len) {
  i.n++
  i.sorted = 0
  len = length(i.cache)
  if (len < i.most) {  # the cache is not full, add something
    push(i.cache,v)
    added = 1
  } else if (rand() < len/i.n) {  # else, sometimes, add "v"
    i.cache[ int(len*rand()) + 1 ] = v
    added = 1
  }
  return added
}
function SomeMedian(i,  m,n) {
  n= i.sorted ? length(i.cache) : asort(i.cache)
  i.sorted=1
  return median(i.cache)
}