if (word(2 $loadinfo()) != [pf]) { load -pf $word(1 $loadinfo()); return; }; unload functions; package functions; # # Some misc functions.. for 2.2 and beyond. # Originally this file contained aliases written by: # by: Ian Frechette (frechett@spot.colorado.edu) # Most of which have been subsumed into epic itself. # There are a great many new aliases written by crazyeddy, # # # These have been updated to handle multiple words. # format and lformat differ from $[-num]var and $[num]var in that # They don't chop off the string if it is too long. # These are obsoleted by the built in $pad() function. # alias format { @ IRCII.word = [$1-]; if (@IRCII.word < [$0]) { @ function_return = [$([-$0]IRCII.word)]; } { @ function_return = IRCII.word; }; }; alias lformat { @ IRCII.word = [$1-]; if (@IRCII.word < [$0]) { @ function_return = [$([$0]IRCII.word)]; } { @ function_return = IRCII.word; }; }; # pluck a word from a list. # eg. $blah == "one two three four five" # $pluck(blah three) returns "one two four five" # alias pluck { @ function_return = $0 = $remw($1 $($0)); }; # These map some of the original functions to their EPIC names. */ alias notword { @ function_return = notw($*); }; alias remove { @ function_return = remw($*); }; # $min() $max(). fe (min leftw max rightw) foo bar { alias $foo return \$$bar\(1 \$numsort($*)\); }; alias minmax (args) return $min($rightw($shift(args) $numsort($args))) alias maxmin (args) return $max($leftw($shift(args) $numsort($args))) # $regpattern() and $regfilter() work exactly like $pattern() # and $filter except that they use regexes rather than wildcards. stack push alias alias.x; alias alias.x (args) fe (filter ! pattern !!) func cond { @ :argz = args; alias $msar(gr/\${func}/$func/\${cond}/$cond/argz); }; alias.x reg\${func} (ret) { @:reg=regcomp($shift(ret)); fe ret foo { @ foo = ${cond}regexec($reg $foo) ? [] : foo=~[* *] ? ["\$foo"] : foo; }; @regfree($reg); return $ret; }; stack pop alias alias.x; # A version of $indextoword() that ignores double quoting. alias indextowword {return $indextoword($tr(/"/_/$*));}; alias wwordtoindex {return $wordtoindex($tr(/"/_/$*));}; # Character jot. # $jotc(afWZ) returns abcdefWXYZ. alias jotc {return $chr($jotm($ascii($*)));}; alias jotm (ret) { fe ret foo bar { @ foo = jot($foo $bar); @ bar = []; }; return $ret; }; # A version of $mask() that operates with multiple args. alias maskm (mask,args) { fe args arg { @ arg = mask($mask $arg); }; return $args; }; # $randn( rep val ) returns rep random numbers up to val. alias randn (args) { @ :rep = isnumber(b10 $args) ? shift(args) : 1; @ :val = isnumber(b10 $args) ? shift(args) : 0; eval return$repeat($rep \$rand\($val\)); }; # $replace(#x# x $jot(0 3)) returns #0# #1# #2# #3#. alias replace (args) { @ :xin = urlencode($shift(args)); @ :rep = urlencode($shift(args)); fe args arg { @ in = xin; @ arg = urldecode($sar(gr/$rep/$urlencode($arg)/in)); }; return $args; }; # Shuffle functions shuffle their argument lists, except for shufflec, which # shuffles all the characters in $*. The s in shuffles (now) stands for # "short". The f in shufflef stands for "fast". The c in shufflec stands for # "char". They are all "imperfect" due to the fact that $shuffles() is # imperfect. The short version is actually faster than the fast for smaller # arg lists. # # The d in shuffled() stands for "dual" and it appears to be a perfect shuffle, # though that could possibly be due to the shuffling happening twice more than # anything else. # # The 1 in shuffle1 means one pass shuffle. You need to call this multiple # times on the data to have a chance at an unbiased shuffle. # # The 2 means randomly select only half the elements. 2n returns 1/$0 # arguments from $1- and 2m returns 1-1/$0 arguments from $1-. # # $shuffle1r() puts a greater effort into shuffling the data at the beginning # of the list while returning the poorly shuffled end of the list too. $0 # indicates how many elements at the beginning of the list need to be properly # shuffled and should be overestimated. This function is meant to be a # comprimise between speed and bias. # alias randsel (args) { @ :foo = randn($#args $shift(args));return $copattern(0 foo args); }; alias shufflec { return $chr($shuffles($ascii($*))); }; alias shuffle1 (args) { @ :foo = randn($#args 2);return $copattern(0 foo args) $cofilter(0 foo args); }; alias shuffle1r (min,args) { if (2*min >= #args) {return $shuffles($args)} {@ :foo=randn($#args 2);return $shuffle1r($min $copattern(0 foo args)) $cofilter(0 foo args)}; }; alias shuffle2 (args) { @ :foo = randn($#args 2);return $cofilter(0 foo args); }; alias shuffle2m (rnd,args) { @ :foo = randn($#args $rnd);return $cofilter(0 foo args); }; alias shuffle2n (rnd,args) { @ :foo = randn($#args $rnd);return $copattern(0 foo args); }; alias shufflef (args uwords 1) { @ :foo = randn($#args 2); if (#args < 128) { return $shuffled($copattern(0 foo args)) $shuffled($cofilter(0 foo args)); } else { return $shufflef($copattern(0 foo args)) $shufflef($cofilter(0 foo args)); }; }; alias shuffles (args uwords 1) { @ :c = #args; @ :ret = []; if (c <= 64) { repeat $c { @ splice(ret $rand(${++:w}) 0 $splice(args $rand(${c--}) 1)); }; } elsif (128 <= c) { @ :foo = randn($c 2); return $shuffles($copattern(0 foo args)) $shuffles($cofilter(0 foo args)); } elsif (xdebug(extractw) =~ [-*]) { fe ($args) foo { @ splice(ret $rand(${++:w}) 0 $foo); }; } else { @ :space = chr($jot(1 32)); fe ($args) foo { @ splice(ret $rand(${++:w}) 0 ${0>index("$space" $foo)?foo:["$foo"]}); }; }; return $ret; }; alias shuffled (args uwords 1) { @ :argc = 1 + #args; @ :ret = []; repeat $argc { @ splice(ret $rand(${++:w}) 0 $splice(args $rand(${argc--}) 1)); }; return $ret; }; # # $shuffle() is an attempt to prove that the $shufflef() algorithm works # properly. Not quite as fast or neat, but it's a "perfect" shuffle in that # all results have equal chance of occuring. Incrementing the 2 in $randn() # doesn't change this. # alias shuffle (args) { @ :argc =#args; switch ($argc) { (0) (1) {return $args;}; (2) {return ${rand(2) ? args : [$pop(args) $args]};}; (*) { do {@ :rand = randn($argc 2);} until (match(0 $:rand) && match(1 $:rand)); return $shuffle($copattern(0 rand args)) $shuffle($cofilter(0 rand args)); }; }; }; # A lot more like the shell version of "uniq -d" than $uniq(). # $uniqd() removes unique items in a list. alias uniqd (ret) { fe ret foo { if (last!=foo) { @ :last = foo; @ :foo = []; }; }; return $ret; }; alias uniqn (num,args) { repeat $num @ args = uniqd($args); return $args; }; alias uniqc (num,args) { @ :ret = uniq($args); fe ret foo { @ :count = #findws($foo $args); @ foo = num > count ? [] : [$count:$foo]; }; return $ret; }; # $wordn(0.9.2,9.0.2 $jot(0 9)) returns "0 2 4 6 8 9 7 5 3 1". alias wordn (num,args) { @ :num = split(, $num); fe num foo { @ :foo = foo =~ [*.*] ? jot($split(. $foo)) : foo; }; fe num foo { @ foo = word($foo $args); }; return $num; }; # $cut(0.9.2,9.0.2 : $unsplit(: $jot(0 9))) returns "0:2:4:6:8:9:7:5:3:1". alias cut (num,sep,args) { switch ($num) { (*,*) { @ :mid = (count(, $num) + 1) / 2; return $cut($before($mid , $num) $sep $args)$sep$cut($after($mid , $num) $sep $args); } (*.*) { return $cut($unsplit(, $jot($split(. $num))) $sep $args); } (*) { return $before(1 $sep ${num ? after($num $sep $args) : args}$sep); } }; }; # Splice characters. The bless is so it can be called on local vars. alias splicec (var,start,count,replace) { bless; ^assign $var $ascii($($var)); @ function_return = chr($splice($var $start $count $ascii($replace))); ^assign $var $chr($($var)); }; # Bit of a hack here. Return all args with spacing removed. alias words { return $unsplit(" " $*); }; # $chvoices() returns the voiced users on a channel. # $chnovoices() is the compliment. stack push alias alias.x; alias alias.x (args) { fe (chhops 0 ishalfop nochhops 1 ishalfop chvoices 0 ischanvoice chnovoices 1 ischanvoice) cmd val fn { @ :argz = args; //alias $msar(gr/\$cmd/$cmd/\$val/$val/\$fn/$fn/argz); }; }; alias.x \$cmd (args) { @ :chan = shift(args); @ :ret = chanusers($chan); fe ret user { @ user = $val != $fn($user $chan) ? user : []; }; return $ret; }; stack pop alias alias.x; alias mychats (args) { @ :function_return = dccctl(typematch chat); fe function_return dcc {@ :dcc = [=]##dccctl(get $dcc user)}; };