All pastes #38945 Raw Edit

Miscellany

public text v1 · immutable
#38945 ·published 2006-01-28 22:48 UTC
rendered paste body
;; 99 bottles of beer example -- simple, no cuteness.
;; Demonstrates several features of Moose, such as: cloning objects, blocks,
;; object mutability, ranges, regular expressions, etc.

BottleSong -> Object clone.

BottleSong current -> MutableString clone.

BottleSong phraseForBottle(Number) -> [|n|
    cond((n = 0) ["no more bottles of beer"],
         (n = 1) ["1 bottle of beer"],
         true    ["#{n} bottles of beer"])].

BottleSong doSong -> [
    99 to(1) each([i,
        "#{self current} on the wall, #{self current}." println.
        "take one down, pass it around, " print.
        self current := self phraseForBottle(i - 1).
        "#{self current} on the wall" println])].

; this isn't required, but it demonstrates how to set particular slots
; immutable ("final" in java/c# (except immutable slots can still be accessed
; through the lookup graph)) all in one go.
BottleSong slots select([|s|
    Regex clone setPattern("(phraseForBottle|doSong)") match(s)])
    each([|i| BottleSong setFlagOn('i, ObjectFlags immutableFlag)]).

; we don't need to clone this, but it's good style.
song -> BottleSong clone.
song doSong.