Dragon Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Dragon?


Dragon is a general-purpose multi-paradigm language.


Supports Imperative, Object-Oriented, Functional, Declarative and Natural programming.


Influenced by Lua, Python, Ruby, Java, QML and Ring.


Small, easy to learn but extremely fast, versatile and fun.


How to install Dragon


You can choose between Dragon (JVM) and Dragon-Native (LLVM)


Dragon

Dragon-Native

Who uses Dragon?


I'm not aware of any big company using it right now


but it's a pretty young language...


and it's getting a lot of attention lately...


Starting out...


Once installed, simply run dragon or dragon-native -r and filename.dgn

Like this...

dragon -r sandbox.dgn

Comments


Comments are very simple

					
    //This is a comment



    /*This is 

    a comment as well*/
				

Printing Output


				
show("Hello World") //No carriage return


showln("Hello World") //Carriage return or new line
				

Variables


Variables don't need a type as the language is dynamic.


				
x = 5


x = "This is a string"


x = ["Hello", 5]
				

Conditionals


The good IF-ELSE always prevail

				
select "types"


a = 3

    
if a > 0
		
    showln(string(a) + " is bigger than zero")
        
else
		
    showln(string(a) + " is way to small") 
    

//3 is bigger than zero    
				

"types" is a library for data type manipulation. We're converting a from int to string

Match is a good way to have an IF-ELSE

				
a = "3"

    
match a {
		
case "1": {showln("One")}
       
case "2": {showln("Two")}

case "3": {showln("Three")}
       
case _: {showln("Out of range")}

}


//Three
				

Loops

We have many option when it comes to loops...

				
for(i = 0, i < 6, i++){
	
   showln i
	
}
				

				
a = [1,2,3,4,5]


for x : a {
	
   showln x
	
}
				

				
i = 0


while i < 5 {

    showln i

    i++

}
				

				
i = 0

do {

    showln i

    i++

}while i < 5
				

Functions


Return one value and can have default parameters

				
func say_greetings(name = "Blag"){
	
    greet = "Hello " + name
    
    return greet
    
} 

	
show(say_greetings("Blag"))

show("\n")

show(say_greetings()) 


//Hello Blag


//Hello Blag
				

Arrays and Maps


				
a = [1,2,3]

show(a[0])
				

				
select "std"


names = newarray(2)


names[0] = "Blag"  
  
names[1] = "Oliver"
    

show(length(names))			
				

				
m = {"one":1, "two":2}

showln(m["one"])



n = {1:"one", 2:"two"}

show(n[1])
				

Classes


We can create classes and objects

				
class person{
	
    func greet(name){
		
	    showln("Hello " + name + "!")
	    
    }
    
}


blag = new person()

blag.greet("Oliver")


//Hello Oliver
				

Modules


Most of the functions in Dragon are handled in modules

				
select "GUI" 


win = newWindow("My Window") 

win.setSize(400,300) 

win.setVisible()


msg = newText(text = "Hello World")


win.add(msg)
				

				
select "http"

select "json"


response = ""

s_json = ""


http("https://www.metaweather.com/api/location/
      
      search/?query=toronto","GET", 
      
      func(v){response = v})


s_json = jsondecode(response)

showln(s_json[0]["latt_long"])
				

				
select "graphic"

select "std"


window("graphics", 800, 600)


x = 150; y = 50; r = 500


for(i=0,i<5,i++){

    color(rand(0,255),rand(0,255),rand(0,255))

    oval(x, y, r, r)

    x = x + 50

    y = y + 50

    r = r - 100

}


repaint()
				

Fibonacci list sequence


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


Create a file and call it fibonacci.dgn


				
select "std"

select "types"


func fib(num, a, b){

    result = ""

    if a > 0 && num > 1

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

    else if a == 0

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

    return result

}
				
				
show "Enter a number: "

num = readln()

showln(fib(num,0,1))
				

To run this just do


dragon -r app.dgn

Making an LCD Number App


This is my favorite application of all time...


Create the app and name it LED_Numbers.dgn


				
select "std"

select "types"


leds = {0:" _  ,| | ,|_| ",

        1:"  ,| ,| ",

        2:" _  , _| ,|_  ",

        3:"_  ,_| ,_| ",

        4:"    ,|_| ,  | ",

        5:" _  ,|_  , _| ",

        6:" _  ,|_  ,|_| ",

        7:"_   , |  , |  ",

        8:" _  ,|_| ,|_| ",

        9:" _  ,|_| , _| "}
				
				
show "Enter a number: "

num = readln()

digits = split(num,"")


for(i=0,i<3,i++){

    for x: digits{

        line = split(leds[int(x)],",")

        show(line[i])

    }

    showln("")

}

showln("")
				

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

Decimal to Romans


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


Create the app and name it decimal_to_roman.dgn


				
select "std"

select "types"



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"}
               


keys = sort(arrayKeys(Roman_Table))
				
				 
func roman_number(number){

    result = ""

    while number > 0{

        for(i=length(keys) - 1,i>-1,i--){

            if number >= keys[i]{

                result = result + Roman_Table[keys[i]]

                number = number - keys[i]

                break

            }

        }

    }

    return result

}
				
				 
show "Enter a number: "

num = readln()

showln(roman_number(int(num)))
				

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.


Create the app and name it random_names.dgn


				
select "std"


names = ["Anne","Gigi","Blag","Juergen","Marek","Ingo",

         "Lars","Julia", "Danielle","Rocky","Julien",

         "Uwe","Myles","Mike","Steven","Fanny"]


last_names = ["Hardy","Read","Tejada","Schmerder",

              "Kowalkiewicz","Sauerzapf","Karg",

              "Satsuta","Keene","Ongkowidjojo",

              "Vayssiere","Kylau","Fenlon",

              "Flynn","Taylor","Tan"]



full_names = newarray(1000000)
				
				
for(i=0,i<1000000,i++){

    full_names[i] = names[rand(from=0,to=16)] + " " + 
    
                    last_names[rand(from=0,to=16)]

}
				

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


That was pretty good and fast

We run it using -s -r in order to get stats

Counting Letters


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


Name the app count_letters.dgn


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


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


				
select "std"

select "files"

select "functional"

select "types"


file = fopen("readme.txt")

line = readLine(file)


letters = {}


for(i=0,i<length(line),i++){

    letter = toChar(charAt(line,i))

    if arrayKeyExists(letter,letters) == 0{

        letters = letters + {letter:1}

    }
				
				
    else{

        letters[letter] = letters[letter] + 1

    }

}


for key, value : letters{

    showln(key + " => " + value)

}
				

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


That's it for now...


Dragon is a young but very promising language


And it's production ready!


Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...