diff --git a/src/env/functions/collection-access.cpp b/src/env/functions/collection-access.cpp index 501cfe7..8063f24 100644 --- a/src/env/functions/collection-access.cpp +++ b/src/env/functions/collection-access.cpp @@ -16,6 +16,35 @@ namespace blaze { void Environment::loadCollectionAccess() { + // (count '(1 2 3)) -> 3 + // (count [1 2 3]) -> 3 + // (count {:foo 2 :bar 3}) -> 2 + ADD_FUNCTION( + "count", + { + CHECK_ARG_COUNT_IS("count", SIZE(), 1); + + size_t result = 0; + if (is(begin->get()) && std::static_pointer_cast(*begin)->state() == Constant::Nil) { + // result = 0 + } + else if (is(begin->get())) { + result = std::static_pointer_cast(*begin)->size(); + } + else if (is(begin->get())) { + result = std::static_pointer_cast(*begin)->size(); + } + else { + Error::the().add(::format("wrong argument type: Collection, '{}'", *begin)); + return nullptr; + } + + // FIXME: Add numeric_limits check for implicit cast: size_t > int64_t + return makePtr((int64_t)result); + }); + + // ----------------------------------------- + // (first (list 1 2 3)) -> 1 ADD_FUNCTION( "first", diff --git a/src/env/functions/other.cpp b/src/env/functions/other.cpp index 351f4e1..57f8d1f 100644 --- a/src/env/functions/other.cpp +++ b/src/env/functions/other.cpp @@ -19,31 +19,6 @@ namespace blaze { void Environment::loadOther() { - // (count '(1 2 3)) -> 3 - // (count [1 2 3]) -> 3 - ADD_FUNCTION( - "count", - { - CHECK_ARG_COUNT_IS("count", SIZE(), 1); - - size_t result = 0; - if (is(begin->get()) && std::static_pointer_cast(*begin)->state() == Constant::Nil) { - // result = 0 - } - else if (is(begin->get())) { - result = std::static_pointer_cast(*begin)->size(); - } - else { - Error::the().add(::format("wrong argument type: Collection, '{}'", *begin)); - return nullptr; - } - - // FIXME: Add numeric_limits check for implicit cast: size_t > int64_t - return makePtr((int64_t)result); - }); - - // ----------------------------------------- - // (throw x) ADD_FUNCTION( "throw",