Raevnos' Function Library 2.0

Raevnos' Function Library 2.0. Functions are on the objects inside this one; check their descriptions for more information.%r%rThe library is intended for PennMUSH 1.7.6 and up; parts might break with older versions. Parts will break when used with other codebases, but in this version, all the attributes should at least be settable (MUX uses a more restrictive set of rules about what can be in the first character of attribute names; version 1 of the function library included attribute names that can't be set on MUX. Other codebases might have similiar problems).

Author: Raevnos
Category: Functions
Commands: @create, @dolist, @link, @lset, @power, @set, @xa, @xb, @xc, @xd, @xe.
Features: #lambda.
Compatibility: CobraMUSH, PennMUSH.

Instructions

Copy and paste the below code into a compatible MUSH or MUX.

MUSHCode for Raevnos' Function Library 2.0

@@@@@@@@@ Function Library 2.0 @@@@@@@@@
@@ Function Library 2.0 (#243)
@create Function Library 2.0
@link Function Library 2.0 = #1580
@lock/Basic Function Library 2.0==#1622
@lset Function Library 2.0/Basic=no_inherit
@set Function Library 2.0 = VISUAL
@set Function Library 2.0 = NO_COMMAND
&DESCRIBE Function Library 2.0=Raevnos' Function Library 2.0. Functions are on the objects inside this one; check their descriptions for more information.%r%rThe library is intended for PennMUSH 1.7.6 and up; parts might break with older versions. Parts will break when used with other codebases, but in this version, all the attributes should at least be settable (MUX uses a more restrictive set of rules about what can be in the first character of attribute names; version 1 of the function library included attribute names that can't be set on MUX. Other codebases might have similiar problems).
@set Function Library 2.0/DESCRIBE=no_command visual public nearby
&SET_DBREFS Function Library 2.0=@dolist lcon(me)={@xa ##=num(my %xa); @xb ##=num(my %xb); @xc ##=num(my %xc); @xd ##=num(my %xd); @xe ##=num(my %xe)}
&XA Function Library 2.0=Predicate Functions
&XB Function Library 2.0=Transformation Functions
&XC Function Library 2.0=General Utility Functions
&XE Function Library 2.0=List Transformation and Utility Functions
List Transformation and Utility Functions
@@ List Transformation and Utility Functions (#260)
@create List Transformation and Utility Functions
@link List Transformation and Utility Functions = #1580
@lock/Basic List Transformation and Utility Functions==#1622
@lset List Transformation and Utility Functions/Basic=no_inherit
@set List Transformation and Utility Functions = VISUAL
@set List Transformation and Utility Functions = NO_COMMAND
&CAPITALIZE List Transformation and Utility Functions=u(capitalize`code, lcstr(%0), firstof(%1,%b))
&CAPITALIZE`ABOUT List Transformation and Utility Functions=u(capitalize, <string>\[, <delimiter>\]) returns string with every delimited word (Defaults to space) capitalized, and all other letters lowercased.
&CAPITALIZE`CODE List Transformation and Utility Functions=merge(regeditall(%0, (^|\\%1)\[\[:lower:\]\], $1A), ucstr(%0), A)
&DESCRIBE List Transformation and Utility Functions=The ufuns on this object take lists as arguments and do stuff to them, or are meant for use with functions like munge() and mix(). See the FUNCTIONS attribute, and FOO`ABOUT for each function FOO.
@set List Transformation and Utility Functions/DESCRIBE=no_command visual public nearby
&FUNCTIONS List Transformation and Utility Functions=capitalize grabdefault lstrlen maxn minn removeall removeallbutone transpose
&GRABDEFAULT List Transformation and Utility Functions=switch(grab(%0, %1, %3), , %2, #$)
&GRABDEFAULT_ABOUT List Transformation and Utility Functions=u(grabdefault,list,pattern,default[,delimiter]) -- Acts just like grab(), except that if the pattern doesn't match anything in the list, the the first item of the default value is returned. delimiter is optional, default is a space.
&LSTRLEN List Transformation and Utility Functions=switch(%1,,u(lstrlen`code, edit(squish(%0), `, _), %b, `), %b, u(lstrlen`code, edit(squish(%0), `, _), %b, `), `, u(lstrlen`code, edit(%0, ~, _), `, ~), u(lstrlen`code, edit(%0, `, _), %1, `))
&LSTRLEN`ABOUT List Transformation and Utility Functions=u(lstrlen, <list>[, <delimiter>]) returns a space-seperated list of the length of each word in the list. It always takes the same number of function calls, no matter the number of elements in the list.
&LSTRLEN`CODE List Transformation and Utility Functions=vsub(lpos(%0%1, %1), 0 [lpos(%2%0, %1)])
&MAXN List Transformation and Utility Functions=extract(revwords(sort(%0, %3, %2), %2), 1, %1, %2)
@set List Transformation and Utility Functions/MAXN=visual
&MAXN`ABOUT List Transformation and Utility Functions=u(maxn,<LIST>,<NUM>[,<DELIM>,<TYPE>]). Returns the NUM-most highest elements of LIST. DELIM is an optional delimiter, default is space. TYPE is also optional, it determines what kind of items are in the list, same as sort()'s second argument.
@set List Transformation and Utility Functions/MAXN`ABOUT=visual
&MINN List Transformation and Utility Functions=extract(sort(%0, %3, %2), 1, %1, %2)
@set List Transformation and Utility Functions/MINN=visual
&MINN`ABOUT List Transformation and Utility Functions=u(minn,<LIST>,<NUM>[,<DELIM>,<TYPE>]). Returns the NUM-most lowest elements of LIST. DELIM is an optional delimiter, default is space. TYPE is also optional, it determines what kind of items are in the list, same as sort()'s second argument.
@set List Transformation and Utility Functions/MINN`ABOUT=visual
&PAIR List Transformation and Utility Functions=%0%1
&REMOVEALL List Transformation and Utility Functions=squish(edit(splice(%0, u(%xc/trim, repeat(NOTAWORD[if(%2, %2, %b)], words(%0, %2)), b, %2), %1, %2), %2NOTAWORD,, NOTAWORD%2, ))
&REMOVEALL`ABOUT List Transformation and Utility Functions=u(removeall,<LIST>,<ITEM>[,<DELIM>]). Removes all occurances of <ITEM> in <LIST>. <DELIM> is optional, default is space.
&REMOVEALLBUTONE List Transformation and Utility Functions=edit(replace(splice(%0, trim(repeat(NOTAWORD[if(%2, %2, %b)], words(%0, %2)), %2), %1, %2), member(%0, %1, %2), %1, %2), %2NOTAWORD, )
&REMOVEALLBUTONE`ABOUT List Transformation and Utility Functions=u(removeallbutone,<LIST>,<ELEMENT> [,<DELIM>])). This returns LIST, will all occurances of <ELEMENT> except the first one deleted. The order of the rest of the list is preserved, unlike with set functions. DELIM is an optional delimiter, default is space.
&RPAIR List Transformation and Utility Functions=%1%0
&SORT List Transformation and Utility Functions=sort(%0, %1, %2)
&SORT_MUNGE List Transformation and Utility Functions=sort(%0,,%1)
&SORT_PIPE List Transformation and Utility Functions=sort(%0, %1, |)
&TRANSPOSE List Transformation and Utility Functions=elements(%0[repeat(%2, %1)], u(transpose`reorder, %0, ceil(fdiv(words(%0, %2), %1)), %1), %2)
@set List Transformation and Utility Functions/TRANSPOSE=visual
&TRANSPOSE`ABOUT List Transformation and Utility Functions=Transposes a matrix. u(transpose,list,columns,delimiter). u(transpose,a|b|c|d,2,|) returns a|c|b|d, for example. When the results are fed to table(), it will make the elements go down in columns, not across in rows.
@set List Transformation and Utility Functions/TRANSPOSE`ABOUT=visual
&TRANSPOSE`REORDER List Transformation and Utility Functions=vadd(sort(repeat(lnum(1, %1)%b, %2)), repeat(vmul(%1, lnum(%2))%b, %1))
&XA List Transformation and Utility Functions=#244
&XB List Transformation and Utility Functions=#245
&XC List Transformation and Utility Functions=#247
&XE List Transformation and Utility Functions=#260
@@@@@@@ Private Helper Functions @@@@@@@
@@ Private Helper Functions (#258)
@create Private Helper Functions
@link Private Helper Functions = #1580
@set Private Helper Functions = VISUAL
@set Private Helper Functions = NO_COMMAND
&DESCRIBE Private Helper Functions=The attributes on this object are private helper ufuns and data used by the other objects in the function library. They should not be called directly.
@set Private Helper Functions/DESCRIBE=no_command visual public nearby
&XA Private Helper Functions=#244
&XB Private Helper Functions=#245
&XC Private Helper Functions=#247
&XD Private Helper Functions=#258
&XE Private Helper Functions=#260
@@@@@ Character Listing Functions @@@@@@
@@ Character Listing Functions (#256)
@create Character Listing Functions
@link Character Listing Functions = #1580
@lock/Basic Character Listing Functions==#1622
@lset Character Listing Functions/Basic=no_inherit
@set Character Listing Functions = VISUAL
@set Character Listing Functions = NO_COMMAND
&DESCRIBE Character Listing Functions=The ufuns on this object generate lists of all characters the mush accepts that meet certain criteria. Hopefully they're self-explanatory.
@set Character Listing Functions/DESCRIBE=no_command visual public nearby
&GENERATE_CHARS Character Listing Functions=regraball(map(#apply/chr, lnum(%1, %2)), ^\[\[:%0:\]\]$)
&LIST_ALPHA Character Listing Functions=u(generate_chars, alpha, 1, 255)
&LIST_ASCII Character Listing Functions=u(generate_chars, ascii, 1, 127)
&LIST_DIGITS Character Listing Functions=u(generate_chars, digit, 1, 255)
&LIST_LOWERCASE Character Listing Functions=u(generate_chars, lower, 1, 255)
&LIST_PRINTABLE Character Listing Functions=u(generate_chars, print, 1, 255)
&LIST_UPPERCASE Character Listing Functions=u(generate_chars, upper, 1, 255)
&LIST_WORDCHARS Character Listing Functions=u(generate_chars, word, 1, 255)
&XA Character Listing Functions=#244
&XB Character Listing Functions=#245
&XC Character Listing Functions=#247
&XE Character Listing Functions=#260
@@@@@@ General Utility Functions @@@@@@@
@@ General Utility Functions (#247)
@create General Utility Functions
@link General Utility Functions = #1580
@lock/Basic General Utility Functions==#1622
@lset General Utility Functions/Basic=no_inherit
@set General Utility Functions = VISUAL
@set General Utility Functions = NO_COMMAND
@power General Utility Functions = See_All
&BETWEEN General Utility Functions=u(between`util, %0, 1, %1)
&BETWEEN`ABOUT General Utility Functions=u(between, <number>, <N1> <N2>[ ..., <NX>]) Returns the position <N> falls into between the rest of its arguments, which should be in sorted order. For example, u(#1838/between, 2, 1 5 10) would return 1.
&BETWEEN`UTIL General Utility Functions=switch(words(%2), >1, if(cand(gte(%0, first(%2)), lte(%0, first(rest(%2)))), %1, u(between`util, %0, inc(%1), rest(%2))), 0)
&CLEARQ General Utility Functions=setq(0,,1,,2,,3,,4,,5,,6,,7,,8,,9,,a,,b,,c,,d,,e,,f,,g,,h,,i,,j,,k,,l,,m..n..o..p,,q,,r,,s,,t,,u,,v,,w,,x,,y,,z,)
&CLEARQ`ABOUT General Utility Functions=u(clearq) sets all 36 q-registers to empty strings.
&DESCRIBE General Utility Functions=The ufuns on this object are general utility routines. Most of them are math-related. See the FUNCTIONS attribute for a list of functions, and the FOO_ABOUT attribute for help on each function FOO.
@set General Utility Functions/DESCRIBE=no_command visual public nearby
&EXPOMOD General Utility Functions=u(expomod`util, %1, 1, mod(%0, %2), %2)
&EXPOMOD`ABOUT General Utility Functions=u(expomod,<X1>,<X2>,<X3>) Equilivant to mod(power(X1,X2),X3), but handles larger values of X* than possible with the direct way.
&EXPOMOD`UTIL General Utility Functions=switch(%0, >0, u(expomod`util, shr(%0, 1), if(mod(%0, 2), mod(mul(%1, %2), %3), %1), mod(power(%2, 2), %3), %3), %1)
&FACTORIAL General Utility Functions=lmath(mul, if(gt(%0, 20), lnum(21, %0)) [extract(v(factorial`table), min(%0, 20), 1)])
@set General Utility Functions/FACTORIAL=visual
&FACTORIAL`ABOUT General Utility Functions=u(factorial,<INT>). Returns INT!. The relation 0 < INT < 171 must hold true. Anything larger results in math overflows, and anything less will return 0 or -0.
@set General Utility Functions/FACTORIAL`ABOUT=visual
&FACTORIAL`TABLE General Utility Functions=1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600 6227020800 87178291200 1307674368000 20922789888000 355687428096000 6402373705728000 121645100408832000 2432902008176640000
&FUNCTIONS General Utility Functions=between clearq expomod factorial gcd lcm ncr npr pol2xy rec2sphere sex sphere2rec trim xroot xy2pol
&GCD General Utility Functions=switch(%0, >0, u(gcd, remainder(%1, %0), %0), %1)
@set General Utility Functions/GCD=visual
&GCD`ABOUT General Utility Functions=u(gcd,<INT1>,<INT2>). Returns the greatest common divisor of INT1 and INT2 usuing Euclid's algorithm. It's a recursive implementation, so really big numbers might not work well. Both arguments should be positive integers.
@set General Utility Functions/GCD`ABOUT=visual
&LCM General Utility Functions=fdiv(mul(%0, %1), u(gcd, %0, %1))
&LCM`ABOUT General Utility Functions=u(lcm,<INT1>,<INT2>). Returns the lowest common multiple of INT1 and INT2, which should both be postive integers.
&LLOC General Utility Functions=if(findable(%!, %0), unique(iter(lnum(1, 20), rloc(%0, #@))), #-1 PERMISSION DENIED)
&MWHO General Utility Functions=mwho()
&NCR General Utility Functions=fdiv(u(factorial, %0), mul(u(factorial, %1), u(factorial, sub(%0, %1))))
&NCR`ABOUT General Utility Functions=u(ncr,N,R). Returns the number of combinations of R items from a set of N total items.
&NPR General Utility Functions=fdiv(u(factorial, %0), u(factorial, sub(%0, %1)))
&NPR`ABOUT General Utility Functions=u(npr,N,R). Returns the number of permutations of R items from a set of N total items.
&POL2XY General Utility Functions=mul(%0,cos(%1)) [mul(%0,sin(%1))]
@set General Utility Functions/POL2XY=visual
&POL2XY`ABOUT General Utility Functions=u(pol2xy,<R>,<ANGLE>). Returns a two element list, of R and ANGLE converted from Polar coordinates to Cartesian coordinates. The list is in X Y order. ANGLE should be in radians.
@set General Utility Functions/POL2XY`ABOUT=visual
&REC2SPHERE General Utility Functions=vmag(%0 %1 %2) [atan2(%1, %0)] [switch(vmag(%0 %1 %2), 0, 0, acos(fdiv(%2, #$)))]
&REC2SPHERE`ABOUT General Utility Functions=u(rec2sphere,X,Y,Z) -- Turns rectangular coordinates to spherical (Radius, angle, phi).
&SEX General Utility Functions=switch(get(%0/sex), m*, male, f*, female, p*, plural, unknown)
@set General Utility Functions/SEX=no_command visual prefixmatch
&SEX`ABOUT General Utility Functions=u(sex, *player) returns male, female, etc. depending on the object's &sex attribute, using the same rules as the functions like poss().
&SPHERE2REC General Utility Functions=mul(%0, cos(%1), sin(%2)) [mul(%0, sin(%1), sin(%2))] [mul(%0, cos(%2))]
&SPHERE2REC`ABOUT General Utility Functions=u(sphere2rec,r,theta,phi) - Turns spherical coordinates into rectangular (X Y Z)
&TRIM General Utility Functions=trimpenn(%0, %2, %1)
&TRIM`ABOUT General Utility Functions=u(trim, <string>[, <trim style>[, <trim character>]]) calls trim(), but the arguments of the ufun are independant of the tiny_trim_fun config option setting.
&XA General Utility Functions=#244
&XB General Utility Functions=#245
&XC General Utility Functions=#247
&XE General Utility Functions=#260
&XROOT General Utility Functions=root(%0, %1)
&XROOT`ABOUT General Utility Functions=u(xroot, <N>, <ROOT>) returns the ROOTth root of N. For example u(xroot, 27, 3) returns the cube root of 27. Not always accurate. :)
&XY2POL General Utility Functions=vmag(%0 %1) [atan2(%1, %0)]
&XY2POL`ABOUT General Utility Functions=u(xy2pol,<X>,<Y>). Returns a two element list, of X and Y converted from Cartesian coordinates to Polar coordinates. The first element is range, second is angle (In radians).
@set General Utility Functions/XY2POL`ABOUT=visual
@@@@@@@ Transformation functions @@@@@@@
@@ Transformation functions (#245)
@create Transformation functions
@link Transformation functions = #1580
@set Transformation functions = VISUAL
@set Transformation functions = NO_COMMAND
&CAPSTR Transformation functions=capstr(%0)
&CBRT Transformation functions=u(%xc/xroot, %0, 3)
&CEIL Transformation functions=ceil(%0)
&CHR Transformation functions=chr(%0)
&CONN Transformation functions=conn(%0)
&CUBE Transformation functions=mul(%0, %0, %0)
&DEC Transformation functions=dec(%0)
&DESCRIBE Transformation functions=The ufuns on this argument take a single-word argument, do something to it, and return the transformed result. They're meant for use with map(), foreach(), and any other place they'd be useful. Not much use since the advent of #lambda, though...
@set Transformation functions/DESCRIBE=no_command visual public nearby
&EXP Transformation functions=exp(%0)
&FLOOR Transformation functions=floor(%0)
&HOME Transformation functions=home(%0)
&IDENTITY Transformation functions=%0
&IDLE Transformation functions=idle(%0)
@set Transformation functions/IDLE=no_command prefixmatch
&INC Transformation functions=inc(%0)
&INVERSE Transformation functions=fdiv(1, %0)
&LCSTR Transformation functions=lcstr(%0)
&LN Transformation functions=ln(%0)
&LOC Transformation functions=loc(%0)
&LOG Transformation functions=log(%0)
&NAME Transformation functions=name(%0)
&NOT Transformation functions=not(%0)
&ORD Transformation functions=ord(%0)
&REVERSE Transformation functions=reverse(%0)
&ROOM Transformation functions=room(%0)
&SOUNDEX Transformation functions=soundex(%0)
&SQRT Transformation functions=sqrt(%0)
&SQUARE Transformation functions=mul(%0, %0)
&STRLEN Transformation functions=strlen(%0)
&TRUE Transformation functions=t(%0)
&TRUNC Transformation functions=trunc(%0)
&TYPE Transformation functions=type(%0)
&UCSTR Transformation functions=ucstr(%0)
&XA Transformation functions=#244
&XB Transformation functions=#245
&XC Transformation functions=#247
&XE Transformation functions=#260
@@@@@@@@@ Predicate Functions @@@@@@@@@@
@@ Predicate Functions (#244)
@create Predicate Functions
@link Predicate Functions = #1580
@lock/Basic Predicate Functions==#1622
@lset Predicate Functions/Basic=no_inherit
@set Predicate Functions = VISUAL
@set Predicate Functions = NO_COMMAND
&DESCRIBE Predicate Functions=The ufuns on this object return 1 or 0 depending on if their argument meets some criteria. They're meant for use with filter(), filterbool(), the switch-less and unnested-switch() coding styles, and any other place they'd be useful.
@set Predicate Functions/DESCRIBE=no_command visual public nearby
&ISADMIN Predicate Functions=cand(hastype(%0, player), orflags(%0, Wr))
&ISALPHA Predicate Functions=regmatch(%0, ^\[\[:alpha:\]\]+$)
&ISALPHANUM Predicate Functions=regmatch(%0, ^\[\[:alnum:\]\]+$)
&ISCONNECTED Predicate Functions=gte(conn(%0), 0)
&ISDIGIT Predicate Functions=regmatch(%0, ^\[\[:digit:\]\]+$)
&ISEVEN Predicate Functions=not(band(%0, 1))
&ISEXIT Predicate Functions=hastype(%0, exit)
&ISGOODOBJECT Predicate Functions=t(member(PLAYER THING EXIT ROOM, type(%0))
&ISGUEST Predicate Functions=haspower(%0, guest)
&ISINT Predicate Functions=isint(%0)
&ISLOWER Predicate Functions=regmatch(%0, ^\[\[:lower:\]\]+$)
&ISNUM Predicate Functions=isnum(%0)
&ISODD Predicate Functions=band(%0, 1)
&ISPLAYER Predicate Functions=hastype(%0, player)
&ISPRIME Predicate Functions=switch(0, gt(%0, 4), 1, band(%0, 1), 0, u(isprime`util, %0, 10))
&ISPRIME`ABOUT Predicate Functions=u(#244/isprime, N) returns 1 if N is a prime integer, 0 if not. It uses a probabilistic monte carlo algorithm, so when it returns 1, there is a very slight chance (Roughly once in a million) that the number is actually nonprime.
&ISPRIME`BTEST Predicate Functions=setq(0, 0)[setq(1, dec(%0))][u(isprime`btest1)][setq(2, u(%xc/expomod, %1, %q1, %0))][switch(1, %q2, 1, eq(%q2, dec(%0)), 1, u(isprime`btest2, %0, dec(%q0), dec(%0)))]
&ISPRIME`BTEST1 Predicate Functions=if(eq(mod(%q1, 2), 1), , setq(0, inc(%q0))[setq(1, shr(%q1, 1))][u(isprime`btest1)])
&ISPRIME`BTEST2 Predicate Functions=switch(1, eq(%1, 0), 0, eq(setr(2, u(%xc/expomod, %q2, 2, %0)), %2), 1, u(isprime`btest2, %0, dec(%1), %2))
&ISPRIME`UTIL Predicate Functions=switch(0, gt(%1, 0), 1, u(isprime`util2, %0), 0, u(isprime`util, %0, dec(%1)))
&ISPRIME`UTIL2 Predicate Functions=ulocal(isprime`btest, %0, first(shuffle(lnum(2, sub(%0, 2)))))
&ISPUPPET Predicate Functions=hasflag(%0, puppet)
&ISROOM Predicate Functions=hastype(%0, room)
&ISTHING Predicate Functions=hastype(%0, thing)
&ISUPPER Predicate Functions=regmatch(%0, ^\[\[:upper:\]\]+$)
&ISWHITESPACE Predicate Functions=regmatch(%0, ^\[\[:space:\]\]+$)
&ISWIZARD Predicate Functions=hasflag(%0, wizard)
&ISWORD Predicate Functions=isword(%0)
&XA Predicate Functions=#244
&XB Predicate Functions=#245
&XC Predicate Functions=#247
&XE Predicate Functions=#260