Shawn Pringle

Shawn Pringle's Web Document

Standard Library Routines

In the EUPHORIA 4.0 Standard Library there are many that take some notion of a needle and haystack: You can conceptually divide these into five classes depending on what kind of object you take as the 'needle'.

Consider s a haystack and x a needle and the abstract notion of matching. A match is indicated by a beginning i and an ending j. Depending on what kind of needle you have you have a different operational definition of a match:

  • You can consider an object is a match at i, if it is equal to some s[i]
  • You can consider an object is a match at i for a key, key; if it is equal to some s[i][key]. This is a keyed needle4.
  • You can consider an object is a match at i, if it is equal to s[i..j] for some j. This is a 'slice' needle.
  • You can consider an object x is match at i if, it is such that one if its members x[j] is equal to s[i]. This is an 'any-member' needle.
  • You can consider wildcard string x to match in s at i, if for some j wildcard:is_match(x,s[i..j]) returns true. This is a wildcard needle
  • You can consider a regular expression x to match if it matches as a regular expression of s

Objects have a degree. An atom has a degree 0; a string a degree of 1. A sequence of degree 2 contains string as members such as a list of names.

Locating or Processing based on Data already in a Sequence
descriptionsimple member2 any membersliceregular expression slice wildcarddegree change
match a needle forwards and return an index find(x,s) find_any(x,s) match(x,s) See note 3variable
match a needle backwards and return an index rfind(x,s) rmatch(x,s) to 0
match needle and return all indecies find_all(x,s) match_all(x,s) re:match_all(x,s) variable
split based on some needle split_any(x,s) split(x,s) re:split(x,s) +1
Replace all data matching needle with some other supplied data mapping replace_all re:find_replace no change
Replace all data matching with some other supplied data to some limit search:find_replace search:match_replace, replace (limit is always 1)re:find_replace_limitno change
Removing Data and its place1remove_item, remove_allno change
Inserting data and its place1add_itemno change
appendoperator &
Indicate whether the needle matches the entire stringlength(s)=1 and equal(x,s[1]) is_in_list(s,x) equal(s,x)re:is_match(x,s)wildcard:is_match(x,s)to 0, boolean
Indicate whether the needle matches a portion of the string match_any(x,s) re:has_match to 0, boolean
  1. The missing insert and remove data routines for slice and regular expression needles can be easily simulated using replacement routines.
  2. Missing simple member routines can be simulated by passing the item as {item} to the slice routines.
  3. The routine regex:find returns a value which is much more complex than an integer. However, deeply nested in this sequence the starting index is contained.
  4. There are no routines for this kind of search in the 'Standard Library'. However in the Win32lib wrapper library there are w32findKey and w32findKeyEx.
Using Indeces on Sequences
descriptionsingle indexmany indiciesslicedeep index
retrieve a value with an index[] operatorextract, projectslice, [..] operatorfetch
Overwriting a value into a sequence at some index[] =patchstore
Insert Item and providing its placeinsertsplice

These tables are neither brief nor complete. With a more complete table you might see how one could profit from these similarities. You might not be able to remember the slice needle routine names and you can look them up in this table. You might decide it would be a good idea to alias them to the similar names or even the same names thanks to namespaces. To make data encapsulation for these kinds of operations you would need a file for every kind of needle: We already have different files for wildcard, regex. You can create one for any-item needles, one for slice needles and one for item needles.

Needles Types and Application

There is only one wildcard routine, is_match. Others can be easily built up based on this routine and its only known advantage is simplicity. For user interfaces this might be what you want. This is the same used in searching for files. Needles and haystacks are supposed to be strings but there is no indication in the documentation that they must be.

Slice and member needles are the basic versions that have been with us from the early days of EUPHORIA. Also easy to use like wildcards but less powerful than wildcard needles. These are not limited to string data.

Any Member needles, like slice and member are not limited to strings and needles must be the same degree as the haystack. This means for searching a list of strings you must use another list of strings. Useful internally in a program to be sure but not for regular end user input.

Regular expression needles have the most complete set of functions and options I have ever seen. In terms of completeness and its unique subgroups it really is in a completely different class. However, both the needle and the haystack must be UTF-8 compliant strings unlike the other needle/haystack routines you will have to convert. For the user, you might correctly judge that most users don't want this and use it only internally in your application. I would be disappointed though.

Windows Calculator

Ever wonder why the Windows calculator is as dumb as the five dollar calculators you find in stores? You cannot type in an expression like (32+42)1/2 and get an answer. I have uploaded one that you can do just that at the Euphoria Archive:, just do a search for calculator. I had to modify their win32lib wrapper to get the thing to display superscripts.

Create a Free Website