Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Messages - silenus

Pages: 1 ... 10 11 [12]
Code Vault / a pair function adapters for sort_array
« on: April 17, 2007, 08:53:23 AM »
I wrote a pair of function adapters for sort array which came up in a problem in sorting and displaying list data. The first comparator() converts a binary "greater or equal than" comparison into the 3 value comparison needed by sort_array that returns 1,0, or -1 depending on if the value is greater, equal or less than the respective second value respectively for example to sort a list of integers with this function one would do the following:

sort_array(array_of_ints, comparator( (: $1 >= $2 :) ) )

The second function is for sorting tuples when you have to sort a list of items which consist of lists of data or tuples where each element at a given index is of the same type. i.e. ({"Silenus", 1, "orc", "warrior"}), ({"Cratylus", 1, "human", "mage"}) it creates a comparison function which gives priority to the indexes as ordered. An example usage might be (using the previous data)-

sort_array(array_of_tuples, tuple_comparator( ({ comparator( (: $1 >= $2 :) ), (: strcmp($1,$2) :) }), ({1,0}) ) )

which prioritizes the sort by level and if the levels are found to be equal sorts by name.

Both functions rely on tricks from functional programming in that they both create functions on the fly based on a function or functions passed in. The code might be a bit cryptic to those not use to this style but is illustrative of this approach.

/* Converts a greator or equal binary comparator into a 3 value comparator */
public function comparator(function f)
   return (: evaluate($(f), $1, $2) ? ( evaluate($(f), $2, $1) ? 0 : 1) : -1 :);

    Compares two tuples in the order specified by the indices. If a comparator
    applied to an index early in the order results in equality, the next
    comparator is used on the next index on the list. 
private function tuple_comparator_(function array, int array);

public function tuple_comparator(function array f, int array i)
   if( sizeof(f) != sizeof(i) ) error("Argument arrays must be of the same size.");
   return tuple_comparator_(f,i);

private function tuple_comparator_(function array f,int array i)
   if( sizeof(f) == 0 ) return (: 0 :);
   return (: evaluate( $(f[0]), $1[$(i[0])], $2[$(i[0])] ) != 0 ? evaluate( $(f[0]), $1[$(i[0])], $2[$(i[0])] ) : evaluate( $(tuple_comparator_(f[1..],i[1..])), $1, $2 ) :);



Design Lab / meta programming and LPC etc
« on: April 09, 2007, 03:01:02 PM »
Hi folks,

I have been working on a meta programming tool for MudOS which so far seems to work on DS 2.4.1 but I am sure will work for the most part on other libraries for MudOS. The issue that I am wrestling with is how to further develop this tool. The basic idea I have so far implemented is quite simple-

The code simply hooks into compile_object with a slightly altered syntax. The file is parsed and converted into a generator LPC object which then is used to create the real LPC code which is compiled as an object and returned as object.

The issue here for me is my syntax so far is far from pretty. Currently @@@ tags are used to mark special regions and these are parsed as either text functions or copied wholesale into the constructed object file. The text functions are called by the user in a special block which is used to construct the remainder of the file. I wish however that this syntax could be a bit cleaner.

I am curious if there have been other projects for extending LPC in this manner which might have a nicer syntax I could perhaps emulate. Or if anyone has better ideas of how to mix LPC as data and LPC as generative functions in a single file.



Code Vault / partition_array sefun
« on: April 04, 2007, 03:08:22 PM »
Someone mentioned a problem on intercre/ds channel(can't remember which) about grouping users with the same ip addresses into clusters and I thought of this as a possible solution and decided to post it. It's a bit more general and is based on the idea of partitions and equivalence classes though there is in this case only a partition function. I hope someone finds it useful. For example to solve the above problem would be quite easy once you have this function just do- partition_array( map( users(), (: query_ip_number($1) :) ) ) and implode the result as needed.

   partitions an array into equivalence classes based on a partition function
    example usage- partition_array( ({1,2,3,4,5,6}), (: $1 % 2 :) ) would return
    ({ ({2, 4, 6}), ({1, 3, 5}) }).   

varargs mixed array partition_array(mixed array ar, function f)
   mapping m = ([]);
   f = undefinedp(f) ? (: $1 :) : f;
   foreach(mixed elem in ar)
      if( undefinedp( m[ evaluate(f, elem) ] ) ) m[ evaluate(f, elem) ] = ({ elem });
      else m[ evaluate(f, elem) ] += ({ elem });
   return values( m );

Pages: 1 ... 10 11 [12]