Pony Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Pony?


Pony is an object-oriented, actor-model, capabilities-secure, high performance programming language.


Pony is free and Open Source.


It's type safe...and memory safe...and exception safe...


Pony is an ahead-of-time (AOT) compiled language. There is no interpreter or virtual machine.

How to install Pony?


It can be installed on Windows (Not there yet), Linux and MacOS...but I would recommend Linux.

wget -O - http://releases.ponylang.org/buildbot@lists.ponylang.org.gpg.key | sudo apt-key add -

sudo add-apt-repository "deb http://releases.ponylang.org/apt ponyc main"

sudo apt-get update

sudo apt-get install ponyc

Who uses Pony?


  • Too young and still in development...

Starting out...


Pony code needs to be compile before using it...


You need to create a folder where your source code will reside...the name of the folder will be the name of your application.


Inside the folder you main file will be named "main.pony"...


To compile...we need go inside the folder

ponyc "main.pony"


Basic Concepts


Just line in C++ one line comments are // and multiple lines comments are /* and */.


Precedence must be enforced by parenthesis...otherwise you will get an error...

				
actor Main

    new create(env: Env) =>
    
        var number:I8 = (2 * 3) + 5 
        
        env.out.print("" + number.string())
				

Variables


We can create variables using either var or let...


"var" works like any programming language...variables can be reassigned...


"let" works like functional programming...variables cannot be reassigned...


Global variables doesn't even exist...


Arrays are easy...

				
actor Main

    new create(env: Env) =>
    
        var array: Array[String] = ["A","B","C"]
        
        var elem: String = try array(0) else "" end
        
        env.out.print(elem)
				
				
        for elem in array.values() do
        
            env.out.print(elem)
            
        end
				

We can create an array of arrays...

				
actor Main

    new create(env: Env) =>
    
        var array: Array[Array[String]] = 
                   
                   [["A","B","C"],["1","2","3"]]
        
        for i in ["0","1"].values() do
        
            for j in ["0","1","2"].values() do
            
                var elem: String = 
                
                    try array(i.u64())(j.u64()) 
                    
                        else "" end
                
                env.out.print(elem)
                
            end
            
        end
				

In Pony, everything is an expression...that's why we can do something like this...


				
actor Main

	new create(env: Env) =>
	
		var myNumber:I8 = 10
		
		var myAdd:I8 = 5
		
		myNumber = myNumber + if myAdd == 5 
		
		           then 15 else 1 end
		
		env.out.print(myNumber.string())
				

One weird thing aboout Pony...in order to create functions...you must create a class...


				
class Add
		
    fun get_add(a:I32, b:I32): I32 =>
	
       a + b



actor Main

    new create(env: Env) =>
	
    let num = Add.get_add(5,8)
	
    env.out.print(num.string())       
				

Fibonacci List


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


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


Name your file "main.pony" inside a folder called "fibonacci"


				
class Fibo

    fun get_fibo(num:I32,a:I32,b:I32):String =>
    
        var result:String = ""
        
        if (a > 0) and (num > 1) then
        
            result = result + (a+b).string() + " " + 
            
                     get_fibo(num-1,a+b,a)
            
        elseif (a == 0) then
        
            result = a.string() + " " + b.string() + 
            
                     " " + (a+b).string() + " " + 
                     
                     get_fibo(num-1,a+b,b)
            
        end
        
        result
				
				
actor Main

    new create(env: Env) =>
    
        var sNum:String = try env.args(1) else "" end
        
        var num:I32 = try sNum.i32() else 0 end
        
        var line:String = Fibo.get_fibo(num,0,1)
        
        env.out.print(line)
				

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


ponyc


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

Making an LED Number App


This is one of my favorite codes because it helps you learn a new language...


