Friday, May 22, 2009

A look at Ioke

This will be a short look at Ioke ( a programming language created by Ola Bini. Ioke code is read from left to right. You can read about the execution model in Ioke here. The first symbol implicit on each line is called Origin. "kind" is a method that tells you the kind of the symbol it is evaluated on.
iik> kind
+> "Origin"

iik> 4 kind
+> "Number Integer"

Addition works as you would expect.
iik> 1 + 1
+> 2

+ is actually a method defined in the kind Number Integer so we can also call it as
iik> 1 +(1)
+> 2

Create variables by assigning a value to a new name. What you actually are doing is creating a cell in the receiving object, which is Origin in this case.
iik> x = 1

You can see what cells are defined on Origin withe the cells method. This returns a Dict with names and values. To get a nicer view we can use the each method and tell it to apply println to each pair in the Dict
iik> cells each(println)

You will see x as the last cell in the list of cells on Origin.

Ioke does some operator shuffling between readable syntax as above and a canonical form. So the above assignment can also be written as.
iik> =(x, 1)

Which is the form Ioke will print parsed code in. With this syntax it becomes clear that = is just another function that takes two arguments. You define methods with the method keyword. To see this in practice we create a method with an assignment in it.
iik> a = method(x=1) 
+> a:method(=(x, 1))

The value returned from a function is the last evaluated expression.
iik> myFunction = method("Hello there")
+> myFunction:method("Hello there")

iik> myFunction println
Hello there
+> nil

To define a method that takes some arguments:
iik> myAdd = method(num1, num2, num1 + num2)

iik> myAdd(3, 4)
+> 7

We can also choose to add our new function to the Ioke kind Number Integer.
iik> Number Integer add = method(num, num + self)
+> add:method(num, num + self)

iik> 3 add(2)
+> 5

self (or @) refers to the current object, like this in Java. We are also free to redefine + on Number Integer if we want to.
iik> Number Integer + = method(num, self - num)

iik> 3 + 2
+> 1

Let's try some conditions in our function. The if function will evaluate its second argument if the first argument is true and the third argument otherwise. One or both of the branches can be left out. There are also conditional functions called unless, cond and case. Go look them up in the ioke reference.
iik> Number Integer add2IfTrue = method(boolean, if(boolean, self + 2, self))
+> add2IfTrue:method(boolean, if(boolean, self +(2), self))

iik> 2 add2IfTrue(4>3)
+> 4

iik> 2 add2IfTrue(3>3)
+> 2

Looping over stuff can be done with loop, while, until, times, each and some other. Here are some examples from the ioke guide.
iik> loop("hello" println) ;; An infinite loop

iik> x = 0
iik> loop(if(x > 10, break) . x++ println)
iik> 3 times("hello" println)

iik> [1, 2, 3] each(println)

[] creates arrays and {} creates Dict's or hashes.
iik> nums = {1 => "one", 2 => "two"}
+> {1 => "one", 2 => "two"}

iik> nums[1]
+> "one"

No comments: