io Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is io?


Io is a programming language focused on expressiveness through simplicity.


It's pure, dynamic, concurrent and accesible.


It's a dynamic prototype-based programming language.


Inspired by SmallTalk, Self, NewtonScript, ACT, Lisp and Lua.

How to install IO?


Follow the instuctions on IO Github


Who uses IO?


  • I don't think there's anyone actually using it in Production...
  • IO is amazing and kind of mind bending...so might be more for Programming Language Geeks like myself -:).

Starting out...


Once IO is installed, we can simply call it from the terminal


Package Management


Eerie is the package manager for io


Installation instructions are in the Github page as well as how to use it...


Basic Concepts


One line comments are // and multiple lines comments are /* and */.


Precedence must be enforced by parenthesis


				
(2*3+5) print

=>11


(2*(3+5)) print

=>16
				

Basic Concepts


Printing on the screen is easy...

				
text := "This is io!"



text print



=>This is io!
				

Yep, print goes after the text...basically you pass it as a parameter -;)


You can assign values using := or =


:= is prefer for initialization and = to reassign or update the value...

				
a := "Hello"


a print


=>Hello



a = 5


a print


=>5
				

Data Structures


Let's start with Lists

				
lst := List clone append(1, 2, 3)

=>list(1,2,3)



lst size

=>3



lst append(4)

=>list(1,2,3,4)
				

				
lst map(*2)

=>list(2,4,6,8)



lst select(<3)

=>list(1,2)



lst at(2)

=>3



lst remove(2)

=>list(1,3,4)
				

Dictionaries

				
dict := Map clone



dict atPut("Name", "Blag")



dict at("Name")

=>Blag



dict hasKey("Name")

=>true



dict foreach(k, v, (k .. ":" .. v) println)

=>Name:Blag
				

Objects

				
Person := Object clone


Person name ::= nil

Person age ::= nil



blag := Person clone setName("Blag") setAge(41)

=> Person

  age              = 41

  name             = "Blag"



blag age

=> 41
				

Strings

				
text := "Hello"



hello at(0)

=> 72



hello at(0) asCharacter

=> H



text = text .. " World"



text print

=> Hello World
				

Methods

Methods are anonymous functions assigned to a variable

				
hello := method(name, "Hello " .. name)



hello("Blag")

=> Hello Blag



Blag := Object clone

Blag speak := method("Whazzup!" print)



Blag speak

=> Whazzup!
				

Conditions

				
number := 2



#This should go in one line...

#otherwise it will not work -:(


(number == 1) ifTrue("Nope" print) 

              ifFalse("Yep!" print)

=> Yep!




if(number == 1,("Nope" print),("Yep!" print))

=> Yep!
				

Loops

				
for(i,1,10,write(i," "))

=> 1 2 3 4 5 6 7 8 9 10



list("a", "b", "c") foreach(println)

=> a

   b

   c
				

Fibonacci List


Finally...we're going to make our first application...


So grab your favorite text editor and get ready...


Name your file "Fibonacci.io"


				
#This needs to go in one line...


fib := method(num,a,b,result, 

       if(a > 0 and num > 1) 
       
       then(result = result .. (a+b) .. " " .. 
       
            fib(num-1,a+b,a,result)) 
            
       elseif(a == 0) 
       
       then(result = a .. " " .. b .. " " .. 
       
            (a+b) .. " " .. fib(num-1,a+b,b,result)); 
            
            return result)
				
				
"Enter a number: " print

number := ReadLine readLine

fib(number asNumber,0,1,"") println
				

Open the Terminal and go to your source code folder...

io "Name_of_File.io"


When we run it we're going to see...

Making an LED Number App


This is one of my favorite codes of all time...


Name your file "LED_Numbers.io"


				
led := Map clone

led atPut("0", " _  ,| | ,|_| ")

led atPut("1", "  ,| ,| ")

led atPut("2", " _  , _| ,|_  ")

led atPut("3", "_  ,_| ,_| ")

led atPut("4", "    ,|_| ,  | ")

led atPut("5", " _  ,|_  , _| ")

led atPut("6", " _  ,|_  ,|_| ")

led atPut("7", "_   , |  , |  ")

led atPut("8", " _  ,|_| ,|_| ")

led atPut("9", " _  ,|_| , _| ")
				
				
"Enter a number: " print


number := ReadLine readLine



line := method(a, b, led at(number at(a) asCharacter) 

        split(",","\n") at(b) print)



digits := method(a, for(j,0,number size - 1, 

          line(j, a)) "\n" print)



for(i,0,2, digits(i))
				

When we run it we're going to see...


Random Names


This App will generate 100,000 random names using two 16 elements lists


We will measure the runtime


Name your file "Random_Names.io"


				
beginTimer := method(self t1 := Date clone now)


endTimer := method(s, mps := Date clone now 

                   secondsSince(t1); 
                   
                   write(s, mps asString(0,2) , "\n"))


beginTimer


names := List clone append("Anne","Gigi","Blag",

                           "Juergen","Marek","Ingo",
                           
                           "Lars","Julia","Danielle",
                           
                           "Rocky","Julien","Uwe",
                           
                           "Myles","Mike","Steve",
                           
                           "Fanny")
				
				
last_names := List clone append("Hardy","Read",

                                "Tejada","Schmerder",
                                
                                "Kowalkiewicz",
                                
                                "Sauerzapf","Karg",
                                
                                "Satsuta","Keene",
                                
                                "Ongkowidjojo",
                                
                                "Vayssiere","Kylau",
                                
                                "Fenlon","Flynn",
                                
                                "Taylor","Tan")

full_names := List clone
			
				
for(i,0,100000, full_names append(

    names at(Random value(0, 16) floor) 
    
    .. " " .. names at(Random value(0, 16) floor)))



endTimer("Time: ")



"100000 names generated" print
			

When we run it we're going to see...

How this behaves in Python?

And in Julia?

Decimal to Romans


This App will create a Roman Numeral based on a decimal number


This will include some nice commands...


Name your file "Decimal_to_Roman.io"


				
Roman_Table := Map clone

Roman_Table atPut("1000", "M")

Roman_Table atPut("900", "CM")

Roman_Table atPut("500", "D")

Roman_Table atPut("400", "CD")

Roman_Table atPut("100", "C")

Roman_Table atPut("90", "XC")

Roman_Table atPut("50", "L")

Roman_Table atPut("40", "XL")

Roman_Table atPut("10", "X")

Roman_Table atPut("9", "IX")

Roman_Table atPut("5", "V")

Roman_Table atPut("4", "IV")

Roman_Table atPut("1", "I")
			
				
Keys := List clone append("1000","900","500",

                          "400","100","90","50",

                          "40","10","9","5","4","1")



Roman_Number := method(number,result,

                while(number asNumber > 0, 
                
                Keys foreach(key, 
                
                if(number asNumber >= key asNumber) 
                
                then(result = result .. 
                
                Roman_Table at(key); 
                
                number asNumber = number asNumber - 
                
                key asNumber; break))); return result)
			
				
"Enter a number: " print

number := ReadLine readLine

Roman_Number(number, "") print
				

When we run it we're going to see...


Count Letters


In this example we're going to read a file and count how many time a letter appears...


Call your file "countletters.io" (all in lowercase).


Create a file called "readme.txt" with the following text...


"This is a text file that we're going to read using io"


				
f := File with("readme.txt")


f openForReading



letters := Map clone


for(i,0,f size-1,(letters hasKey(f at(i) asCharacter)) 

    ifTrue(letters atPut(f at(i) asCharacter, 
    
    letters at(f at(i) asCharacter) + 1)) 
    
    ifFalse(letters atPut((f at(i) asCharacter),1)))


f close


letters foreach(k, v, (k .. ":" .. v) println)
			

When we run it we're going to see...


That's it for now


io is a pretty awesome language


Sadly...it's not being developed anymore...although patches are accepted...


Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...