view 2-Io/day2-operators.io @ 103:98be775c533c default tip

An odd "non-determinism" example from StackOverflow It is clever, but doesn't make much sense as to how it gets its results
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:44:13 +0100
parents 86668d32e162
children
line wrap: on
line source

# Operators are syntactic sugar and can be listed from a table:
OperatorTable println

# Creating an operator involves defining it with a precedence
# (index based on the output from the previous table)
OperatorTable addOperator("xor", 11)

# And then implementing it on appropriate prototypes
# Note that Io is open-edit, like Ruby, so we can screw with core stuff
# like Booleans and extend them
true xor := method(bool, if(bool, false, true))
false xor := method(bool, if(bool, true, false))

# For some reason I have to work out, the prints don't work here
# They print the second value. But running in REPL works and return correct
# value (before printing wrong one)
(true xor true) println
(true xor false) println
(false xor true) println
(false xor false) println