turbo-editors/turbo-moonbitpublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-moonbit.git
git clone ssh://git@rickub.com/turbo-editors/turbo-moonbit.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

📦 Turbo MoonBit cc1f595 · on main · k33g · 11h ago
main.mbt · 52 lines · 1.2 KBMoonBit Blame HistoryRaw
 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
///|
/// A greeting to print, and how many times to print it.
struct Greeting {
  name : String
  times : Int
} derive(Eq)

///|
/// Show is written out by hand rather than derived: `derive(Show)` is
/// deprecated, and this is the form the toolchain points at instead.
impl Show for Greeting with fn output(self, logger) {
  logger.write_string("\{self.name} × \{self.times}")
}

///|
/// The tone a greeting is delivered in.
enum Tone {
  Plain
  Loud
  Question
} derive(Debug)

///|
/// punctuate returns the mark a tone ends on.
fn punctuate(tone : Tone) -> String {
  match tone {
    Plain => "."
    Loud => "!"
    Question => "?"
  }
}

///|
/// greet prints one greeting, once per `times`.
///
/// `tone` is a labelled argument with a default, so a call site may leave it
/// out — and when it does not, it reads as `greet(g, tone=Loud)`.
fn greet(g : Greeting, tone? : Tone = Plain) -> Unit {
  for i in 0..<g.times {
    println("Hello, \{g.name}\{punctuate(tone)} (\{i + 1} of \{g.times})")
  }
}

///|
fn main {
  let g = { name: "MoonBit", times: 2, }
  for tone in [Plain, Loud, Question] {
    greet(g, tone~)
  }
  println("")
  println("the greeting itself: \{g}")
}