1. Home
  2. /
  3. Swift-wasm
  4. /
  5. SwiftWasm Fibonacci

SwiftWasm Fibonacci - Swift-wasm Typing CST Test

Loading…

SwiftWasm Fibonacci — Swift-wasm Code

Calculates nth Fibonacci number recursively and prints it.

# swift/demo/fibonacci.swift
import JavaScriptKit

let console = JSObject.global.console
func fib(_ n: Int) -> Int {
	if n <= 1 { return n }
	return fib(n - 1) + fib(n - 2)
}
console.log!("Fibonacci(6): \(fib(6))")

Swift-wasm Language Guide

SwiftWasm is a toolchain and ecosystem that compiles Swift code into WebAssembly, allowing developers to run Swift applications directly in the browser, WASI runtimes, serverless platforms, and edge environments. It brings Swift’s safety, performance, and modern language features to WebAssembly-based execution environments.

Primary Use Cases

  • ▸Running Swift code in the browser
  • ▸Building rich client-side apps using Swift + DOM APIs
  • ▸WASI-based CLI tools written in Swift
  • ▸Edge computing microservices in Swift
  • ▸Full-stack Swift (backend + Wasm frontend)

Notable Features

  • ▸Swift -> WebAssembly compilation support
  • ▸JavaScript ↔ Swift bridging APIs
  • ▸WASI target support for system-level interactions
  • ▸Event loops and async/await support
  • ▸Swift Package Manager integration

Origin & Creator

SwiftWasm originated as an open-source community project led by Max Desiatov and contributors around 2019, expanding the Swift compiler to support WebAssembly targets.

Industrial Note

SwiftWasm is gaining traction for Swift-based web UIs, full-stack Swift apps, serverless Wasm microservices, and Apple ecosystem developers who want to run Swift outside Apple hardware.

More Swift-wasm Typing Exercises

Simple SwiftWasm ProgramSwiftWasm Add Two NumbersSwiftWasm Multiply Two NumbersSwiftWasm FactorialSwiftWasm Even CheckSwiftWasm Maximum of Two NumbersSwiftWasm Minimum of Two NumbersSwiftWasm Toggle BooleanSwiftWasm Print Array

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher