Skip to content

Commit f1f3f4c

Browse files
authored
Add Fizz Buzz in Tcl (#5059)
1 parent b0bb9d6 commit f1f3f4c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

archive/t/tcl/fizz-buzz.tcl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This approach is exclusively to show the capabilities and flexibility of Tcl.
2+
# It doesn't deserve being this overengineered in the normal case. :P
3+
4+
proc fizzbuzz {rules {limit 100}} {
5+
namespace eval ::fizzbuzz {
6+
namespace export *
7+
namespace ensemble create
8+
}
9+
10+
foreach {div word} $rules {
11+
proc ::fizzbuzz::$word {n} [format {
12+
expr {($n %% %d) == 0 ? "%s" : ""}
13+
} $div $word]
14+
}
15+
16+
for {set n 1} {$n <= $limit} {incr n} {
17+
set s ""
18+
foreach {div word} $rules {
19+
append s [::fizzbuzz::$word $n]
20+
}
21+
puts [expr {$s eq "" ? $n : $s}]
22+
}
23+
}
24+
25+
fizzbuzz {3 Fizz 5 Buzz}

0 commit comments

Comments
 (0)