-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6458394
commit af2ba3d
Showing
4 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.welambdas.lambda; | ||
|
||
import java.util.List; | ||
|
||
class LambdaArray extends LambdaInstance { | ||
private final Object[] elements; | ||
|
||
LambdaArray(int size) { | ||
super(null); | ||
elements = new Object[size]; | ||
} | ||
|
||
@Override | ||
Object get(Token name) { | ||
if (name.lexeme.equals("get")) { | ||
return new LambdaCallable() { | ||
@Override | ||
public int arity() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public Object call(Interpreter interpreter, | ||
List<Object> arguments) { | ||
int index = (int)(double)arguments.get(0); | ||
return elements[index]; | ||
} | ||
}; | ||
} else if (name.lexeme.equals("set")) { | ||
return new LambdaCallable() { | ||
@Override | ||
public int arity() { | ||
return 2; | ||
} | ||
|
||
@Override | ||
public Object call(Interpreter interpreter, | ||
List<Object> arguments) { | ||
int index = (int)(double)arguments.get(0); | ||
Object value = arguments.get(1); | ||
return elements[index] = value; | ||
} | ||
}; | ||
} else if (name.lexeme.equals("length")) { | ||
return (double) elements.length; | ||
} | ||
|
||
throw new RuntimeError(name, | ||
"Undefined property '" + name.lexeme + "'."); | ||
} | ||
|
||
@Override | ||
void set(Token name, Object value) { | ||
throw new RuntimeError(name, "Can't add properties to arrays."); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuffer buffer = new StringBuffer(); | ||
buffer.append("["); | ||
for (int i = 0; i < elements.length; i++) { | ||
if (i != 0) buffer.append(", "); | ||
buffer.append(elements[i]); | ||
} | ||
buffer.append("]"); | ||
return buffer.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#>test | ||
var arr = Array(3); | ||
|
||
var greeting = Greeter("srijan", "evening"); | ||
arr.set(1, "word"); | ||
|
||
greeting.greet(); | ||
print arr.get(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Maps { | ||
init (modulo_val) { | ||
this.modulo_val = modulo_val; | ||
} | ||
insert(number) | ||
} |