Hello, Fate!

What guide would be complete without a "Hello, World!" example? This guide doesn't disappoint.

Assuming you've already installed Fate as a global npm module, fire up your favorite text editor. Then start typing:

from io import print

def hello(name)
  {name} | "Hello, %name!"
end

"World" | hello | print

Any idea what the output of this program will be? Save it to a file called hello.fate and then run it with the Fate interpreter from that directory:

fate hello

If you didn't see "Hello, World!" then I've either failed you miserably or you screwed up. In either case, some explanation is in order.

The first def is creating a function called hello. Within that function, there is a single expression. This expression is creating a new object with a single name property. It is then passing that object to a string using the piped function calling operator. Yes, in Fate, certain strings are actually callable functions, but we'll get to that later.

The value of the last evaluated expression is what Fate will return as the result of a Function. You can also use the return keyword if you're the controlling type.

The line that starts with "World" also uses the piped calling convention, twice! First, it is passing the string "World" into our hello function. It's then taking the result of that call, and passing it to the built-in print function.

Of course, the program could have been written like this:

from io import print

print("Hello, World!")

But what fun would that have been?

Worth Noting

Like JavaScript, Fate is case-sensitive. Additionally, the language requires new lines to separate statements (rather than semi-colons). As a result, the addition and subtraction operators must appear at the end of a line if an expression is going to span lines. For example, the following script will result in -10. This is because the 20 evaluates as-is and the - becomes a unary negation:

20
- 10

Instead, do this to treat the two lines as a single expression, yielding 10 as its result:

20 -
10

Node.js Integration

You can also compile scripts on the fly by requiring the fatejs module, and calling its compile function, like so:

// require the Fate module
var fate = require('fatejs');

// compile a script that returns a lambda
var script = fate.compile('x -> x * 100');

// execute the compiled script, 
// will return the lambda instance
var resultingLambda = script();

// spit out the result of the lambda!
console.log(resultingLambda(4));

Or, if you're lazy:

// require the Fate module
var fate = require('fatejs');
var resultingLambda = fate.evaluate('x → x • 100');
console.log(resultingLambda(4));

results matching ""

    No results matching ""