list utilities #55

Parent #79Owner #36Flags readSource RPG Core/rpgcore-patched.db

Aliases: list utilities, list_utilities

38 verbs · 7 properties · 0 children

Verbs

VerbSpecFlagsDefinerLines
makethis none thisrxd#5517
rangethis none thisrxd#557
map_prop*ertythis none thisrxd#5510
map_verbthis none thisrxd#5510
map_argthis none thisrxd#5516
map_builtinthis none thisrxd#5514
find_insertthis none thisrxd#5521
remove_duplicatesthis none thisrxd#556
arraysetthis none thisrxd#558
setremove_allthis none thisrxd#556
appendthis none thisrxd#559
reversethis none thisrxd#552
_reversethis none thisrxd#559
compressthis none thisrxd#5514
sortthis none thisrxd#5513
sort_suspendedthis none thisrxd#5519
slicethis none thisrxd#5520
assocthis none thisrxd#5511
iassocthis none thisrxd#5513
iassoc_suspendedthis none thisrxd#5516
assoc_prefixthis none thisrxd#558
iassoc_prefixthis none thisrxd#558
iassoc_sortedthis none thisrxd#5522
sort_alistthis none thisrxd#5557
sort_alist_suspendedthis none thisrxd#5535
randomly_permutethis none thisrxd#557
countthis none thisrxd#5512
flattenthis none thisrxd#5511
longest shortestthis none thisrxd#5519
check_nonstring_tell_linesthis none thisrxd#5510
reverse_suspendedthis none thisrxd#554
_reverse_suspendedthis none thisrxd#5511
randomly_permute_suspendedthis none thisrxd#559
swap_elementsthis none thisrxd#5517
random_item random_elementthis none thisrxd#5514
assoc_suspendedthis none thisrxd#5513
amergethis none thisrxd#5520
build_alistthis none thisrxd#5517

Properties

PropertyDefinerFlagsOwnerValue
nonstring_tell_lines#55r#36{}
help_msg#79rc#36
list of 45{"append (list,list,..) => result of concatenating the given lists", "reverse (list) => reversed list", "remove_duplicates (list) => list with all duplicates removed", "compress (list) => list with consecutive duplicates removed", "setremove_all (list,elt) => list with all occurrences of elt removed", "find_insert (sortedlist,e) => index of first element > e in sortedlist", "sort (list[,keys]) => sorted list", "count (elt,list) => count of elt found in list.", "flatten (list) => flatten all recursive lists into one list", "randomly_permute (list) => list with elements randomly permuted", "longest (list) => longest in list (consisting of str or list)", "shortest (list) => shortest in list (as above)", "", "make (n[,e]) => list of n copies of e", "range (m,n) => {m,m+1,...,n}", "", "arrayset (list,val,i[,j,k...]) => array modified so that list[i][j][k]==val", "", "-- Mapping functions (take a list and do something to each element):", "", "map_prop ({o...},prop) => list of o.(prop) for all o", "map_verb ({o...},verb[,args]) => list of o:(verb)(@args) for all o", "map_arg ([n,]obj,verb,{a...},args) => list of obj:(verb)(a,@args) for all a", "map_builtin (objectlist, function) => applies function to all in objectlist", "", "-- Association list functions --", "", "An association list (alist) is a list of pairs (2-element lists), though the following functions have been generalized for lists of n-tuples (n-element lists). In each case i defaults to 1.", "", "assoc (targ,alist[,i]) => 1st tuple in alist whose i-th element is targ", "iassoc (targ,alist[,i]) => index of same.", "assoc_prefix (targ,alist[,i]) => ... whose i-th element has targ as a prefix", "iassoc_prefix(targ,alist[,i]) => index of same.", "iassoc_sorted(targ,slist[,i]) => index of last element in sortedlist <= targ", "slice (alist[,i]) => list of i-th elements", "sort_alist (alist[,i]) => alist sorted on i-th elements.", "amerge (alist,[tind,[dind]]) => merges tuples of alist with matching i-th elt", "build_alist (list,N) => make an alist of N-intervals from list", "", "-- Functions that suspend --", "", "Each of these either suspends(0) as needed or takes an interval in seconds for the suspend as a first argument. See help $list_utils:<verb>.", "", "sort_suspended iassoc_suspended sort_alist_suspended", "reverse_suspended randomly_permute_suspended"}
key#1c#36<clear>
aliases#1rc#36{"list utilities", "list_utilities"}
description#1rc#36{"This is the list utilities utility package. See `help $list_utils' for more details."}
object_size#1r#36{26955, -1090650497}
html#1rc#36<clear>

