~tim/lydia

831c58da83f2e1efb915173fc2e21adbeed2b785 — Tim Morgan 8 years ago 199c6f6
Refactor the core library a bit.
5 files changed, 21 insertions(+), 17 deletions(-)

M lib/core/list.lid
M src/funcs.c
M src/lib/list.c
M src/lib/list.h
M test/list.lid
M lib/core/list.lid => lib/core/list.lid +9 -1
@@ 21,10 21,18 @@ join = [l s] { c = count l
                   c == 1 { str (first l) },
                          { ((str (first l)) + s) + (join (rest l) s) }] }


# given f (from) and t (to), return a list of consecutive numbers in between
.. = [f t] { if f == t,
.. = [f t] { if f >= t,
                [t],
                { [f] + ((f + 1) .. t) } }

# takes a list and two indices, returns sub-list of values in the range
slice = [l s e] { map (s .. e) [i] { l -> i } }

# infix form of slice, e.g. ['a' 'b' 'c'] // [1 2] => ['b' 'c']
// = [l i] { slice l (i -> 0) (i -> ((count i) - 1)) }

first = [l] { l -> 0 }

last = [l] { l -> ((count l) - 1) }

M src/funcs.c => src/funcs.c +0 -1
@@ 136,7 136,6 @@ void l_create_funcs(LClosure *closure) {
  l_insert_func("if",      l_func_if,        closure);
  l_insert_func("while",   l_func_while,     closure);
  l_insert_func("count",   l_func_count,     closure);
  l_insert_func("first",   l_func_first,     closure);
  l_insert_func("rest",    l_func_rest,      closure);
  l_insert_func("==",      l_func_eq,        closure);
  l_insert_func("!=",      l_func_neq,       closure);

M src/lib/list.c => src/lib/list.c +0 -8
@@ 24,14 24,6 @@ LValue *l_func_count(LValue *args, LClosure *closure) {
  return value;
}

LValue *l_func_first(LValue *args, LClosure *closure) {
  LValue *list = l_list_get(args, 0);
  l_assert_is(list, L_LIST_TYPE, L_ERR_MISSING_LIST, closure);
  LValue *value = l_list_get(list, 0);
  if(value == NULL) value = l_value_new(L_NIL_TYPE, closure);
  return value;
}

LValue *l_func_rest(LValue *args, LClosure *closure) {
  LValue *list = l_list_get(args, 0);
  l_assert_is(list, L_LIST_TYPE, L_ERR_MISSING_LIST, closure);

M src/lib/list.h => src/lib/list.h +0 -1
@@ 6,7 6,6 @@ bool l_list_eq(LValue *l1, LValue *l2);
LValue *l_list_get(LValue *list, long index);
LValue *l_func_list_get(LValue *args, LClosure *closure);
LValue *l_func_count(LValue *args, LClosure *closure);
LValue *l_func_first(LValue *args, LClosure *closure);
LValue *l_func_rest(LValue *args, LClosure *closure);
LValue *l_func_list_add(LValue *args, LClosure *closure);


M test/list.lid => test/list.lid +12 -6
@@ 1,14 1,20 @@
require "lib/extra/test.lid"

suite "LIST TESTS",
      ["count list"       { test (count [9]) eq 1 }],
      ["count empty list" { test (count []) eq 0 }],
      ["filter list"      { l = filter [1 2 3] [x] { x == 2 }
      ["count"            { test (count [9]) eq 1 }],
      ["count empty"      { test (count []) eq 0 }],
      ["filter"           { l = filter [1 2 3] [x] { x == 2 }
                            test l eq [2] } ],
      ["map list"         { l = map [1 2 3] [x] { x * 2 }
      ["map"              { l = map [1 2 3] [x] { x * 2 }
                            test l eq [2 4 6] } ],
      ["slice list"       { l = slice [1 2 3 4] 1 2
      ["slice"            { l = slice [1 2 3 4] 1 2
                            test l eq [2 3] } ],
      ["empty compare"    { test [] eq [] }],
      ["compare =="       { test [1] eq [1] }],
      ["compare !="       { test [2] ne [1] }]
      ["compare !="       { test [2] ne [1] }],
      ["first of empty"   { test (first []) eq nil }],
      ["index 0 of empty" { test ([] -> 0) eq nil }],
      ["index past end"   { test ([1] -> 1) eq nil }],
      ["rest"             { test (rest [1 2 3]) eq [2 3] }],
      ["rest of single"   { test (rest [1]) eq [] }],
      ["rest of empty"    { test (rest []) eq [] }]