From ae416ff5c427a072948d050682f5e70b6e0fa8de Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Wed, 22 Jul 2015 21:05:20 -0700 Subject: [PATCH] [feat] Added length count to utilities. --- src/reduce.coffee | 6 ++++++ test/reduce.coffee | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/reduce.coffee b/src/reduce.coffee index 167f96f..ce0b894 100644 --- a/src/reduce.coffee +++ b/src/reduce.coffee @@ -1,6 +1,11 @@ {car, cdr, cons, listp, pairp, nilp, nil, list, listToString} = require './lists' +length = (lst, l = 0) -> + return l if (nilp lst) + length (cdr lst), (l + 1) + + reduce = (lst, iteratee, memo, context) -> count = 0 console.log lst @@ -40,6 +45,7 @@ filter = (lst, iteratee, context) -> filter (cdr lst), iteratee, context module.exports = + length: length reduce: reduce map: map rmap: rmap diff --git a/test/reduce.coffee b/test/reduce.coffee index ff5dcad..46a1e01 100644 --- a/test/reduce.coffee +++ b/test/reduce.coffee @@ -4,10 +4,26 @@ expect = chai.expect {listToVector, vectorToList, listToString, cons, list, nil} = require '../src/lists' -{map, reduce, filter, reverse} = require '../src/reduce' +{map, reduce, filter, reverse, length} = require '../src/reduce' id = (item) -> item +describe 'Length Testing', -> + + samples = [ + [cons(), 0] + [cons(nil), 0] + [cons('a'), 1] + [cons('a', cons('b')), 2] + [cons('a', cons('b', cons('c'))), 3] + [cons('a', cons('b', cons('c'), nil)), 3]] + + for [t, v] in samples + do (t, v) -> + it "should produce a length of #{v}", -> + product = length(t) + expect(product).to.equal(v) + describe 'Map Identity Testing', -> samples = [