Nim
2008fragletsystemsimperativefunctionalobject-oriented.nim
docker run --rm --platform="linux/amd64" 100hellos/nim:latest
MCP + fragletc
MCPstdinargs
This language supports code execution via MCP and the fragletc CLI. Stdin piping and argument passing are both supported.
Install fragletc →Nim is a statically typed, compiled systems programming language that is designed to be "efficient, expressive, and elegant".
Fun Facts
- Python-like Syntax: Nim's syntax is heavily inspired by Python, making it clean, readable, and easy to learn for programmers coming from a Python background.
- Compiles to C, C++, or JavaScript: Nim is a true transpiled language. This allows it to generate highly optimized, dependency-free native executables or to be used for frontend web development.
- Performance: Because it compiles to C/C++, Nim executables are extremely fast and memory-efficient, making it suitable for systems programming, game development, and other performance-critical applications.
- Powerful Metaprogramming: Nim has a powerful macro system that allows developers to manipulate the abstract syntax tree (AST) directly. This enables the creation of expressive domain-specific languages (DSLs) and can eliminate boilerplate code.
- Self-Hosting: The Nim compiler itself is written in Nim.
- Flexible Memory Management: Nim offers multiple memory management strategies, from a deterministic garbage collector (the default) to manual memory management, giving developers fine-grained control over performance.
- Style Insensitive: Identifiers in Nim are style-insensitive.
helloWorldis the same ashello_world, allowing developers with different style preferences to collaborate easily.
This "Hello World" is implemented by compiling the Nim source code with the nim compiler and executing the resulting binary.
Hello World
#!/bin/sh
# If this file is present, this is the file that runs when you add the
# RUN=1 option.
#
# Otherwise, the default behavior is to run the first file in the
# directory that matches the pattern `hello-world.*``.
# Build it
# Run it
cd /hello-world
nim c --hints:off --verbosity:0 -o:/tmp/hello /hello-world/helloworld.nim
/tmp/hello "$@"Coding Guide
Language Version
Nim (latest stable)
Execution Model
- Compiled language using
nim c(compile to C, then compile C to binary) - Code is compiled to a binary executable, then executed
- Top-level code executes when the program runs
- Procedures (functions) can be defined and called
Key Characteristics
- Statically typed with type inference (
let,var) - Indentation-sensitive (like Python)
- Case-insensitive identifiers (style-insensitive)
- Garbage collected (default)
- Strong typing with type inference
- Procedures are first-class citizens
- Supports both imperative and functional programming styles
- Compiles to C/C++ for native performance
Fragment Authoring
Write valid Nim code. Your fragment can define procedures, variables, and top-level execution code. Import statements are already in place. Your fragment will be compiled and executed.
Available Libraries
The template includes the standard library package:
std/strformat- String formatting (fmtmacro)
Additional standard library packages can be imported as needed:
std/os- Operating system interfacestd/strutils- String utilitiesstd/sequtils- Sequence utilitiesstd/math- Mathematical functionsstd/times- Time operationsstd/json- JSON encoding/decodingstd/httpclient- HTTP client- And many more from the Nim standard library
Common Patterns
- Print:
echo "message"orecho fmt"formatted {value}" - Variables:
let x = 10(immutable) orvar x = 10(mutable) - Type annotations:
let x: int = 10 - Procedures:
proc add(a, b: int): int = a + b - Procedures with body:
proc greet(name: string): string = return "Hello, " & name - Sequences:
let numbers = @[1, 2, 3]orvar numbers = newSeq[int]() - Tables (maps):
import std/tables; let m = {"key": "value"}.toTable - Loops:
for item in items: echo item - Conditionals:
if condition: echo "yes" else: echo "no" - String concatenation:
let s = "Hello" & " " & "World"
Examples
# Simple output
echo "Hello from fragment!"
# Variables and calculations
let a = 5
let b = 10
echo fmt"Sum: {a + b}"
# Procedures
proc add(a, b: int): int =
return a + b
echo fmt"{5} + {10} = {add(5, 10)}"
# Sequences and loops
let numbers = @[1, 2, 3, 4, 5]
var sum = 0
for num in numbers:
sum += num
echo fmt"Sum: {sum}"
# String operations
let s = "Hello"
let result = s & " World!"
echo result
echo fmt"Length: {result.len}"
# Conditional logic
let x = 10
if x > 5:
echo "x is greater than 5"
else:
echo "x is not greater than 5"
# Mutable variables
var counter = 0
counter += 1
echo fmt"Counter: {counter}"Caveats
- Fragments must be valid Nim code that compiles
- Indentation is significant (use spaces, typically 2 spaces)
- Variables declared with
letare immutable, usevarfor mutable variables - The code is compiled fresh each time, so compilation errors will fail execution
- Type inference is powerful but explicit types can improve clarity
- String concatenation uses
&operator, not+ - Sequences use
@[]syntax for literals - Style-insensitive means
helloWorldandhello_worldare the same identifier - Top-level code executes immediately when the program runs
Fraglet Scripts
Echo Args
#!/usr/bin/env -S fragletc --vein=nim
import std/strutils
import std/os
var a: seq[string]
for i in 1..paramCount(): a.add paramStr(i)
echo "Args: ", a.join(" ")Nim Fortune
#!/usr/bin/env -S fragletc --vein=nim
# One from the vault: Nim fortunes
echo "Nim: the language that asked 'what if Python and C had a baby?' and then made it work."Stdin Upper
#!/usr/bin/env -S fragletc --vein=nim
import std/strutils
try:
while true:
let line = readLine(stdin)
echo line.toUpper()
except EOFError:
discardTest
#!/usr/bin/env -S fragletc --vein=nim
import std/strformat
proc getGreeting(): string =
let part1 = "Hello"
let part2 = "World"
fmt"{part1} {part2}!"
echo getGreeting()