Skip to content

Instantly share code, notes, and snippets.

@ythirion
Created December 18, 2019 15:13
Show Gist options
  • Save ythirion/529cd3c2e659f7136f4e622fa4b2c630 to your computer and use it in GitHub Desktop.
Save ythirion/529cd3c2e659f7136f4e622fa4b2c630 to your computer and use it in GitHub Desktop.
Option monad
Option<int> aValue = 2;
aValue.Map(x => x + 3); // Some(5)
Option<int> none = None;
none.Map(x => x + 3); // None
//Left -> Some, Right -> None
aValue.Match(x => x + 3, () => 0); // 5
none.Match(x => x + 3, () => 0); // 0
// Returns the Some case 'as is' -> 2 and 1 in the None case
int value = aValue.IfNone(1);
int noneValue = none.IfNone(42); // 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment