Skip to content

Commit

Permalink
implemented Length() for associations
Browse files Browse the repository at this point in the history
Task #130 - teach Length() to work with Association
  • Loading branch information
grzegorzmazur committed Oct 5, 2015
1 parent f5de44b commit e85bc21
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions JavaYacas/net/sf/yacas/MathCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -1474,10 +1474,12 @@ public void Eval(LispEnvironment aEnvironment,int aStackTop) throws Exception
}
GenericClass gen = ARGUMENT(aEnvironment, aStackTop, 1).Get().Generic();
if (gen != null)
if (gen.TypeName().equals("\"Array\""))
{
if (gen.TypeName().equals("\"Array\"")) {
int size=((ArrayClass)gen).Size();
RESULT(aEnvironment, aStackTop).Set(LispAtom.New(aEnvironment,""+size));
} else if (gen.TypeName().equals("\"Association\"")) {
int size=((AssociationClass)gen).Size();
RESULT(aEnvironment, aStackTop).Set(LispAtom.New(aEnvironment,""+size));
}
// CHK_ISLIST_CORE(aEnvironment,aStackTop,ARGUMENT(aEnvironment, aStackTop, 1),1);
}
Expand Down
2 changes: 2 additions & 0 deletions src/mathcommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ void LispLength(LispEnvironment& aEnvironment, LispInt aStackTop)
size = ARGUMENT(1)->String()->size()-2;
} else if (ArrayClass* arr = dynamic_cast<ArrayClass*>(ARGUMENT(1)->Generic())) {
size = arr->Size();
} else if (AssociationClass* assoc = dynamic_cast<AssociationClass*>(ARGUMENT(1)->Generic())) {
size = assoc->Size();
} else
CheckArg(false, 1, aEnvironment, aStackTop);

Expand Down
4 changes: 4 additions & 0 deletions tests/association.yts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ NextTest("Association");
a := Association'Create();

Verify(Association'Size(a), 0);
Verify(Length(a), 0);

Verify(Association'Set(a, x, y), True);
Verify(Association'Size(a), 1);
Verify(Length(a), 1);
Verify(Association'Get(a, x), y);
Verify(Association'Drop(a, x), True);
Verify(Association'Drop(a, x), False);
Verify(Association'Get(a, x), Undefined);
Verify(Association'Size(a), 0);
Verify(Length(a), 0);
];

0 comments on commit e85bc21

Please sign in to comment.