Ancestry

Ancestors (nearest first): #79 Generic Utilities Package#1 Root Class

Children: none

Call graph

calls n55_3 #55:map_verb n55_3->n55_3 n55_5 #55:map_builtin n55_5->n55_5 n55_8 #55:arrayset n55_8->n55_8 n55_10 #55:append n55_10->n55_10 n55_11 #55:reverse n55_12 #55:_reverse n55_11->n55_12 n55_12->n55_12 n55_14 #55:sort n55_6 #55:find_insert n55_14->n55_6 n55_15 #55:sort_suspended n55_15->n55_6 n55_23 #55:sort_alist n55_23->n55_14 n55_23->n55_23 n55_16 #55:slice n55_23->n55_16 n55_24 #55:sort_alist_suspended n55_24->n55_14 n55_24->n55_16 n55_24->n55_24 n55_27 #55:flatten n55_27->n55_27 n55_30 #55:reverse_suspended n55_31 #55:_reverse_suspended n55_30->n55_31 n55_31->n55_31 n55_36 #55:amerge n55_36->n55_23 n55_33 #55:swap_elements n55_36->n55_33

Source

make

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1":make(n[,elt]) => a list of n elements, each of which == elt. elt defaults to 0.";
2{n, ?elt = 0} = args;
3if (n < 0)
4return E_INVARG;
5endif
6ret = {};
7build = {elt};
8while (1)
9if (n % 2)
10ret = {@ret, @build};
11endif
12if (n = n / 2)
13build = {@build, @build};
14else
15return ret;
16endif
17endwhile

range

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1":range([m,]n) => {m,m+1,...,n}";
2{?m = 1, n} = args;
3ret = {};
4for k in [m..n]
5ret = {@ret, k};
6endfor
7return ret;

map_prop*erty

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

none

Source

1set_task_perms(caller_perms());
2{objs, prop} = args;
3if (length(objs) > 50)
4return {@this:map_prop(objs[1..$ / 2], prop), @this:map_prop(objs[($ / 2) + 1..$], prop)};
5endif
6strs = {};
7for foo in (objs)
8strs = {@strs, foo.(prop)};
9endfor
10return strs;

map_verb

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1set_task_perms(caller_perms());
2{objs, vrb, @rest} = args;
3if (length(objs) > 50)
4return {@this:map_verb(@listset(args, objs[1..$ / 2], 1)), @this:map_verb(@listset(args, objs[($ / 2) + 1..$], 1))};
5endif
6strs = {};
7for o in (objs)
8strs = {@strs, o:(vrb)(@rest)};
9endfor
10return strs;

map_arg

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1"map_arg([n,]object,verb,@args) -- assumes the nth element of args is a list, calls object:verb(@args) with each element of the list substituted in turn, returns the list of results.  n defaults to 1.";
2"map_verb_arg(o,v,{a...},a2,a3,a4,a5)={o:v(a,a2,a3,a4,a5),...}";
3"map_verb_arg(4,o,v,a1,a2,a3,{a...},a5)={o:v(a1,a2,a3,a,a5),...}";
4set_task_perms(caller_perms());
5if (n = args[1])
6{object, verb, @rest} = args[2..$];
7else
8object = n;
9n = 1;
10{verb, @rest} = args[2..$];
11endif
12results = {};
13for a in (rest[n])
14results = listappend(results, object:(verb)(@listset(rest, a, n)));
15endfor
16return results;

