annotate 2-Io/day2-operators.io @ 37:86668d32e162

Add Day 2 code
author IBBoard <dev@ibboard.co.uk>
date Tue, 12 Sep 2017 20:58:51 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 # Operators are syntactic sugar and can be listed from a table:
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 OperatorTable println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 # Creating an operator involves defining it with a precedence
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 # (index based on the output from the previous table)
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 OperatorTable addOperator("xor", 11)
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 # And then implementing it on appropriate prototypes
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 # Note that Io is open-edit, like Ruby, so we can screw with core stuff
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 # like Booleans and extend them
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 true xor := method(bool, if(bool, false, true))
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 false xor := method(bool, if(bool, true, false))
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 # For some reason I have to work out, the prints don't work here
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 # They print the second value. But running in REPL works and return correct
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 # value (before printing wrong one)
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 (true xor true) println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 (true xor false) println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 (false xor true) println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 (false xor false) println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21