HardPro challengePythonJavaScriptTypeScript

Option (Maybe) Monad

FunctionalPatternsTypeScript

Implement Some(value) and None — a minimal Option monad — with these
methods on each:

  • .map(fn) — apply fn to the value if present, return a new Option.
  • .flatMap(fn) — like map but fn returns an Option.
  • .getOrElse(default) — return the value or default for None.
  • .isSome() / .isNone() — type predicates.

solve(ops) chains operations and returns the final .getOrElse result.

Sample tests

Test #1None with no ops → default 42
Input: [null,[],42]
Output: 42
Test #2Some(5).map(x => x*2).getOrElse(0) = 10
Input: [5,[{"fn":"x => x * 2","type":"map"}],0]
Output: 10
Test #3None.map(...).getOrElse(99) = 99
Input: [null,[{"fn":"x => x * 2","type":"map"}],99]
Output: 99
Test #4Chain two maps: (10+5)*3 = 45
Input: [10,[{"fn":"x => x + 5","type":"map"},{"fn":"x => x * 3","type":"map"}],0]
Output: 45
Test #5Some(0) getOrElse(42) → 0 (not 42)
Input: [0,[],42]
Output: 0