;; Racket
(check-expect (deepmap identity '()) '())
(check-expect (deepmap add1 '(1 2 3 4)) '(2 3 4 5))
(check-expect (deepmap add1 '(() 1)) '(() 2))
(check-expect (deepmap add1 '(1 (2 3) 4)) '(2 (3 4) 5))
(define (greet person) (format "hello ~a" person))
(check-expect (deepmap greet '("bob" "mary" "alice"))
'("hello bob" "hello mary" "hello alice"))
(check-expect (deepmap greet '(("bob" ("mary")) "alice"))
'(("hello bob" ("hello mary")) "hello alice"))
(check-expect (deepmap list '(1 2 3 4)) '((1) (2) (3) (4)))
(check-expect (deepmap list '((1) ((2) (3)) (4)))
'(((1)) (((2)) ((3))) ((4))))
(check-expect (deepmap (lambda (x) (* 2 x))
(build-list 7 (lambda (j) (make-list j j))))
(list '() (list 2) (list 4 4) (list 6 6 6) (list 8 8 8 8)
(list 10 10 10 10 10) (list 12 12 12 12 12 12)))
// JS
test("add", (t)=> {
let zipper = new Zipper((a,b)=>a+b, [1,2,3], [4,5,6]);
assert.deepStrictEqual(Array.from(zipper),[5,7,9]);
});
test("length mismatch", (t)=> {
let zipper = new Zipper((a,b)=>a+b, [1,2,3], [4,5,6,7]);
assert.deepStrictEqual(Array.from(zipper),[5,7,9]);
});
test("indexing", (t) => {
let zipper = new Zipper((a,b)=>a[b], [[1,2,3],[4,5],[2,3]], [2,0,1]);
assert.deepStrictEqual(Array.from(zipper),[3,4,3]);
});
test("strings", (t) => {
let zipper = new Zipper((a,b)=>a.substring(0,b),
["Alice", "Bob", "Mallory"],
[2,1,3]);
assert.deepStrictEqual(Array.from(zipper),["Al","B","Mal"]);
});
# Python
def make_abm():
return [{'name': 'Alice', 'hobby': 'Kendo', 'age': 21},
{'name': 'Bob', 'hobby': 'Skiing', 'age': 23},
{'name': 'Mallory', 'hobby': 'Cryptanalysis', 'age': 30}]
def test_strings():
rows=make_abm()
table = Table(['name', 'hobby'], rows)
assert str(table) == \
'name hobby \n' + \
'Alice Kendo \n'+ \
'Bob Skiing \n' + \
'Mallory Cryptanalysis'
def test_strings_numbers():
rows=make_abm()
table = Table(['name', 'age'], rows)
assert str(table) == \
'name age \n' + \
'Alice 21 \n'+ \
'Bob 23 \n'+ \
'Mallory 30 '
def make_rows():
return [{0: 'entry 0 0', 1: 'entry 0 1',
2: 'entry 0 2', 3: 'entry 0 3'},
{0: 'entry 1 0', 1: 'entry 1 1',
2: 'entry 1 2', 3: 'entry 1 3'},
{0: 'entry 2 0', 1: 'entry 2 1',
2: 'entry 2 2', 3: 'entry 2 3'},
{0: 'entry 3 0', 1: 'entry 3 1',
2: 'entry 3 2', 3: 'entry 3 3'},
{0: 'entry 4 0', 1: 'entry 4 1',
2: 'entry 4 2', 3: 'entry 4 3'}]
def test_grid():
rows = make_rows()
cols = [0,1,2,3]
table = Table(cols,rows)
assert str(table) == \
'0 1 2 3 \n'+\
'entry 0 0 entry 0 1 entry 0 2 entry 0 3 \n'+\
'entry 1 0 entry 1 1 entry 1 2 entry 1 3 \n'+\
'entry 2 0 entry 2 1 entry 2 2 entry 2 3 \n'+\
'entry 3 0 entry 3 1 entry 3 2 entry 3 3 \n'+\
'entry 4 0 entry 4 1 entry 4 2 entry 4 3 '
def test_grid_select():
rows = make_rows()[0:3]
cols = [1,3]
table = Table(cols,rows)
assert str(table) == \
'1 3 \n'+\
'entry 0 1 entry 0 3 \n'+\
'entry 1 1 entry 1 3 \n'+\
'entry 2 1 entry 2 3 '
# Octave
%!assert (cut([1,0],[1,0],0,0) == [1,0])
%!assert (cut([0,1],[1,0],0,0) == [0,0])
%!assert (cut([2,0],[1,0],0,0) == [2,0])
%!assert (cut([0,2],[1,0],0,0) == [0,0])
%!assert (cut([1,0],[1],1,0) == [0,0])
%!assert (cut(eye(3),[1],1,1) == [0,0,0;
%! 0,1,0;
%! 0,0,0])
%!assert (cut(ones(3),[1],1,1) == [0,0,0;
%! 0,1,0;
%! 0,0,0])
%!assert (cut(ones(4),ones(3),0,0) == [1,1,1,0;
%! 1,1,1,0;
%! 1,1,1,0;
%! 0,0,0,0])
%!assert (cut(ones(4),ones(2),2,2) == [0,0,0,0;
%! 0,0,0,0;
%! 0,0,1,1;
%! 0,0,1,1])
%!assert (cut(ones(4),ones(2),2,1) == [0,0,0,0;
%! 0,0,1,1;
%! 0,0,1,1;
%! 0,0,0,0])