Dao Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Dao?


Lightweight and optionally typed programming language.


It includes features that can make concurrent programming very simple.


Support closures and anonymous functions.


Designed and implemented as a register-based virtual machine.

How to install Dao?


Simply go to Dao Source Code (https://github.com/daokoder/dao) and extract it


Also (and this is optional) download Modules (https://github.com/daokoder/dao-modules) and extract it and move the content to the modules folder of the Dao Master folder...


Then simply do make -f Makefile.daomake PLATFORM. Replace PLATFORM with linux, macosx, mingw and so on...

then make install.


Who uses Dao?


As far as I know...no companie is using it...


Maybe, even not many people...


The author created basically because he was frustrated by Perl...


Starting out...


Perl doesn't come with a REPL...


So just create a file with the .dao extension and call it like this...


dao filename.dao

Package Management


If you downloaded the Modules before compiling Dao then there's nothing to do here -:)

Basic Concepts


Printing on the screen is easy...

				
io.write("This is Dao!")



#This is a Dao comment 



#{

  This is a 
	
  multiline
  
  comment...
  
#}
				

Basic Concepts


Variables can be constant, mutable or immutable...

				
const MYCONST = 'I\'m a constant'


var myVar = 'I am mutable...'


myVar = 'I can change...'


invar myInVar = 'I remain the same'
				

Arrays are easy...but there's only numeric arrays...


				
var numArray = [1, 2, 3]

var numArray2 = array{1, 2, 3}



io.writeln(numArray) # [ 1, 2, 3 ]

io.writeln(numArray2) # [ 1, 2, 3 ]
				

Multidimension Arrays are easy too...


				
var numArray = [[1, 2], [3, 4]]


var numArray2 = [1, 2 ; 3, 4]



io.writeln(numArray[0,:])

# [ 1, 2]


io.write(numArray[0,1])

# 2

Lists can hold strings and the kind of work like arrays...

				
var myList = {'a', 'b', 'c'}


var myList2 = list{5, 6, 7}



io.writeln(myList[0]) # a


io.writeln(myList2[1:3]) # { 6, 7 }
				
			
var myList = {1, 2, 3}


myList.insert(0)


io.writeln(myList) # { 0, 1, 2, 3 }


myList.append(4)


io.writeln(myList) # { 0, 1, 2, 3, 4 }


#myList.push(0, $front) --> Same as Insert

#myList.push(0, $back) --> Same as Append


io.writeln(myList.size()) # 5


myList.pop($back)


io.writeln(myList) # { 0, 1, 2, 3 }
				

Maps and Hash Maps

			
var myMap = { 'a' => 1, 'b' => 2, 'c' => 3 }


var myHashMap = { 'a' -> 1, 'b' -> 2, 'c' -> 3 }



io.writeln(myMap)

# { 'a' => 1, 'b' => 2, 'c' => 3 }


io.writeln(myHashMap)

# { 'c' -> 3, 'a' -> 1, 'b' -> 2 }
				

We can iterate on Lists, Arrays and Hashes


				
var lang = {'Spanish', 'English', 'German'}

lang.iterate(){[x] io.writeln(x)}


#Spanish

#English

#German


for(var language in lang){

  io.writeln(language)

}
				
				
var lang = {'Spanish' => 'Peru',

            'English' => 'US',

            'German'  => 'Germany'}



lang.iterate(){[key,value]

  io.writeln(key + " is spoken in " + value)

}



#English is spoken in US

#German is spoken in Germany

#Spanish is spoken in Peru
				

Functions


Are actually called Routines...and can return multiple parameters...

				
routine concat(a: string, b: string){

  return a + " " + b, (a + " " + b).size()

}


var (message, size) = concat('Hello', 'Dao')



io.writeln(message)  #Hello Dao

io.writeln(size)     #9
				

Classes

				
class Person

{

  private

    var name

    var age = 0


  public

    routine Person(name :string){

      self.name = name;

    }

  

    routine .name(){ return name }

    routine .age(){ return age }

    routine .age=(newval :int){ age = newval }

}
  		
				
var Blag = Person('Blag')


Blag.age = 40


io.writeln(Blag.name)  #Blag


io.writeln(Blag.age)   #40
  		

Fibonacci List


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


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


Name your file "fibonacci.dao"


				
routine fib(num :int, a :int, b :int){

  var result = ""

  if (a > 0 and num > 1){

    result = result + " " + (string)(a + b) + 
    
             fib(num - 1, a + b, a)

  }else if (a == 0) {

    result = (string)a + " " + (string)b + " " + 
    
             (string)(a + b) + fib(num - 1, a + b, b)

  }

  return result

}
				
				
io.write('Enter a number: ')



invar num = io.read()



io.writeln(fib((int)num, 0, 1))
				

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

dao "Name_of_File.dao"


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.dao"


				
invar leds = {0 => {' _  ','| | ','|_| '},

              1 => {'  ','| ','| '},

              2 => {' _  ',' _| ','|_  '},

              3 => {'_  ','_| ','_| '},

              4 => {'    ','|_| ','  | '},

              5 => {' _  ','|_  ',' _| '},

              6 => {' _  ','|_  ','|_| '},

              7 => {'_   ',' |  ',' |  '},

              8 => {' _  ','|_| ','|_| '},

              9 => {' _  ','|_| ',' _| '}}
              

io.write('Enter a number: ')


invar num = io.read()
				
				
var i = 0

while(true){

  var j = 0

  while(true){

    io.write(leds[(int)num[j]-48][i])

    if(j == num.size() - 2) break

    j += 1

  }

  io.writeln("")

  if(i == 2) break

  i += 1

}

io.writeln("")
				

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


Random Names


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


We will measure the runtime


Name your file "random_names.dao"


				
load random

load time


var start = time.now().second


invar names = {'Anne','Gigi','Blag','Juergen','Marek',

               'Ingo','Lars','Julia', 'Danielle',
               
               'Rocky','Julien','Uwe','Myles','Mike',
               
               'Steven','Fanny'}


invar last_names = {'Hardy','Read','Tejada',
	
                    'Schmerder','Kowalkiewicz',
	                
                    'Sauerzapf','Karg','Satsuta',
	                
                    'Keene','Ongkowidjojo',

                    'Vayssiere','Kylau','Fenlon',

                    'Flynn','Taylor','Tan'}
				
				
var full_names = {}

var i = 0

while(true){

  full_names.append(names[random.rand(15)] + " " + 
  
                    last_names[random.rand(15)])

  if(i == 99999) break

  i += 1

}



invar end = (time.now().second) - start

io.writeln("Time: " + (string)end)

io.writeln("10000 names generated")
			

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

How this behaves in Python?

And what about Go?

Perl was faster this time!

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.cr"


				
invar Roman_Table = {1000=>'M',900=>'CM',500=>'D', 

                     400=>'CD',100=>'C',90=>'XC', 

                     50=>'L',40=>'XL',10 =>'X', 

                     9=>'IX',5=>'V',4 =>'IV',1 =>'I'}
			
				
routine roman_number(number :int){

  var result = ""

  while(number > 0){

    var counter = 0

    Roman_Table.keys().sort($descend).iterate(){[key]

      if(number >= key and counter == 0){

        result += Roman_Table[key]

        number -= key

        counter += 1

      }

    }

  }

  return result

}
			
				
io.write('Enter a number: ')



invar num = io.read()


io.writnln(roman_number((int)num))
				

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 "count_letters.dao" (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 Dao"


				
load stream


var line = io.read('readme.txt').trim()

var counter = {'' => 0}

line.iterate($char){[key]

  if(counter.find(string(1, key)) != none){

    counter[string(1, key)] += 1

  }else{

    counter.insert(string(1, key), 1)

  } 

}
				
				
counter.erase('')



counter.iterate(){[key, value]

  io.writeln(key + " => " + (string)value)

}
				

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


That's it for now


Dao is a nice and interesting language...


Although it seems to be a little bit abandoned...


It has an http module...but strange enough...the POST method only allows for 2 inputs...


Let's keep an eye on Dao to see if it keeps evolving in the future...

Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...