view 2-Io/day2-messages.io @ 101:1fae0cca1ef8

Reduce large maze to single width corridors This reduces the permutations for a x x x b x To one (two steps north) from four (two steps north; one east, two north, one west; one east, one north, one west, one north; and one north, one east, one north, one west). Longer corridors were worse! We would filter this in the "been here before via another path" but that's still a lot of lookups in lists, which is inefficient.
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:42:24 +0100
parents 86668d32e162
children
line wrap: on
line source

# Everything is a message in Io, apparently. Everything.
# Messages have senders, receivers and args, and you can get and use those objects.
# So the example makes a post office.
postOffice := Object clone
# Add a method that returns the sender object
postOffice packageSender := method(call sender)
"Post office: " print
postOffice println
mailer := Object clone
# Call the PostOffice's method
mailer deliver := method(postOffice packageSender)
"Mailer: " print
mailer println
# Calling the method returns the caller (sender via the postOffice object)
mailer deliver println

# We can also get message arguments and name
# Let's do this a little differently to the book!
postOffice messageDetails := method(call message name println; call message arguments println)
postOffice messageDetails("One", 2, :three)

# Because of how args are passed unevaluated then we can make our own "unless" method
unless := method(
    (call sender doMessage(call message argAt(0))) \
        ifFalse(call sender doMessage(call message argAt(1))) \
        ifTrue(call sender doMessage(call message argAt(2)))
)
# Note the line continuations - this seems more readable than the approach in the book of
# leaving the "ifFalse(" and "ifTrue(" on the previous line!
# Also, this is an IF in the form "[boolean statement] ifFalse(code) ifTrue(code)", not the earlier
# "if(boolean, true-code, false-code)" form

unless (1 == 2, write("One is not two\n"), write("OMG! Maths has broken!!!!\n"))