Size

stdtraits~ Size

Source:

Trait to de­term­ine the size of a con­tainer.

// Im­ple­ment­ing size
class Foo {
  con­structor(len) {
    this.len = len || 42;
  }

  [Size.sym­bol]() {
    re­turn this.len;
  }
}

// Al­tern­at­ively
Size.impl(Foo, (v) => v.len);

// De­term­ine the size
size(new Map()); // => 0
size("foobar"); // => 6
size({foo: 42, bar: 5}); // => 2
empty(new Set()); // => true
empty([1,2]); // => false

Im­ple­men­ted at least for Ob­ject, String, Ar­ray, Map, Set.

In­ter­face

In­voc­a­tion takes the form (c: Con­tainer) => i: In­teger

Laws

  • i >= 0
  • i !== null && i !== un­defined.
  • Must be ef­fi­cient to ex­ecute. No IO, avoid bad al­gorithmic com­plex­it­ies.

See: size See: empty