forked from exercism/wren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof.ci.wren
52 lines (46 loc) · 1.45 KB
/
proof.ci.wren
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class FoodChain {
/// Recite verse number `n`
static recite(n) { new().verse(n - 1) }
/// Recite verses from number `from` to number `to` inclusive
static recite(from, to) {
var chain = new()
var lines = []
for (n in from..to) {
lines.addAll(chain.verse(n - 1))
lines.add("")
}
return lines[0...-1]
}
construct new() {
_animals = ["fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"]
_phrases = {
"spider": "It wriggled and jiggled and tickled inside her.",
"bird": "How absurd to swallow a bird!",
"cat": "Imagine that, to swallow a cat!",
"dog": "What a hog, to swallow a dog!",
"goat": "Just opened her throat and swallowed a goat!",
"cow": "I don't know how she swallowed a cow!",
"horse": "She's dead, of course!"
}
}
verse(i) {
var lines = []
var animal = _animals[i]
lines.add("I know an old lady who swallowed a %(animal).")
if (animal != "fly") {
lines.add(_phrases[animal])
if (animal == "horse") {
return lines
}
var predator = animal
for (j in i..1) {
var prey = _animals[j - 1]
var extra = prey == "spider" ? " that wriggled and jiggled and tickled inside her" : ""
lines.add("She swallowed the %(predator) to catch the %(prey)%(extra).")
predator = prey
}
}
lines.add("I don't know why she swallowed the fly. Perhaps she'll die.")
return lines
}
}