Open Euphoria Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Open Euphoria?


Open Euphoria is a general-purpose high-level imperative/procedural interpreted programming language.


It was developed by the openEuphoria Group in Toronto, Canada.


Open Euphoria is free and Open Source.


It can be generated and compiled into C.

How to install Open Euphoria?


Open Euphoria is available on Windows, OS X and flavors on Linux

Just go to the Download section and search for your system.

Who uses Open Euphoria?


  • Haven't found any -:(.

Starting out...


Open Euphoria doesn't come with a REPL, so we need to use file and then load it into the interpreter...

Which is eui

Basic Concepts


Comments are --.


There's only 3 types of variables...


  • atom -> Elemental type. Holds a numeric or string value.
  • sequence -> Array type. Holds atoms and/or sequences.
  • integer -> Represents a numeric value, and it's mostly replaced by atom.

Well...there's also object but that's the father of atom and sequence...

				
atom myatom

sequence mysequence

sequence anothersequence



myatom = 2016

mysequence = "This is the year "

anothersequence = {'H','e','l','l','o'}


puts(1,mysequence)

print(1, myatom)

puts(1,"\n")

puts(1,myatom)

puts(1,"\n")

puts(1,anothersequence)
				

Maps


Open Euphoria doesn't have arrays, but we can use maps

				
include std/map.e


sequence myvalue


map myMap = new()

    put(myMap, 0, "A")

    put(myMap, 1, "B")

    put(myMap, 2, "C")



for i = 0 to 2 do

    myvalue = map:get(myMap,i)

    puts(1,myvalue)

    puts(1,"\n")

end for
				

Functions and Procedures

Functions must always return a value...

Procedures doesn't return a value...


				
atom result



function sum(atom a, atom b)

    atom sum

    sum = a + b

    return sum

end function



result = sum(4,8)



print(1,result)
				
				
procedure test()

    puts(1,"\nHello World!")

end procedure



test()
				

Callable Functions

We can call a function as a parameter of another function

				
function sum(atom a, atom b)

    return a + b

end function



function multi(atom a, atom b)

    return a * b

end function



function operation(integer f, atom a, atom b)

    return call_func(f ,{a,b})

end function



print(1,operation(routine_id("sum"),4,8)) puts(1,"\n")

print(1,operation(routine_id("multi"),4,8))
				

Fibonacci List


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


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


Name your file "Fibonacci.ex"


				
include std/convert.e

include get.e



atom num

sequence result = ""
			
				
function fib(atom num, atom a, atom b)

    sequence result = ""

    if a > 0 and num > 1 then

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

    end if

    if a = 0 then

        result = to_string(a) & " " & 
        
                 to_string(b) & " " & 

                 to_string(a+b) & " " & 
                 
                 fib(num-1, a+b, b)

    end if  

    return result  

end function
				
				
num = prompt_number("Enter a number: ",{})

result = fib(num,0,1)

puts(1,result)
				

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

You need to call it as eui "name_of_file.ex"


Making an LED Number App


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


Name your file "LED_Numbers.ex"


				
include get.e

include std/map.e

include std/convert.e

include std/sequence.e



sequence num

sequence snum

object onum

atom anum
				
				
map leds = new()

   put(leds, 0, {" _  ","| | ","|_| "})

   put(leds, 1, {"  ","| ","| "})

   put(leds, 2, {" _  "," _| ","|_  "})

   put(leds, 3, {"_  ","_| ","_| "})

   put(leds, 4, {"    ","|_| ","  | "})

   put(leds, 5, {" _  ","|_  "," _| "})

   put(leds, 6, {" _  ","|_  ","|_| "})

   put(leds, 7, {"_   "," |  "," |  "})

   put(leds, 8, {" _  ","|_| ","|_| "})

   put(leds, 9, {" _  ","|_| "," _| "})
				
				
num = prompt_string("Enter a number: ")

snum = breakup(num,1)

for i = 1 to 3 do

        for j = 1 to length(num) do

            anum = to_number(snum[j])

            onum = map:get(leds,anum)

            puts(1,onum[i])

        end for

        puts(1,"\n")

end for

puts(1,"\n")
				

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


				
sequence full_name = {}

sequence full_names = {}



full_name = ""

full_names = repeat(" ", 100000)



sequence names = {{"Anne"},{"Gigi"},{"Blag"},
	
	              {"Juergen"},{"Marek"},{"Ingo"},
	              
	              {"Lars"},{"Julia"},{"Danielle"},
	              
	              {"Rocky"},{"Julien"},{"Uwe"},
	              
	              {"Myles"},{"Mike"},{"Steven"},

                      {"Fanny"}}
				
				
sequence last_names = {{"Hardy"},{"Read"},{"Tejada"},
	
	               {"Schmerder"},{"Kowalkiewicz"},
	                   
	               {"Sauerzapf"},{"Karg"},
	                   
	               {"Satsuta"},{"Keene"},
	                   
	               {"Ongkowidjojo"},{"Vayssiere"},
	                   
	               {"Kylau"},{"Fenlon"},{"Flint"},
	                 
	               {"Taylor"},{"Tan"}}
	               
	   
	               
for i = 1 to 100000 do

    full_name = names[rand(16)] & 

                " " & last_names[rand(16)]

    full_names[i] = full_name

end for	               
			

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


				
include get.e include std/map.e include std/sort.e



atom num

sequence onum sequence key

sequence result = ""



map romans = new()

   put(romans, 1000, "M") put(romans, 900, "CM")

   put(romans, 500, "D") put(romans, 400, "CD")

   put(romans, 100, "C") put(romans, 90, "CX")

   put(romans, 50, "L") put(romans, 40, "XL")

   put(romans, 10, "X") put(romans, 9, "IX")

   put(romans, 5, "V") put(romans, 4, "IV")

   put(romans, 1, "I")
			
				
function Roman_Number(atom num,sequence key,map romans)

sequence result = ""

    while num > 0 do

        for i = 1 to 13 do

            if num >= key[i] then

                result = result & map:get(romans,key[i])

                num = num - key[i]

                exit

            end if

        end for

    end while

    return result

end function
			
				
print("Enter a number: ")

num = int( input() )

print(Roman_Number(num))

printl("")
			

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.ex" (all in lowercase).


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


"This is a text file that we're going to read it using Open Euphoria"


				
include std/sequence.e

include std/map.e

include std/io.e



integer file_num

sequence file

sequence letters

object letter



function PrintValues(object k, object v, 

                     object d, integer pc)

    writefln("[] = []", {k, v})

    return 0

end function
			
				
file_num = open("readme.txt","r",0)

file = gets(file_num)

letters = breakup(file,1)

map list_letters = new()

for i = 1 to length(letters) do

    if(map:has(list_letters,letters[i]) = 0) then

        put(list_letters,letters[i],1)

    else

        letter = map:get(list_letters,letters[i])

        put(list_letters,letters[i],letter + 1)

    end if

end for



map:for_each(list_letters,routine_id("PrintValues"))

			

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


That's it for now


Open Euphoria is a really nice language...


That sadly haven't got enough promotion...even when there's a big community around it...


Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...