Add `pre` for prepending to lists

This commit is contained in:
Sage Vaillancourt 2020-10-29 11:23:35 -04:00
parent 9486854267
commit b443a54822
4 changed files with 13 additions and 1 deletions

View File

@ -192,7 +192,8 @@ struct Environment defaultEnv()
struct symFunc symFuncs[] = { struct symFunc symFuncs[] = {
{"+", &add}, {"-", &sub}, {"*", &mul}, {"/", &dvi}, {"%", &mod}, {"+", &add}, {"-", &sub}, {"*", &mul}, {"/", &dvi}, {"%", &mod},
{"=", &equ}, {">", &gth}, {"<", &lth}, {"=", &equ}, {">", &gth}, {"<", &lth},
{"cat", &catObjects}, {"fil", &filter}, {"len", &len}, {"ap", &append}, {"cat", &catObjects}, {"fil", &filter}, {"len", &len},
{"ap", &append}, {"pre", &prepend},
{"at", &at}, {"rest", &rest}, {"rev", &reverse}, {"at", &at}, {"rest", &rest}, {"rev", &reverse},
{"isnum", &isNum}, {"isnum", &isNum},
}; };

View File

@ -330,6 +330,14 @@ Object append(Object list, Object newElement, struct Environment *env)
return newList; return newList;
} }
Object prepend(Object list, Object newElement, struct Environment *env)
{
Object newList = listObject();
nf_addToList(&newList, newElement);
appendList(&newList, &list);
return newList;
}
Object at(Object index, Object list, struct Environment *env) Object at(Object index, Object list, struct Environment *env)
{ {
return cloneObject(*itemAt(&list, index.number)); return cloneObject(*itemAt(&list, index.number));

View File

@ -50,6 +50,7 @@ BASIC_OP(gth); BASIC_OP(lth);
Object catObjects(const Object obj1, const Object obj2, struct Environment *env); Object catObjects(const Object obj1, const Object obj2, struct Environment *env);
Object filter(Object obj1, Object obj2, struct Environment *env); Object filter(Object obj1, Object obj2, struct Environment *env);
Object append(Object list, Object newElement, struct Environment *env); Object append(Object list, Object newElement, struct Environment *env);
Object prepend(Object list, Object newElement, struct Environment *env);
Object at(Object index, Object list, struct Environment *env); Object at(Object index, Object list, struct Environment *env);
Object rest(Object list, Object ignore, struct Environment *env); Object rest(Object list, Object ignore, struct Environment *env);
Object reverse(Object _list, Object ignore, struct Environment *ignore2); Object reverse(Object _list, Object ignore, struct Environment *ignore2);

View File

@ -107,6 +107,8 @@ check "ListIndex" "(at (+ 1 1) (1 2 1000 4 5))" "1000"
check "EvalElems" "((* 10 10) 7)" "( 100 7 )" check "EvalElems" "((* 10 10) 7)" "( 100 7 )"
check "AppendToList" "(ap (1 20 300 4000 50000) 600000)" "( 1 20 300 4000 50000 600000 )" check "AppendToList" "(ap (1 20 300 4000 50000) 600000)" "( 1 20 300 4000 50000 600000 )"
check "AppndToEnvList" "(def l (50000 4000 300 20)) (def l (ap l 1)) l" "( 50000 4000 300 20 1 )" check "AppndToEnvList" "(def l (50000 4000 300 20)) (def l (ap l 1)) l" "( 50000 4000 300 20 1 )"
check "PrependToList" "(pre (1 20 300 4000 50000) 600000)" "( 600000 1 20 300 4000 50000 )"
check "PrepndToEnvList" "(def l (50000 4000 300 20)) (def l (pre l 1)) l" "( 1 50000 4000 300 20 )"
check "Rest(Tail)OfList" "(def l (50000 4000 300 20)) (rest l)" "( 4000 300 20 )" check "Rest(Tail)OfList" "(def l (50000 4000 300 20)) (rest l)" "( 4000 300 20 )"
check "ListLength" "(def l (1 20 3 'abc' 'banana' (+ 10 5))) (len l)" "6" check "ListLength" "(def l (1 20 3 'abc' 'banana' (+ 10 5))) (len l)" "6"
endBlock endBlock