-
Notifications
You must be signed in to change notification settings - Fork 0
Use Cases
It is useful to have operators be functions so that you can use them in higher order functions like tlc.map and tlc.filter. Translucent has a tlc.op object that has functional versions of operators.
var plus = tlc.op["+"];
plus(1, 2); // 3Partial application (currying):
var plus1 = plus(1);
plus1(2); // 3Mapping over arrays:
var a = [1, 2, 3, 4];
tlc.map(plus1, a); // [2, 3, 4, 5]Say you have an array of DOM elements, xs. If you want to get their offset widths you can do the following.
tlc.map(tlc.prop("offsetWidth"), xs); // [132, 493, ...]If you want to sort them from thinnest to widest:
tlc.sortBy(tlc.prop("offsetWidth"), xs);If you want to get only elements that have children:
tlc.filter(tlc.propCall("hasChildNodes", []), xs);Say you have an array of DOM elements, xs. If you want to attach an event listener called clickHandler to all of them you could do the following.
tlc.map(tlc.propCall("addEventListener", ["click", clickHandler]), xs);Say you have a function that determines if numbers are even:
function even(x) {
return x % 2 === 0;
}Then you can use function composition to easily (and readably) create a function to determine if numbers are odd:
var odd = tlc.compose(tlc.not, even);Maybe (aka Option) is a type representing that a value may or may not exist. This is usually what null is used for but that's not a good solution because:
- It's not in the function's type signature
- There aren't library functions that can help you work with values that could be
null
Maybe solves both of these problems. Here's an example.
var keys = ["one", "two", "three"];
var db = {"one": 1, "three": 3};
var getValue = tlc.flip(tlc.lookup);
var result = tlc.map(getValue(db), keys); // [Just 1, Nothing, Just 3]
var values = tlc.catMaybes(maybeValues); // [1, 3]We know that tlc.lookup might not return a value because it returns a Maybe object. We can also easily filter out all the Nothings (nulls) with tlc.catMabyes. In fact tlc.mapMaybe was made for exactly this use case, so we can shorten it even further by combining the last two lines.
var values = tlc.mapMaybe(getValue(db), keys); // [1, 3]There are more useful functions for working with Maybes in the [relevant API documentation](API Maybe).
Typeclasses let you use Translucent's features on your own data structures. For example, you might create a Tree data structure like this:
function Tree(value, left, right) {
this.value = value;
this.left = left;
this.right = right;
}If you wanted to be able to map a function over all the nodes of your tree you could write a treeMap function to do that:
function treeMap(f, tree) {
var left = (tree.left === undefined) ? undefined : treeMap(tree.left);
var right = (tree.right === undefined) ? undefined : treeMap(tree.right);
return new Tree(f(tree.value), left, right);
}But what if you wanted to be able to use tlc.map to map over your Tree? For example, what if another Translucent library function needed to be able to map over your Tree. It wouldn't know to use treeMap; it would just try to tlc.map your tree and it would fail. This is what typeclasses are for. To make your tree a member of the Functor typeclass (which means it can be mapped over), you just tell Translucent how to map over it:
tlc.addInstance(Tree, {map: treeMap});Now your Tree is a first class citizen and you can use Translucent's map function on it:
var tree = new Tree(1, new Tree(2), new Tree(3));
tlc.map(plus1, tree); // Tree 2 (Tree 3) (Tree 4)See the [typeclass guide](Guide Typeclasses) for more detailed information.