Tagged union, discriminated union, variant or sum type.. for JavaScript.
Example
JavaScript
var T = ; // with underscore.jsvar nums = 3 5 6;{ return x%2 === 0; }{ return x > 10; } var optional_x = TOption;var optional_y = TOption; optional_x; // output: I got 6 var y = optional_y;console; // 10
Coffee Script
T = require 'tagval' # with underscorejsnums = 3 5 6is_even = x-> x%2 is 0is_gt_10 = x-> x > 10 optional_x = TOptionfromValue _optional_y = TOptionfromValue _ optional_xmatch Some: x-> consolelog "I got #{x}" None: -> consolelog "nothing"# output: I got 6 y = optional_ygetOrElse 10consolelog y # 10
TagVal.Option
class
Option
is a class which expresses "something x"(Some) or "nothing"(None).
Example
// Finding a number from collection.var optionalX = ; // assume it returns `Option`var DEFAULT_X = 30; // Try to set the found number or 30 by default.var x = optionalX;
API
method | detail |
---|---|
TagVal.Some(v) |
creates a new Option object tagged Some with v in the value. |
TagVal.None() |
creates a new Option object tagged None . |
TagVal.Option.fromValue(v) |
creates Some(v) if v is neither undefined nor null . |
TagVal.optionFrom(v) |
alias of TagVal.Option.fromValue(v) |
TagVal.fromBool(v) |
If v is truthy, it returns Some(true) |
opt.map(f) |
Some(x) to Some(f(x)) / nothing to None() |
opt.getOrElse(x) |
Some(y) to y / None() to x |
opt.getOrElseF(f) |
Some(y) to y / None() to f() |
opt.toArray() |
Some(y) to [y] / None() to [] |
opt.toStatus(msg) |
Some(v) to Success(v) / None() to Failure(msg) |
opt.toValue() |
Some(v) to v / None() to undefined |
opt.equal(y) |
If opt=Some(x) and y=Some(y) then x === y , and true if both are None() , otherwise false . |
opt.mapEqual(y, f) |
If opt=Some(x) and y=Some(y) then f(x, y) , and true if both are None() , otherwise false . |
opt.match |
see Matchable |
opt.when |
see Matchable |
opt.toString |
see Matchable |
TagVal.Status
class
Status object is a class that express "success with result"(Success) or "failure with message"(Failure).
API
method | detail |
---|---|
TagVal.Status.trying(f) |
Success(f()) or Failure(e) if caught an exception e . |
TagVal.withTry |
alias of TagVal.Status.trying |
stat.getOrThrow() |
Success(v) to v / throw msg if Failure(msg) |
stat.toOption() |
Success(v) to Some(v) / Failure(msg) to None() |
stat.match |
see Matchable |
stat.when |
see Matchable |
stat.toString |
see Matchable |
stat.equal |
see Matchable |
TagVal.Matchable
class
TagVal's basic concept is regarding {tag: String, val: Value}
as minimum tagged-value interface. Matchable
has actually only this two fields, and they are initialized simply:
var value = "some value"var tv = "Tag" value; // I often express as "Tag(value)" console; // Tagconsole; // value
In addition, Matchable
has a few, generic methods below. You can inherit it. Option
and Status
are subclasses of Matchable
.
API
method | detail |
---|---|
tv.match(table[, default_fun]) |
table[T](v) if table[T] exists. Next, default_fun(v) if default_fun exists. Otherwise undefined . |
tv.when(table) |
table[T](v) if table[T] exists, otherwise returns object itself. |
tv.toString() |
Tag( value.toString() ) |
tv.equal(y) |
tv.valEqual(y) if same tag, otherwise false |
tv.valEqual(y) |
tv.val === y.val |
TagVal.match(tv)(table) |
match method for arbitrary { tag: Tag, value: Value } |
Matchable#when
example:
In JavaScript:
x_stat = ;
In CoffeeScript:
x_stat = when Failure: -> when Failure: -> when Failure: -> TagValFailure "All tries failed."