Skip to content

Instantly share code, notes, and snippets.

@samaaron
Created January 20, 2009 20:55
Show Gist options
  • Select an option

  • Save samaaron/49658 to your computer and use it in GitHub Desktop.

Select an option

Save samaaron/49658 to your computer and use it in GitHub Desktop.
obj.registerMethod(runtime.newJavaMethod("takes either one or two or three arguments. if one argument is given, it should be a message chain that will be sent to each object in the list. the result will be thrown away. if two arguments are given, the first is an unevaluated name that will be set to each of the values in the list in succession, and then the second argument will be evaluated in a scope with that argument in it. if three arguments is given, the first one is an unevaluated name that will be set to the index of each element, and the other two arguments are the name of the argument for the value, and the actual code. the code will evaluate in a lexical context, and if the argument name is available outside the context, it will be shadowed. the method will return the list.", new JavaMethod("each") {
private final DefaultArgumentsDefinition ARGUMENTS = DefaultArgumentsDefinition
.builder()
.withRequiredPositionalUnevaluated("indexOrArgOrCode")
.withOptionalPositionalUnevaluated("argOrCode")
.withOptionalPositionalUnevaluated("code")
.getArguments();
@Override
public DefaultArgumentsDefinition getArguments() {
return ARGUMENTS;
}
@Override
public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
getArguments().checkArgumentCount(context, message, on);
Runtime runtime = context.runtime;
Object onAsList = on;
if(!(IokeObject.data(onAsList) instanceof IokeList)) {
onAsList = IokeObject.convertTo(context.runtime.list, onAsList, true, null, message, context);
}
List<Object> ls = ((IokeList)IokeObject.data(onAsList)).list;
switch(message.getArgumentCount()) {
case 1: {
IokeObject code = IokeObject.as(message.getArguments().get(0));
for(Object o : ls) {
code.evaluateCompleteWithReceiver(context, context.getRealContext(), o);
}
break;
}
case 2: {
LexicalContext c = new LexicalContext(context.runtime, context, "Lexical activation context for List#each", message, context);
String name = IokeObject.as(message.getArguments().get(0)).getName();
IokeObject code = IokeObject.as(message.getArguments().get(1));
for(Object o : ls) {
c.setCell(name, o);
code.evaluateCompleteWithoutExplicitReceiver(c, c.getRealContext());
}
break;
}
case 3: {
LexicalContext c = new LexicalContext(context.runtime, context, "Lexical activation context for List#each", message, context);
String iname = IokeObject.as(message.getArguments().get(0)).getName();
String name = IokeObject.as(message.getArguments().get(1)).getName();
IokeObject code = IokeObject.as(message.getArguments().get(2));
int index = 0;
for(Object o : ls) {
c.setCell(name, o);
c.setCell(iname, runtime.newNumber(index++));
code.evaluateCompleteWithoutExplicitReceiver(c, c.getRealContext());
}
break;
}
}
return onAsList;
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment