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
|
nilp = (c) -> !!c.isList and c.length == 0
|
||||||
|
|
||||||
cons = (a = nil, b = nil) ->
|
cons = (a = nil, b = nil) ->
|
||||||
return nil if (nilp a) and (nilp b)
|
return (new ConsList()) if (nilp a) and (nilp b)
|
||||||
if (a) then ConsList(a, b) else ConsList(b)
|
if (a) then (new ConsList(a, b)) else (new ConsList(b))
|
||||||
|
|
||||||
car = (c) -> c[0]
|
car = (c) -> c[0]
|
||||||
cdr = (c) -> c[1]
|
cdr = (c) -> c[1]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
{car, cdr, cons, listp, pairp, nilp,
|
{car, cdr, cons, listp, pairp, nilp,
|
||||||
nil, list, listToString} = require './lists'
|
nil, list, listToString} = require './lists'
|
||||||
|
|
||||||
|
|
||||||
length = (lst, l = 0) ->
|
length = (lst, l = 0) ->
|
||||||
return l if (nilp lst)
|
return l if (nilp lst)
|
||||||
length (cdr lst), (l + 1)
|
length (cdr lst), (l + 1)
|
||||||
|
@ -20,6 +21,7 @@ reduce = (lst, iteratee, memo, context) ->
|
||||||
null
|
null
|
||||||
memo
|
memo
|
||||||
|
|
||||||
|
|
||||||
map = (lst, iteratee, context, count = 0) ->
|
map = (lst, iteratee, context, count = 0) ->
|
||||||
return nil if nilp lst
|
return nil if nilp lst
|
||||||
product = iteratee.call(context, (car lst), count, 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)
|
map((cdr lst), iteratee, context, count + 1)
|
||||||
cons product, rest
|
cons product, rest
|
||||||
|
|
||||||
|
|
||||||
rmap = (lst, iteratee, context, count = 0) ->
|
rmap = (lst, iteratee, context, count = 0) ->
|
||||||
ptr = lst
|
ptr = lst
|
||||||
ret = cons()
|
ret = cons()
|
||||||
|
@ -35,8 +38,10 @@ rmap = (lst, iteratee, context, count = 0) ->
|
||||||
ptr = cdr ptr
|
ptr = cdr ptr
|
||||||
ret
|
ret
|
||||||
|
|
||||||
|
|
||||||
reverse = (lst) -> rmap lst, (i) -> i
|
reverse = (lst) -> rmap lst, (i) -> i
|
||||||
|
|
||||||
|
|
||||||
filter = (lst, iteratee, context) ->
|
filter = (lst, iteratee, context) ->
|
||||||
return nil if nilp lst
|
return nil if nilp lst
|
||||||
if iteratee.call(context, (car lst), lst)
|
if iteratee.call(context, (car lst), lst)
|
||||||
|
@ -44,6 +49,7 @@ filter = (lst, iteratee, context) ->
|
||||||
else
|
else
|
||||||
filter (cdr lst), iteratee, context
|
filter (cdr lst), iteratee, context
|
||||||
|
|
||||||
|
|
||||||
module.exports =
|
module.exports =
|
||||||
length: length
|
length: length
|
||||||
reduce: reduce
|
reduce: reduce
|
||||||
|
|
Loading…
Reference in New Issue