Useful functions - Gorlem/functions-module GitHub Wiki
This page lists useful functions, ready for you to include in your own scripts.
Functions used in the examples
function print(&message)
log(%&message%)
endfunction
function pow(#base,#exponent=2)
#result = #base
for(#i,2,%#exponent%)
#result = #result * #base
next
return(%#result%)
endfunction
function greater10(#number)
if(#number > 10)
return(true)
endif
return(false)
endfunction
Basic data creation
// Creates an array with the specified values in one line
function array(...&values[])
return(&values[])
endfunction
#array1[] = array(1,2,3,4,5)
// => [1, 2, 3, 4, 5]
&array2[] = array("One","Two","Three")
// => ["One", "Two", "Three"]
// Creates an array filled with values between #start and #stop (both inclusive),
// where the next value is increased by #step
function range(#start,#stop=-1,#step=1)
if(#stop == -1)
#stop = #start
#start = 0
endif
for(#i = %#start% to %#stop% step %#step%)
&result[] = %#i%
next
return(&result[])
endfunction
#range1[] = range(5)
// => [0, 1, 2, 3, 4, 5]
#range2[] = range(5,10)
// => [5, 6, 7, 8, 9, 10]
#range3[] = range(0,10,3)
// => [0, 3, 6, 9]
// Creates an array of the size #amount filled with the value of &filler
function fill(#amount,&filler)
for(#i,1,%#amount%)
&result[] = %&filler%
next
return(&result[])
endfunction
&fill1[] = fill(5)
// => ["", "", "", "", ""]
#fill2[] = fill(3,0)
// => [0, 0, 0]
Functional data manipulation
// Iterates over the &array[] and calls the &function for each entry
function each(&array[],&function)
foreach(&array[],&entry)
call(%&function%,%&entry%)
next
endfunction
#array[] = range(0,3)
each(#array[],print)
// Chat log:
// 0
// 1
// 2
// 3
// Iterates over the &array[] and assigns each entry the result of the &function
function select(&array[],&function)
foreach(&array[],&entry,#index)
&array[%#index%] = call(%&function%,%&entry%)
next
return(&array[])
endfunction
#array[] = range(0,3)
#map[] = select(#array[],pow)
// => [0, 1, 4, 9]
// Iterates over the &array[] and filters out every entry where the &function returns false
function where(&array[],&function)
foreach(&array[],&entry)
result = call(%&function%,%&entry%)
if(result)
&result[] = %&entry%
endif
next
return(&result[])
endfunction
#array[] = range(0,20,5)
#where[] = where(#array[],greater10)
// => [15, 20]
// Iterates over the &instructions[] and calls a specified function with the &array[]
// as the first argument and whatever you set after the arrow (->) as the second argument
// A instruction usually looks like this: `each->print`
function chain(&array[],...&instructions[])
foreach(&instructions[],&instruction)
&out[] = split("->",%&instruction%)
&array[] = call(%&out[0]%,&array[],%&out[1]%)
next
return(&array[])
endfunction
#array[] = range(0,5)
chain(#array[],"select->pow","where->greater10","each->print")
// Chat log:
// 16
// 25
#chain[] = chain(#array[],"select->pow","select->pow")
// => [0, 1, 16, 81, 256, 625]
// Returns the first non-empty entry in the &array[]
function first(&array[])
foreach(&array[],&entry)
if(&entry != "")
return(%&entry%)
endif
next
endfunction
&array[] = fill(3)
&array[] = "First"
&array[] = "Last"
&first = first(&array[])
// => "First"
// Returns the last entry in the &array[]
function last(&array[])
#size = arraysize(&array[])
#index = #size - 1
return(%&array[%#index%]%)
endfunction
&array[] = fill(3,"Not last")
&array[] = "Last"
&last = last(&array[])
// => "Last"
// Returns a new array based on &array[], without the first #amount entries
function skip(&array[],#amount)
foreach(&array[],&entry,#index)
if(#index >= #amount)
&result[] = %&entry%
endif
next
return(&result[])
endfunction
#array = range(0,5)
#skip[] = skip(#array[],3)
// => [3, 4, 5]
// Returns a new array based on &array, with only the first #amount entries
function take(&array[],#amount)
foreach(&array[],&entry,#index)
if(#index >= #amount)
break
endif
&result[] = %&entry%
next
return(&result[])
endfunction
#array[] = range(0,5)
#take[] = take(#array[],3)
// => [0, 1, 2]