Skip to content

Instantly share code, notes, and snippets.

View apmckinlay's full-sized avatar

Andrew McKinlay apmckinlay

View GitHub Profile
@apmckinlay
apmckinlay / gist:3701451
Created September 11, 2012 19:38
old way of implementing built-in methods
public static class EqualRange extends SuMethod2 {
{ params = new FunctionSpec(array("value", "block"), Boolean.FALSE); }
@Override
public Object eval2(Object self, Object a, Object b) {
Range r = ((SuContainer) self).equalRange(a, b);
return SuContainer.of(r.left, r.right);
}
}
@apmckinlay
apmckinlay / gist:3701471
Created September 11, 2012 19:42
built-in method using MethodHandle behind the scenes
@Params("value, block=false")
public static Object EqualRange(Object self, Object a, Object b) {
Range r = ((SuContainer) self).equalRange(a, b);
return SuContainer.of(r.left, r.right);
}
@apmckinlay
apmckinlay / gist:4277990
Created December 13, 2012 17:05
One method for creating a comma separated list.
for (int i = 0; i < array.length; ++i) {
if (i > 0)
sb.append(",");
sb.append(fn(array[i]));
}
return sb.toString();
@apmckinlay
apmckinlay / gist:4278013
Created December 13, 2012 17:08
One method for creating a comma separated list.
if (list.isEmpty())
return "";
for (Object x : list)
sb.append(fn(x)).append(",");
return sb.deleteCharAt(sb.length() - 1).toString();
@apmckinlay
apmckinlay / gist:4278026
Created December 13, 2012 17:10
One method for creating a comma separated list.
if (list.isEmpty())
return "";
for (Object x : list)
sb.append(",").append(fn(x));
return sb.substring(1);
@apmckinlay
apmckinlay / CommaStringBuilder
Created December 13, 2012 17:14
A utility class to build comma separated lists.
public class CommaStringBuilder implements Appendable {
private final StringBuilder sb;
private boolean first = true;
public CommaStringBuilder() {
sb = new StringBuilder();
}
public CommaStringBuilder(String s) {
sb = new StringBuilder(s);
@apmckinlay
apmckinlay / gist:4278101
Created December 13, 2012 17:23
An example of using CommaStringBuilder
CommaStringBuilder csb = new CommaStringBuilder("(");
for (Object x : list)
csb.add(fn(x));
return csb.append(")").toString();
getLibsNames(libs)
{
list = Object()
for (i = 0; i < libs.Size(); ++i)
{
lib = libs[i]
li = i.Pad(2)
for x in QueryList(lib $ " where group = -1", "name")
{
list.Add(x.Tr("_").Lower() $ "=" $ x $ ":" $ li)
max_matches: 30
GetMatches(prefix)
{
list = Suneido.LibLocate.list
prefix = prefix.Tr("_").Lower()
from = list.LowerBound(prefix)
to = Min(from + .max_matches, list.LowerBound(prefix.RightTrim() $ "~"))
matches = list[from .. to]
matches.Map!({ it.AfterFirst("=") })
matches.Sort!().Unique!()
// Package float10 implements decimal floating point numbers
// using uint64 to hold the coefficient.
package float10
import "strconv"
import "errors"
import "strings"
import "math"
// value is -1^sign * coef * 10^exp