Name your file "main.pony" inside a folder called "lednumbers"


				
actor Main
	
    new create(env: Env) =>
        
        let leds: Array[Array[String]] = 
        
                  [[" _  ","| | ","|_| "],
        
                   ["  ","| ","| "],
                                          
                   [" _  "," _| ","|_  "],
                                          
                   ["_  ","_| ","_| "],
                                          
                   ["    ","|_| ","  | "],
                                          
                   [" _  ","|_  "," _| "],
                                          
                   [" _  ","|_  ","|_| "],
                                          
                   ["_   "," |  "," |  "],
                                          
                   [" _  ","|_| ","|_| "],
                                          
                   [" _  ","|_| "," _| "]]
				
				
        var num: String = try env.args(1) else "" end
        
        var i: I64 = 0
        
        var j: I64 = 0
        
        var line: String = ""
        
        var sizec: I64 = try num.size().string().i64()
        
                         else 0 end
				
				
        while i < 3 do
        
            while j < sizec do
            
               try line = 
                
               line.insert(line.size().string().i64(),
                
               leds(num.substring(j,j).u64())
               
                   (i.string().u64())) else "" end

               j = j + 1
                
            end
            
            i = i + 1
            
            j = 0
            
            env.out.print(line)
            
            line = ""
            
        end
        
        env.out.print("")
				

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 "main.pony" inside a folder called "random_names"


				
use "collections"

use "random"



actor Main

  new create(env: Env) =>
  
   let names:Array[String] = ["Anne","Gigi","Blag",
   
                              "Juergen","Marek",
                              
                              "Ingo","Lars","Julia", 
                              
                              "Danielle","Rocky",
                              
                              "Julien","Uwe","Myles",
                              
                              "Mike","Steven","Fanny"]
				
				
   let last_names: Array[String] = ["Hardy","Read",
   
                                    "Tejada",
                                    
                                    "Schmerder",
    
                                    "Kowalkiewicz",
                                    
                                    "Sauerzapf",
                                    
                                    "Karg","Satsuta",
                                    
                                    "Keene",
                                    
                                    "Ongkowidjojo",
                                     
                                    "Vayssiere",
                                    
                                    "Kylau","Fenlon",
                                     
                                    "Flynn","Taylor",
                                    
                                    "Tan"]
			
				
    var elem1: String = ""
    
    var elem2: String = ""
    
    var full_names: Array[String] = [""]
    
    let dice = Dice(MT)
    
    for i in Range(0, 100000) do
    
        elem1 = try names(dice(1,16) - 1) 
        
                else "" end
        
        elem2 = try last_names(dice(1,16) - 1) 
        
                else "" end
        
        try full_names.insert(0,elem1 + " " + elem2) 
        
            else "" end
        
    end
			

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

How this behaves in Python?

What about 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 "main.pony" inside a folder called "decimal_to_romans"


				
class Romans

    fun get_romans(num:I32,ctr:U64,
    
                   result:String):String =>
    
        var response:String = ""
        
        let roman_nums: Array[Array[String]] = 
        
                        [["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"]]
			
				
        var line:String = try roman_nums(ctr)(0) 
        
                          else "" end
        
        var iline = try line.i32() else 0 end
        
        if(num >= iline) then
        
            var value:String = try roman_nums(ctr)(1) 
            
                               else "" end
            
            response = value + 
            
                       get_romans((num - iline), ctr, 
                       
                                   result)
            
        elseif (num < iline) and (num > 0) then
        
            response = get_romans(num, (ctr + 1), 
            
                       result)
            
        end
        
        response
			
				
actor Main

    new create(env: Env) =>
    
        var sNum:String = try env.args(1) else "" end
        
        var num:I32 = try sNum.i32() else 0 end
        
        var result:String = ""
        
        result = Romans.get_romans(num,0,"")
        
        env.out.print(result)
				

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

Is that all?

Sadly...it is...


Pony is still more than fresh...heck! they don't even have a proper logo...but some nice people are working pretty heavily trying to make it work in the best and fast possible way...so stay tuned and look for more Pony in the near future...


I'm sure that Pony will cause a pretty big impact when it got finally "officially released"...


Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...