Skip to content

Instantly share code, notes, and snippets.

@caffo
Created January 10, 2012 14:29
Show Gist options
  • Save caffo/1589347 to your computer and use it in GitHub Desktop.
Save caffo/1589347 to your computer and use it in GitHub Desktop.
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)))
)
unless(1 == 2, write("One is not two\n"), write("one is two\n"))
@jeremytregunna
Copy link

As @Ticking already points out, Io already has an unless:

someCondition ifFalse(doSomething)

Enjoy.

@draegtun
Copy link

@paulmillr - Beauty is in the eye of the beholder because I personally find the Io solution much more lucid & elegant. So in my eyes its more beautiful compared to the Scala version.

BTW... the postfix unless is Perl like because that's where Ruby got the Statement Modifiers from: http://perldoc.perl.org/perlsyn.html#Statement-Modifiers

@jeremytregunna
Copy link

Except the postfix version has nothing to do with perl. Io's a message passing language, everything is a message and all messages are expressions. The equivalent code in a language that uses a dot instead a space for passing a message to an object (and supports either first class messages like Io or higher order functions):

condition.ifFalse(doSomething)

It's no different than:

model.update_attributes(bleh: 42)

In a Ruby/Rails app. The same mechanisms are at play. Lookup, & perform

@draegtun
Copy link

@jeremytregunna: I'm referring to the (syntax of the) Scala example provided: println("flag is " + flag) unless flag

@jmcarthur
Copy link

In Haskell:

unless p a b = if p then b else a

If if was just a function instead of special syntax (or one had defined a function with the same behavior), it could look like this:

unless = if . not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment