Skip to content

Instantly share code, notes, and snippets.

@brianjlandau
Created October 10, 2010 18:53
Show Gist options
  • Select an option

  • Save brianjlandau/619456 to your computer and use it in GitHub Desktop.

Select an option

Save brianjlandau/619456 to your computer and use it in GitHub Desktop.
Just some cool snippets I've found or created for adding syntactical sugar to Io.
File read := method(path,
fileToRead := File with(path) openForReading
content := fileToRead readToEnd
fileToRead close
return content
)
# Pulled from http://iota.flowsnake.org/syntax-extensions.html
Object squareBrackets := method(
r := List clone
call message arguments foreach(arg,
r push(call sender doMessage(arg)))
r
)
# Pulled from http://github.com/tyler/iota/blob/master/vendor/iota/iota.io by Tyler McMullen
Object curlyBrackets := method(
map := Map clone
call message arguments foreach(
pair, map atPut(pair name, call sender doMessage(pair last))) )
# Based on code at http://iota.flowsnake.org/syntax-extensions.html
Map squareBrackets := method(key,
self at(call evalArgAt(0))
)
# Pulled from http://iota.flowsnake.org/syntax-extensions.html
List squareBrackets := method(index,
self at(call evalArgAt(0))
)
# Pulled from http://code.google.com/p/iopeg/wiki/IoNuggets
OperatorTable addOperator("||", 13)
Object || := method(
if ( self,
m := call message next ?next
if ( m, call message setNext( m ) )
self
,
call evalArgAt(0)
)
)
# My own creation based on code at http://code.google.com/p/iopeg/wiki/IoNuggets
OperatorTable addAssignOperator( "||=", "createIfNonexistantOrUpdateIfNil" )
createIfNonexistantOrUpdateIfNil := method( seq, val,
if (call sender hasSlot(seq)) then (
currentValue := call sender doMessage( seq asMessage )
if (currentValue == nil) then (
return call sender updateSlot( seq, val )
) else (
return currentValue
)
) else (
return call sender setSlot(seq, val)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment