# HG changeset patch # User IBBoard # Date 1505417065 -3600 # Node ID 82627dd71c7574d4a95b307022309ef629a9a010 # Parent c48e6e454991d2b0635d68a20adae2b1455430cf Implement 2D list/array diff -r c48e6e454991 -r 82627dd71c75 2-Io/day2-self-study.io --- a/2-Io/day2-self-study.io Wed Sep 13 20:55:07 2017 +0100 +++ b/2-Io/day2-self-study.io Thu Sep 14 20:24:25 2017 +0100 @@ -89,4 +89,44 @@ list() myAverage println +#5) 2D list +TwoDList := Object clone +# It'd be nice to do something to set defaults when first cloning +# but I can't find how to do that, so force the developer to +# always dim() after clone +TwoDList dim := method(x, y, + self arr := list() + for (i, 1, x, + sub_list := list() + for (j, 1, y, + sub_list append(nil) + ) + arr append(sub_list) + ) +) +TwoDList set := method(x, y, value, + self arr at(x) atPut(y, value) +) + +TwoDList get := method(x, y, + outer := self arr at(x) + if (outer != nil, + return outer at(y), + return nil + ) +) + +my_arr := TwoDList clone +my_arr dim(2, 3) +my_arr println +my_arr set(0, 1, "0,1") +my_arr set(1, 2, "Bottom-right") +my_arr get(1, 2) println +my_arr get(0, 1) println +my_arr get(0, 0) println +my_arr get(5, 5) println + + + +# 4) final piece list(1, 2, 3, "foo") myAverage \ No newline at end of file