With fi you can ignore the language constructs and write all your conditional logic in a functional way.
Basic if statement
var sw = require'fi';var myVar = fitrue "flower puppy"ret; // we need to add .ret() to get the value of a statement// myVar is "flower puppy"
We can make it more interesting and add an else statement:
var myVar = fifalse "flower puppy"els"space pedals"ret;// myVar is "space pedals"
Even more interesting using an if-else statement as well:
var myVar = fifalse "flower puppy"elsfitrue "human skin"els"space pedals"ret;// myVar is "human skin"
With a half completed if statment, we can also start chaining more stuff to it later
var myif = fifalse "flower puppy"elsfifalse "human skin";// do some other stuff, and add to the chain:var myVar = myifels"crapware"ret;// myVar is "human skin"
Wait, this is not functional enough!
Ok, how about: any value passed into any fi conditional can be either a function, or a value
var myVar = fi return 1 > 0;return 300 / 0;elsreturn 1337/7; ret;// myVar is 191
Functions that don't meet a conditional are never executed, and conditionals that dont' meet a condition (like an else if conditional in an if statement evaluated as true) will also never get executed.
var sw = require'fi'ternary;var myVar = ternary return 1 > 4; return 15; return 42; ;// myVar is 42
Switch statement using an object:
var sw = require'fi'sw;var myVar = sw"Rainbow"red: "Redish"green: "Greenish"blue: "Blueish"default: "Some color";// myVar is "Some color"
Switch statement using an array (so you can use functions as your keys):
var myVar = sw"Rainbow"return "red"; "Redish"return "green"; return "Random green";return "blue"; "Blueish"return "Some color"; // our default;// myVar is "Some color"
This library is pretty fresh and any pull requests or feature requests are welcome.
Arnor Heidar Sigurdsson @arnorhs on Twitter