class Maybe { value: int; static wrap(v: int) { return new Maybe { value = v } } // map() applies fn on the unwrapped value. fn returns a Maybe. map(fn: (x: int) => Maybe) { return this.value == null ? Maybe.wrap(null) : fn(this.value); } }
Maybe.wrap(5) .map(x => Maybe.wrap(x + 1)) .map(x => Maybe.wrap(x * 2))