Updating to prefer nilp() over '== nil'
This commit is contained in:
parent
82696a1f61
commit
d23c849cf9
|
@ -44,8 +44,8 @@ recordp = (c) -> Object.prototype.toString.call(c) == '[object Object]'
|
|||
nilp = (c) -> !!c.isList and c.length == 0
|
||||
|
||||
cons = (a = nil, b = nil) ->
|
||||
return nil if (nilp a) and (nilp b)
|
||||
if (a) then ConsList(a, b) else ConsList(b)
|
||||
return (new ConsList()) if (nilp a) and (nilp b)
|
||||
if (a) then (new ConsList(a, b)) else (new ConsList(b))
|
||||
|
||||
car = (c) -> c[0]
|
||||
cdr = (c) -> c[1]
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{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)
|
||||
|
@ -20,6 +21,7 @@ reduce = (lst, iteratee, memo, context) ->
|
|||
null
|
||||
memo
|
||||
|
||||
|
||||
map = (lst, iteratee, context, count = 0) ->
|
||||
return nil if nilp lst
|
||||
product = iteratee.call(context, (car lst), count, lst)
|
||||
|
@ -27,6 +29,7 @@ map = (lst, iteratee, context, count = 0) ->
|
|||
map((cdr lst), iteratee, context, count + 1)
|
||||
cons product, rest
|
||||
|
||||
|
||||
rmap = (lst, iteratee, context, count = 0) ->
|
||||
ptr = lst
|
||||
ret = cons()
|
||||
|
@ -35,8 +38,10 @@ rmap = (lst, iteratee, context, count = 0) ->
|
|||
ptr = cdr ptr
|
||||
ret
|
||||
|
||||
|
||||
reverse = (lst) -> rmap lst, (i) -> i
|
||||
|
||||
|
||||
filter = (lst, iteratee, context) ->
|
||||
return nil if nilp lst
|
||||
if iteratee.call(context, (car lst), lst)
|
||||
|
@ -44,6 +49,7 @@ filter = (lst, iteratee, context) ->
|
|||
else
|
||||
filter (cdr lst), iteratee, context
|
||||
|
||||
|
||||
module.exports =
|
||||
length: length
|
||||
reduce: reduce
|
||||
|
|
Loading…
Reference in New Issue