Euphoria (programming Language) - Examples

Examples

Note: Comments start with a double dash "--" and go through the end of line.

The following code looks for an old item in a group of items. If found, it removes it by concatenating all the elements prior to it with all the elements after it. Note that the first element in a sequence has the index one and that $ refers to the length (i.e. total number of elements) of the sequence.

global function delete_item( object old, sequence group ) integer pos -- Code begins -- pos = find( old, group ) if pos > 0 then group = group & group end if return group end function

The following modification to the above example replaces an old item with a new item. As the variables old and new have been defined as objects, they could be atoms or sequences. Type checking is not required as the function will work with any sequence of data of any type and requires no external libraries.

global function replace_item( object old, object new, sequence group ) integer pos -- Code begins -- pos = find( old, group ) if pos > 0 then group = new end if return group end function

Furthermore, no pointers are involved and subscripts are automatically checked. Thus the function cannot access memory out-of-bounds. There is no need to allocate or deallocate memory explicitly and no chance of a memory leak.

The line

group = group & group

shows some of the sequence handling facilities. A sequence may contain a collection of any types, and this can be sliced (to take a subset of the data in a sequence) and concatenated in expressions with no need for special functions.

Read more about this topic:  Euphoria (programming Language)

Famous quotes containing the word examples:

    Histories are more full of examples of the fidelity of dogs than of friends.
    Alexander Pope (1688–1744)

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)

    It is hardly to be believed how spiritual reflections when mixed with a little physics can hold people’s attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.
    —G.C. (Georg Christoph)