Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save samaaron/49395 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 TypeCheckingJavaMethod("each") {
private final TypeCheckingArgumentsDefinition ARGUMENTS = TypeCheckingArgumentsDefinition
.builder()
.receiverMustMimic(runtime.list)
.withRequiredPositionalUnevaluated("indexOrArgOrCode")
.withOptionalPositionalUnevaluated("argOrCode")
.withOptionalPositionalUnevaluated("code")
.getArguments();
@Override
public TypeCheckingArgumentsDefinition getArguments() {
return ARGUMENTS;
}
@Override
public Object activate(IokeObject method, Object on, List<Object> args, Map<String, Object> keywords, IokeObject context, IokeObject message) throws ControlFlow {
getArguments().checkArgumentCount(context, message, on);
Runtime runtime = context.runtime;
List<Object> ls = ((IokeList)IokeObject.data(on)).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 on;
}
}));
BUILD SUCCESSFUL
Total time: 13 seconds
*** - couldn't find cell 'evaluateOn' on 'fn(...)' (Condition Error NoSuchCell)
evaluateOn(cell("@")) [builtin/A10_defaultBehavior.ik:18:22]
call resendToMethod("fn") do(=(activatable, true [builtin/A10_defaultBehavior.ik:24:28:in `fnx']
fnx("returns a new, unique symbol every time cal [builtin/A10_defaultBehavior.ik:159:2:in `call']
call(0) [builtin/A10_defaultBehavior.ik:161:21]
*** - couldn't find cell 'evaluateOn' on 'ISpec' (Condition Error NoSuchCell)
evaluateOn(cell("@")) [builtin/A10_defaultBehavior.ik:18:22]
ISpec do(=(Condition, Ground Condition mimic) [/Users/sam/Development/melwin/lib/ioke/ispec/conditions.ik:2:6]
*** - couldn't load module 'ispec/conditions' (Condition Error Load)
use("ispec/conditions") [/Users/sam/Development/melwin/lib/ioke/ispec.ik:4:0]
*** - couldn't load module 'ispec' (Condition Error Load)
use("ispec") [test/list_spec.ik:2:0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment