4

I've used List.exists( ), i woudl like to know if there is an equivalent for Db.

If i have a function f(e) : bool, i would like to know if there is at least one element e, with f(e) -> true. Currently i use the Db.intmap_fold_range( ), but it iterates over all the Db, whereas it would be better for performances to stop at the first return true.

trecouvr
  • 743
  • 5
  • 7

1 Answers1

3

I suppose your DB is an IntMap if you can use Db.intmap_fold_range.

This is a sample code :

// Testing function
f(key, val) = key == 42

// Check existence in "mymap"
res = IntMap.exists(f, /mymap)

If you look at the source code, you can see that it is implemented for folding only on the necessary elements :) (File: stdlib/core/map/map.opa)

akoprowski
  • 3,297
  • 1
  • 18
  • 26
Fred
  • 1,092
  • 5
  • 9
  • Hi, don't you think IntMap.exists(f, /mymap) will create a new map from the Db before to perform ? I've heard that using Map functions on Db is unperformant because a new map is created each time (it's for this reason we use Db.intmap_fold_range( )) – trecouvr Jul 14 '11 at 10:29