changeset 40:82627dd71c75

Implement 2D list/array
author IBBoard <dev@ibboard.co.uk>
date Thu, 14 Sep 2017 20:24:25 +0100
parents c48e6e454991
children f4ea1f637f22
files 2-Io/day2-self-study.io
diffstat 1 files changed, 40 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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