[feat] Added length count to utilities.
This commit is contained in:
parent
416ad7ac20
commit
ae416ff5c4
|
@ -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
|
||||
|
|
|
@ -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 = [
|
||||
|
|
Loading…
Reference in New Issue