Skip to content

Commit

Permalink
simplify reduce primitives
Browse files Browse the repository at this point in the history
add tests for 0 and 1-length lists to reduce
  • Loading branch information
ToadKing committed Feb 28, 2019
1 parent bcc926f commit 813d860
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
30 changes: 14 additions & 16 deletions prim_list_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,11 @@ func ReduceLeftImpl(args *Data, env *SymbolTableFrame) (result *Data, err error)
return
}

length := Length(col)
if length == 0 {
list := ToArray(col)
if len(list) == 0 {
return initial, nil
}

if length == 1 {
return Car(col), nil
}

list := ToArray(col)

result, err = reduceFoldProc(list[0], list[1:], f, env, false)

return
Expand All @@ -290,17 +284,11 @@ func ReduceRightImpl(args *Data, env *SymbolTableFrame) (result *Data, err error
return
}

length := Length(col)
if length == 0 {
list := ToArray(col)
if len(list) == 0 {
return initial, nil
}

if length == 1 {
return Car(col), nil
}

list := ToArray(col)

result, err = reduceFoldProc(list[len(list)-1], list[:len(list)-1], f, env, true)

return
Expand All @@ -316,6 +304,11 @@ func FoldLeftImpl(args *Data, env *SymbolTableFrame) (result *Data, err error) {
initial := Second(args)
col := Third(args)

if !ListP(col) {
err = ProcessError("fold-left needs a list as its third argument", env)
return
}

list := ToArray(col)

result, err = reduceFoldProc(initial, list, f, env, false)
Expand All @@ -333,6 +326,11 @@ func FoldRightImpl(args *Data, env *SymbolTableFrame) (result *Data, err error)
initial := Second(args)
col := Third(args)

if !ListP(col) {
err = ProcessError("fold-right needs a list as its third argument", env)
return
}

list := ToArray(col)

result, err = reduceFoldProc(initial, list, f, env, true)
Expand Down
46 changes: 27 additions & 19 deletions tests/reduce_test.lsp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@

(context "reduce"

()

(it reduce-with-lambda
(assert-eq (reduce (lambda (acc item) 0 (+ acc item)) 0 '(1 2 3))
6))

(it reduce-with-primitive
(assert-eq (reduce + 0 '(1 2 3))
6))

(it reduce-building-a-list
(assert-eq (reduce (lambda (l i) (cons i l)) '() '(1 2 3 4))
'(4 3 2 . 1))
(assert-eq (reduce list '() '(1 2 3 4))
'(((1 2) 3) 4)))

(it reduce-errors
(assert-error (reduce r r '(1 2))) ;initial arg must be a function
(assert-error (reduce + 0 1))) ;last/3rd arg must be a list
()

(it reduce-with-lambda
(assert-eq (reduce (lambda (acc item) 0 (+ acc item)) 0 '(1 2 3))
6))

(it reduce-with-primitive
(assert-eq (reduce + 0 '(1 2 3))
6))

(it reduce-building-a-list
(assert-eq (reduce (lambda (l i) (cons i l)) '() '(1 2 3 4))
'(4 3 2 . 1))
(assert-eq (reduce list '() '(1 2 3 4))
'(((1 2) 3) 4)))

(it reduce-lengths
(assert-eq (reduce-left + 0 '()) 0)
(assert-eq (reduce-left + 0 '(1)) 1)
(assert-eq (reduce-left + 0 '(1 2)) 3)
(assert-eq (reduce-right + 0 '()) 0)
(assert-eq (reduce-right + 0 '(1)) 1)
(assert-eq (reduce-right + 0 '(1 2)) 3))

(it reduce-errors
(assert-error (reduce r r '(1 2))) ;initial arg must be a function
(assert-error (reduce + 0 1))) ;last/3rd arg must be a list

(it reduce-direction
(assert-eq (reduce-left list '() '(1 2 3 4))
Expand Down

0 comments on commit 813d860

Please sign in to comment.