Code · 1 file

Sample Nim File

Nim, a compiled language with Python-like syntax. Every language sample implements the same small program so you can compare them side by side.

Nim · 616 B

Sample Nim file

An idiomatic Nim example — the same customer program (or, for hardware languages, a small counter design) we offer in every language.

CC0 · free for any use · Verified by an independent tool · Report a problem

sample.nim — first linesNim
# Sample Nim program — samplefiles.com
import std/strformat

type Customer = object
  id: int
  name: string
  balance: float

proc isVip(c: Customer): bool = c.balance >= 10_000.0

proc totalBalance(customers: openArray[Customer]): float =
  for c in customers:
    result += c.balance

let customers = [
  Customer(id: 1, name: "Ada", balance: 12_500.0),
  Customer(id: 2, name: "Grace", balance: 830.5),
  Customer(id: 3, name: "Linus", balance: 10_000.0),
]

for c in customers:
  let tag = if c.isVip: "VIP" else: ""
  echo &"{c.name:<6} {c.balance:>10.2f} {tag}"
echo &"Total: {totalBalance(customers):.2f}"

File details

Size
616 B (616 bytes)
MIME type
text/plain
SHA-256
bd44f5c9d1eed4e8908ad1de9b943d69e7c993474c3dd5ee4b73ae494bdcb2e2
MD5
a08c73032803324c90d099d8ff3dea28

Download from the command line

curl -L -o sample.nim https://files.samplefiles.com/code/nim/sample.nim

Or check the download in your browser with the checksum calculator.