Wiki Diff NewsExamples, revision #14 to tip

Here are short eucode examples that will appear on the home page (News). Examples should be short and separated by an HR ~-~-~-~-. Examples should include a 1 or 2 line beginning comment explaining what the demo is attempting to show. Don't include examples > 8 lines long and figure you have about 45 characters wide. Make the examples show something nice, important, elegant and attractive to people who may be considering Euphoria. The more we have the better.

To test your new addition, access the main news page with ?example=XYZ, 1 being the first example.

This introductory text will be skipped.

----

<eucode>
-- Euphoria is simple

puts(1, "Hello, World!")
</eucode>

----

<eucode>
-- Euphoria has powerful sequences

sequence nums = { 5, 6, 7 }
? nums * 2
-- Output: { 10, 12, 14 }
? nums * { 2, 3, 4 }
-- Output: { 10, 18, 28 }
</eucode>

----

<eucode>
-- user-defined types made simple:
type pair (sequence field_value)
return length(field_value) = 2
-- restricted types made simple:

type pair (sequence field_values) -- the ctor
integer howmany = length(field_values)
return howmany = 2
end type
pair p1 = { 2, 3} -- ok
pair p2 = { 2, 4, 6} -- fails
pair p3 = p1 & 0 -- fails
-- types are checked on every assignment.
pair position = { 2, 3 } -- assignment == ctor
pair p2 = { 5,6,7 }
-- so if your data gets corrupted,
-- you know exactly where it happened.

-- OUTPUT:

C:\eucode\type_example.e:8
type_check failure, p2 is {5,6,7}

--> See ex.err

</eucode>
----

<eucode>
-- Euphoria is testable
include std/unittest.e

test_equal("1 plus 1 should be 2",
2, 1 + 1)
test_report()
</eucode>

----

<eucode>
-- Euphoria is innovative
integer easy = 1_923_993, hard = 1923993

printf(1, "%d = %d\n", { easy, hard })

-- Output: 1923993 = 1923993
</eucode>

----

<eucode>
-- Euphoria is innovative
while 1 label "families" do
while 1 do -- labels are optional
while 1 label "children" do
exit "families" -- exit top level while
end while
end while
end while
</eucode>

----

<eucode>
-- Euphoria has power
switch abc with fallthru do
case 1 then
case 2 then
puts(1, "1 hit and fell through to 2")
break
case 3, 4, "John" then
puts(1, "Was 3, 4 or 'John'")
end switch
-- Default is w/o fallthru
</eucode>

----

{{{
Can be interpreted or compiled:
$ eui hello.ex
Hello, World!
$ euc hello.ex
Building... gcc -o hello hello.c
$ ./hello
Hello, World!
}}}

----

{{{
Euphoria is cross platform:
C:\Euphoria> eui hello.ex
Hello, World!
[linux@~] $ eui hello.ex
Hello, World!
[osx@~] eui hello.ex
Hello, World!
}}}

----
//Euphoria is not complex; it only has 4 built-in types.//
[quote Euphoria]
I'm not complex; I only have 4 built-in types.
* **##atom##** : Holds any number.
* **##sequence##** : Holds a dynamically resizeable list of //objects//.
* **##integer##** : Holds 30-bit signed integers; faster than //atom//
* **##object##** : Holds any of the above.

[/quote]

Search



Quick Links

User menu

Not signed in.

Misc Menu