mirror of
https://github.com/zeroflag/fcl.git
synced 2025-01-12 20:01:25 +01:00
map add operation
This commit is contained in:
parent
a0cc094f05
commit
1c2f22f958
2 changed files with 9 additions and 0 deletions
|
@ -1,5 +1,6 @@
|
||||||
package com.vectron.fcl.types;
|
package com.vectron.fcl.types;
|
||||||
|
|
||||||
|
import com.vectron.fcl.exceptions.Aborted;
|
||||||
import com.vectron.fcl.exceptions.TypeMismatched;
|
import com.vectron.fcl.exceptions.TypeMismatched;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -82,6 +83,13 @@ public class Dic implements Obj {
|
||||||
this.value.put(key, value);
|
this.value.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void append(Obj lst) {
|
||||||
|
if (lst instanceof Lst && ((Lst)lst).size() == 2)
|
||||||
|
this.value.put(((Lst)lst).at(new Num(0)), ((Lst)lst).at(new Num(1)));
|
||||||
|
else
|
||||||
|
throw new Aborted("Expected list of size 2 when adding to a map, got: " + lst);
|
||||||
|
}
|
||||||
|
|
||||||
public Obj at(Obj key) {
|
public Obj at(Obj key) {
|
||||||
return value.get(key);
|
return value.get(key);
|
||||||
}
|
}
|
||||||
|
|
|
@ -782,6 +782,7 @@ public class FclTest {
|
||||||
assertEquals(3, evalPop("m size").intValue());
|
assertEquals(3, evalPop("m size").intValue());
|
||||||
assertEquals(1, evalPop("m 'a' at").intValue());
|
assertEquals(1, evalPop("m 'a' at").intValue());
|
||||||
assertEquals("b", evalPop("m 2 at").asStr().value());
|
assertEquals("b", evalPop("m 2 at").asStr().value());
|
||||||
|
assertEquals("#[ 'x' 'y' ]#", evalPop("<map> dup [ 'x' 'y' ] add").toString());
|
||||||
assertEquals(false, evalPop("m true at").boolValue());
|
assertEquals(false, evalPop("m true at").boolValue());
|
||||||
assertEquals(Nil.INSTANCE, evalPop("m 'nosuch' at"));
|
assertEquals(Nil.INSTANCE, evalPop("m 'nosuch' at"));
|
||||||
assertEquals(0, evalPop("m clear m size").intValue());
|
assertEquals(0, evalPop("m clear m size").intValue());
|
||||||
|
|
Loading…
Reference in a new issue