map_builtin

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1":map_builtin(objectlist,func) applies func to each of the objects in turn and returns the corresponding list of results.  This function is mainly here for completeness -- in the vast majority of situations, a simple for loop is better.";
2set_task_perms(caller_perms());
3{objs, builtin} = args;
4if (!`function_info(builtin) ! E_INVARG => 0')
5return E_INVARG;
6endif
7if (length(objs) > 100)
8return {@this:map_builtin(objs[1..$ / 2], builtin), @this:map_builtin(objs[($ / 2) + 1..$], builtin)};
9endif
10strs = {};
11for foo in (objs)
12strs = {@strs, call_function(builtin, foo)};
13endfor
14return strs;

find_insert

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"find_insert(sortedlist,key) => index of first element in sortedlist > key";
2"  sortedlist is assumed to be sorted in increasing order and the number returned is anywhere from 1 to length(sortedlist)+1, inclusive.";
3{lst, key} = args;
4if ((r = length(lst)) < 25)
5for l in [1..r]
6if (lst[l] > key)
7return l;
8endif
9endfor
10return r + 1;
11else
12l = 1;
13while (r >= l)
14if (key < lst[i = (r + l) / 2])
15r = i - 1;
16else
17l = i + 1;
18endif
19endwhile
20return l;
21endif

remove_duplicates

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"remove_duplicates(list) => list as a set, i.e., all repeated elements removed.";
2out = {};
3for x in (args[1])
4out = setadd(out, x);
5endfor
6return out;

arrayset

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"arrayset(list,value,pos1,...,posn) -- returns list modified such that";
2"  list[pos1][pos2][...][posn] == value";
3if (length(args) > 3)
4return listset(@listset(args[1..3], this:arrayset(@listset(listdelete(args, 3), args[1][args[3]], 1)), 2));
5"... Rog's entry in the Obfuscated MOO-Code Contest...";
6else
7return listset(@args);
8endif

setremove_all

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1":setremove_all(set,elt) => set with *all* occurences of elt removed";
2{set, what} = args;
3while (w = what in set)
4set[w..w] = {};
5endwhile
6return set;

append

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"append({a,b,c},{d,e},{},{f,g,h},...) =>  {a,b,c,d,e,f,g,h}";
2if (length(args) > 50)
3return {@this:append(@args[1..$ / 2]), @this:append(@args[($ / 2) + 1..$])};
4endif
5l = {};
6for a in (args)
7l = {@l, @a};
8endfor
9return l;

reverse

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"reverse(list) => reversed list";
2return this:_reverse(@args[1]);

_reverse

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1":_reverse(@list) => reversed list";
2if (length(args) > 50)
3return {@this:_reverse(@args[($ / 2) + 1..$]), @this:_reverse(@args[1..$ / 2])};
4endif
5l = {};
6for a in (args)
7l = listinsert(l, a);
8endfor
9return l;

compress

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

none

Source

1"compress(list) => list with consecutive repeated elements removed, e.g.,";
2"compress({a,b,b,c,b,b,b,d,d,e}) => {a,b,c,b,d,e}";
3if (l = args[1])
4out = {last = l[1]};
5for x in (listdelete(l, 1))
6if (x != last)
7out = listappend(out, x);
8last = x;
9endif
10endfor
11return out;
12else
13return l;
14endif

sort

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"sort(list[,keys]) => sorts keys (assumed to be all numbers or strings) and returns list with the corresponding permutation applied to it.  keys defaults to the list itself.";
2"sort({x1,x3,x2},{1,3,2}) => {x1,x2,x3}";
3lst = args[1];
4unsorted_keys = (use_sorted_lst = length(args) >= 2) ? args[2] | lst;
5sorted_lst = sorted_keys = {};
6for e in (unsorted_keys)
7l = this:find_insert(sorted_keys, e);
8sorted_keys = listinsert(sorted_keys, e, l);
9if (use_sorted_lst)
10sorted_lst = listinsert(sorted_lst, lst[length(sorted_keys)], l);
11endif
12endfor
13return sorted_lst || sorted_keys;

sort_suspended

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1":sort_suspended(interval,list[,keys]) => sorts keys (assumed to be all numbers or strings) and returns list with the corresponding permutation applied to it.  keys defaults to the list itself.";
2"does suspend(interval) as needed.";
3set_task_perms(caller_perms());
4interval = args[1];
5if (typeof(interval) != INT)
6return E_ARGS;
7endif
8lst = args[2];
9unsorted_keys = (use_sorted_lst = length(args) >= 3) ? args[3] | lst;
10sorted_lst = sorted_keys = {};
11for e in (unsorted_keys)
12l = this:find_insert(sorted_keys, e);
13sorted_keys[l..l - 1] = {e};
14if (use_sorted_lst)
15sorted_lst[l..l - 1] = {lst[length(sorted_keys)]};
16endif
17(ticks_left() < 4000) && suspend(interval);
18endfor
19return sorted_lst || sorted_keys;

slice

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"slice(alist[,index]) returns a list of the index-th elements of the elements of alist, e.g., ";
2"    slice({{\"z\",1},{\"y\",2},{\"x\",5}},2) => {1,2,5}.";
3"index defaults to 1 and may also be a nonempty list, e.g., ";
4"    slice({{\"z\",1,3},{\"y\",2,4}},{2,1}) => {{1,\"z\"},{2,\"y\"}}";
5{thelist, ?ind = 1} = args;
6slice = {};
7if (typeof(ind) == LIST)
8for elt in (thelist)
9s = {elt[ind[1]]};
10for i in (listdelete(ind, 1))
11s = {@s, elt[i]};
12endfor
13slice = {@slice, s};
14endfor
15else
16for elt in (thelist)
17slice = {@slice, elt[ind]};
18endfor
19endif
20return slice;

assoc

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"assoc(target,list[,index]) returns the first element of `list' whose own index-th element is target.  Index defaults to 1.";
2"returns {} if no such element is found";
3{target, thelist, ?indx = 1} = args;
4for t in (thelist)
5if (`t[indx] == target ! E_TYPE => 0')
6if ((typeof(t) == LIST) && (length(t) >= indx))
7return t;
8endif
9endif
10endfor
11return {};

iassoc

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"iassoc(target,list[,index]) returns the index of the first element of `list' whose own index-th element is target.  Index defaults to 1.";
2"returns 0 if no such element is found.";
3{target, thelist, ?indx = 1} = args;
4i = 1;
5for lsti in (thelist)
6if (`lsti[indx] == target ! E_TYPE => 0')
7if ((typeof(lsti) == LIST) && (length(lsti) >= indx))
8return i;
9endif
10endif
11i = i + 1;
12endfor
13return 0;

iassoc_suspended

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1"iassoc_suspended(target,list[,index]) returns the index of the first element of `list' whose own index-th element is target.  Index defaults to 1.";
2"returns 0 if no such element is found.";
3"suspends as needed.";
4set_task_perms(caller_perms());
5{target, thelist, ?indx = 1} = args;
6i = 1;
7for lsti in (thelist)
8if (`lsti[indx] == target ! E_TYPE => 0')
9if ((typeof(lsti) == LIST) && (length(lsti) >= indx))
10return i;
11endif
12endif
13i = i + 1;
14(ticks_left() < 4000) && suspend(1);
15endfor
16return 0;

assoc_prefix

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"assoc_prefix(target,list[,index]) returns the first element of `list' whose own index-th element has target as a prefix.  Index defaults to 1.";
2{target, thelist, ?indx = 1} = args;
3for t in (thelist)
4if ((typeof(t) == LIST) && ((length(t) >= indx) && (index(t[indx], target) == 1)))
5return t;
6endif
7endfor
8return {};

iassoc_prefix

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

none

Source

1"iassoc_prefix(target,list[,index]) returns the index of the first element of `list' whose own index-th element has target as a prefix.  Index defaults to 1.";
2{target, lst, ?indx = 1} = args;
3for i in [1..length(lst)]
4if ((typeof(lsti = lst[i]) == LIST) && ((length(lsti) >= indx) && (index(lsti[indx], target) == 1)))
5return i;
6endif
7endfor
8return 0;

iassoc_sorted

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"iassoc_sorted(target,sortedlist[,i]) => index of last element in sortedlist whose own i-th element is <= target.  i defaults to 1.";
2"  sortedlist is assumed to be sorted in increasing order and the number returned is anywhere from 0 to length(sortedlist), inclusive.";
3{target, lst, ?indx = 1} = args;
4if ((r = length(lst)) < 25)
5for l in [1..r]
6if (target < lst[l][indx])
7return l - 1;
8endif
9endfor
10return r;
11else
12l = 0;
13r = r + 1;
14while ((r - 1) > l)
15if (target < lst[i = (r + l) / 2][indx])
16r = i;
17else
18l = i;
19endif
20endwhile
21return l;
22endif

sort_alist

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1":sort_alist(alist[,n]) sorts a list of tuples by n-th (1st) element.";
2{alist, ?sort_on = 1} = args;
3if ((alist_length = length(alist)) < 25)
4"use insertion sort on short lists";
5return this:sort(alist, this:slice(@args));
6endif
7left_index = alist_length / 2;
8right_index = (alist_length + 1) / 2;
9left_sublist = this:sort_alist(alist[1..left_index], sort_on);
10right_sublist = this:sort_alist(alist[left_index + 1..alist_length], sort_on);
11"...";
12"... merge ...";
13"...";
14left_key = left_sublist[left_index][sort_on];
15right_key = right_sublist[right_index][sort_on];
16if (left_key > right_key)
17merged_list = {};
18else
19"... alist_length >= 25 implies right_index >= 2...";
20"... move right_index downward until left_key > right_key...";
21r = right_index - 1;
22while (left_key <= (right_key = right_sublist[r][sort_on]))
23if (r = r - 1)
24else
25return {@left_sublist, @right_sublist};
26endif
27endwhile
28merged_list = right_sublist[r + 1..right_index];
29right_index = r;
30endif
31while (l = left_index - 1)
32"... left_key > right_key ...";
33"... move left_index downward until left_key <= right_key...";
34while ((left_key = left_sublist[l][sort_on]) > right_key)
35if (l = l - 1)
36else
37return {@right_sublist[1..right_index], @left_sublist[1..left_index], @merged_list};
38endif
39endwhile
40merged_list[1..0] = left_sublist[l + 1..left_index];
41left_index = l;
42"... left_key <= right_key ...";
43if (r = right_index - 1)
44"... move right_index downward until left_key > right_key...";
45while (left_key <= (right_key = right_sublist[r][sort_on]))
46if (r = r - 1)
47else
48return {@left_sublist[1..left_index], @right_sublist[1..right_index], @merged_list};
49endif
50endwhile
51merged_list[1..0] = right_sublist[r + 1..right_index];
52right_index = r;
53else
54return {@left_sublist[1..left_index], right_sublist[1], @merged_list};
55endif
56endwhile
57return {@right_sublist[1..right_index], left_sublist[1], @merged_list};

sort_alist_suspended

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1"sort_alist_suspended(interval,alist[,n]) sorts a list of tuples by n-th element.  n defaults to 1.  Calls suspend(interval) as necessary.";
2set_task_perms(caller_perms());
3"... so it can be killed...";
4{interval, alist, ?sort_on = 1} = args;
5if ((alist_length = length(alist)) < 10)
6"insertion sort on short lists";
7(ticks_left() < 4000) && suspend(interval);
8return this:sort(alist, this:slice(@listdelete(args, 1)));
9endif
10"variables specially expanded for the anal-retentive";
11left_index = alist_length / 2;
12right_index = (alist_length + 1) / 2;
13left_sublist = this:sort_alist_suspended(interval, alist[1..left_index], sort_on);
14right_sublist = this:sort_alist_suspended(interval, alist[left_index + 1..alist_length], sort_on);
15left_element = left_sublist[left_index];
16right_element = right_sublist[right_index];
17merged_list = {};
18while (1)
19(ticks_left() < 4000) && suspend(interval);
20if (left_element[sort_on] > right_element[sort_on])
21merged_list = {left_element, @merged_list};
22if (left_index = left_index - 1)
23left_element = left_sublist[left_index];
24else
25return {@right_sublist[1..right_index], @merged_list};
26endif
27else
28merged_list = {right_element, @merged_list};
29if (right_index = right_index - 1)
30right_element = right_sublist[right_index];
31else
32return {@left_sublist[1..left_index], @merged_list};
33endif
34endif
35endwhile

randomly_permute

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1":randomly_permute(list) => list with its elements randomly permuted";
2"  each of the length(list)! possible permutations is equally likely";
3plist = {};
4for i in [1..length(ulist = args[1])]
5plist = listinsert(plist, ulist[i], random(i));
6endfor
7return plist;

count

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1"$list_utils:count(item, list)";
2"Returns the number of occurrences of item in list.";
3{x, xlist} = args;
4if (typeof(xlist) != LIST)
5return E_INVARG;
6endif
7counter = 0;
8while (loc = x in xlist)
9counter = counter + 1;
10xlist = xlist[loc + 1..$];
11endwhile
12return counter;

flatten

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"Copied from $quinn_utils (#34283):unroll by Quinn (#19845) Mon Mar  8 09:29:03 1993 PST";
2":flatten(LIST list_of_lists) => LIST of all lists in given list `flattened'";
3newlist = {};
4for elm in (args[1])
5if (typeof(elm) == LIST)
6newlist = {@newlist, @this:flatten(elm)};
7else
8newlist = {@newlist, elm};
9endif
10endfor
11return newlist;

longest shortest

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"Copied from APHiD (#33119):longest Sun May  9 21:00:18 1993 PDT";
2"$list_utils:longest(<list>)";
3"$list_utils:shortest(<list>)";
4"             - Returns the shortest or longest element in the list.  Elements may be either strings or lists.  Returns E_TYPE if passed a non-list or a list containing non-string/list elements.  Returns E_RANGE if passed an empty list.";
5if (typeof(all = args[1]) != LIST)
6return E_TYPE;
7elseif (all == {})
8return E_RANGE;
9else
10result = all[1];
11for things in (all)
12if ((typeof(things) != LIST) && (typeof(things) != STR))
13return E_TYPE;
14else
15result = (((verb == "longest") && (length(result) < length(things))) || ((verb == "shortest") && (length(result) > length(things)))) ? things | result;
16endif
17endfor
18endif
19return result;

check_nonstring_tell_lines

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

none

Source

1"check_nonstring_tell_lines(lines)";
2if (caller_perms().wizard)
3"don't let a nonwizard mess up our stats";
4for line in (args[1])
5if (typeof(line) != STR)
6this.nonstring_tell_lines = listappend(this.nonstring_tell_lines, callers());
7return;
8endif
9endfor
10endif

reverse_suspended

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1"reverse(list) => reversed list.  Does suspend(0) as necessary.";
2set_task_perms(caller_perms());
3"^^^For suspend task.";
4return this:_reverse_suspended(@args[1]);

_reverse_suspended

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

Source

1":_reverse(@list) => reversed list";
2set_task_perms(caller_perms());
3(ticks_left() < 4000) && suspend(0);
4if (length(args) > 50)
5return {@this:_reverse_suspended(@args[($ / 2) + 1..$]), @this:_reverse_suspended(@args[1..$ / 2])};
6endif
7l = {};
8for a in (args)
9l = listinsert(l, a);
10endfor
11return l;

randomly_permute_suspended

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

none

Source

1":randomly_permute_suspended(list) => list with its elements randomly permuted";
2"  each of the length(list)! possible permutations is equally likely";
3set_task_perms(caller_perms());
4plist = {};
5for i in [1..length(ulist = args[1])]
6plist = listinsert(plist, ulist[i], random(i));
7(ticks_left() < 4000) && suspend(0);
8endfor
9return plist;

swap_elements

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

Source

1"swap_elements -- exchange two elements in a list";
2"Usage:  $list_utils:swap_elements(<list/LIST>,<index/INT>,<index/INT>)";
3"        $list_utils:swap_elements({\"a\",\"b\"},1,2);";
4{l, i, j} = args;
5if (((typeof(l) == LIST) && (typeof(i) == INT)) && (typeof(j) == INT))
6ll = length(l);
7if (((i > 0) && (i <= ll)) && ((j > 0) && (j <= ll)))
8t = l[i];
9l[i] = l[j];
10l[j] = t;
11return l;
12else
13return E_RANGE;
14endif
15else
16return E_TYPE;
17endif

random_item random_element

Spec this none thisFlags rxdOwner #36Definer #55

Referenced by

none

Source

1"random_item -- returns a random element of the input list.";
2if (length(args) == 1)
3if (typeof(l = args[1]) == LIST)
4if (length(l) > 0)
5return l[random($)];
6else
7return E_RANGE;
8endif
9else
10return E_TYPE;
11endif
12else
13return E_ARGS;
14endif

assoc_suspended

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

none

Source

1"assoc_suspended(target,list[,index]) returns the first element of `list' whose own index-th element is target.  Index defaults to 1. Suspends as necessary.";
2"returns {} if no such element is found";
3set_task_perms(caller_perms());
4{target, thelist, ?indx = 1} = args;
5for t in (thelist)
6if (`t[indx] == target ! E_TYPE => 0')
7if ((typeof(t) == LIST) && (length(t) >= indx))
8return t;
9endif
10endif
11(ticks_left() < 4000) && suspend(0);
12endfor
13return {};

amerge

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

none

Source

1"amerge(list[,tindex[,dindex]]) returns an associated list such that all the tuples in the original list with the same tindex-th element are merged. Useful for merging alists ( amerge({@alist1, @alist2, ...}) ) and for ensuring that each tuple has a unique index. Tindex defaults to 1. Dindex defaults to 1 and refers to the position in the tuple where the tindex-th element will land in the new tuple.";
2{alist, ?tidx = 1, ?didx = 1} = args;
3if (alist)
4alist = this:sort_alist(alist, tidx);
5i = 1;
6res = {{cur = alist[1][tidx]}};
7for tuple in (alist)
8if (tuple[tidx] == cur)
9res[i] = {@res[i], @listdelete(tuple, tidx)};
10else
11if (didx != 1)
12res[i] = this:swap_elements(res[i], 1, min(didx, length(res[i])));
13endif
14i = i + 1;
15res = {@res, {cur = tuple[tidx], @listdelete(tuple, tidx)}};
16endif
17endfor
18return res;
19endif
20return alist;

build_alist

Spec this none thisFlags rxdOwner #2Definer #55

Referenced by

none

Source

1"Syntax:  build_alist(list, N) =>";
2"{list[1..N], list[N+1..N*2], list[N*2+1..N*3], ..., list[N*(N-1)+1..N*N]}";
3"";
4"Creates an associated list from a flat list at every Nth interval. If the list doesn't have a multiple of N elements, E_RANGE is returned.";
5"Example:  build_alist({a,b,c,d,e,f,g,h,i},3)=>{{a,b,c},{d,e,f},{g,h,i}}";
6{olist, interval} = args;
7if ((tot = length(olist)) % interval)
8return E_RANGE;
9endif
10nlist = {};
11d = 1;
12while (d <= tot)
13nlist = {@nlist, olist[1..interval]};
14olist[1..interval] = {};
15d = d + interval;
16endwhile
17return nlist;