Last active
January 13, 2025 08:15
-
-
Save jacob-ebey/710aafdcf048e524f32fb67247100411 to your computer and use it in GitHub Desktop.
Neva n-bottles (99 bottles with input)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { fmt, strconv, strings } | |
def Main(start any) (stop any) { | |
print For<int>{Next2Lines} | |
wait Wait | |
readInt ReadInt | |
range Range | |
printErr fmt.Println | |
--- | |
// Notify the range and readInt actions to start | |
:start -> [range:sig, readInt] | |
// Set the range from user input to -1 | |
readInt:res -> range:from | |
-1 -> range:to | |
// If readInt errors, print it | |
readInt:err -> printErr | |
// Print lines for each number in the range | |
range -> print -> wait | |
// If we are done waiting, or have printed an error, stop | |
[wait, printErr] -> :stop | |
} | |
def ReadInt(sig any) (res int, err error) { | |
fmt.Scanln | |
strconv.ParseNum<int> | |
--- | |
:sig -> scanln -> parseNum | |
parseNum:res -> :res | |
parseNum:err -> :err | |
} | |
def Next2Lines(data int) (sig any) { | |
first Tap<int>{FirstLine} | |
dec Dec<int> | |
second SecondLine | |
--- | |
:data -> first -> dec -> second -> :sig | |
} | |
def FirstLine(data int) (sig any) { | |
p1 fmt.Println | |
p2 fmt.Println | |
p3 fmt.Printf | |
panic Panic | |
--- | |
:data -> switch { | |
0 -> 'No more bottles of beer on the wall, no more bottles of beer.' -> p1 | |
1 -> '1 bottle of beer on the wall, 1 bottle of beer.' -> p2 | |
_ -> [ | |
p3:args[0], | |
'$0 bottles of beer on the wall, $0 bottles of beer.\n' -> p3:tpl | |
] | |
} | |
[p1, p2, p3:sig] -> :sig | |
p3:err -> panic | |
} | |
def SecondLine(data int) (sig any) { | |
p1 fmt.Println | |
p2 fmt.Println | |
p3 fmt.Println | |
p4 fmt.Printf | |
panic Panic | |
--- | |
:data -> switch { | |
-1 -> 'Go to the store and buy some more, 99 bottles of beer on the wall.' -> p1 | |
0 -> 'Take one down and pass it around, no more bottles of beer on the wall.\n' -> p2 | |
1 -> 'Take one down and pass it around, 1 bottle of beer on the wall.\n' -> p3 | |
_ -> [ | |
p4:args[0], | |
'Take one down and pass it around, $0 bottles of beer on the wall.\n\n' -> p4:tpl | |
] | |
} | |
[p1, p2, p3, p4:sig] -> :sig | |
p4:err -> panic | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment