Swift for R Developers
Move from R to Swift through static types, value semantics, optionals, Unicode, protocols and ARC. Complete 48 multi-file exercises, build a parcel desk and port two complete R programs.
R and Swift side by side
The same program in R and in Swift, from the lessons of this course.
Bindings and Mutation
R names bind to objects, and an ordinary binding can later hold a different type. Rebinding label does not change saved. Most ordinary vectors and lists have copy-on-modify behavior, while environments have reference semantics; neither a name nor the…
R1label <- "draft"2saved <- label3label <- 174cat(saved, label, sep = " ")5cat("\n")Swift1let saved = "draft"2var label = saved3label = "ready"4print(saved, label)Strings and Interpolation
R commonly constructs text with paste, paste0 or sprintf. Sprintf uses formatting placeholders and %% for a literal percent sign; quoted strings do not interpolate arbitrary expressions by themselves. Character vectors can contain multiple strings, so a text…
R1done <- 3L2cat(sprintf("progress=%d%%", done * 25L), "\n", sep = "")3cat(tolower(as.character(startsWith("report.csv", "report"))), "\n", sep = "")Swift1let done = 32print("progress=\(done * 25)%")3print("report.csv".hasPrefix("report"))Switch and Patterns
R switch can select by a character value using named alternatives, or by a numeric position with different rules. It is not an exhaustive pattern matcher, and unmatched selections need deliberate handling. Here an unnamed final alternative supplies the…
R1command <- "pause"2message <- switch(command, pause = "idle", stop = "idle", "running")3cat(message, "\n", sep = "")Swift1let command = "pause"2switch command {3case "pause", "stop": print("idle")4default: print("running")5}Functions and Labels
R calls can match arguments by name or position, with partial matching rules in some signatures. Arguments are normally lazy promises evaluated when needed. Explicit full names make a public call clearer; a Swift argument label is checked at compilation and…
R1echo_sound <- function(sound, times) strrep(sound, times)2cat(echo_sound("ha", times = 3L), "\n", sep = "")Swift1func echo(_ sound: String, times count: Int) -> String {2 return String(repeating: sound, count: count)3}Dictionaries and Missing Keys
Assigning NULL with double-bracket list replacement removes that element in R. Single-bracket replacement with list(NULL) can instead preserve a present NULL element. Names can be inspected when missing versus present-null matters. Length counts list…
R1settings <- list(font = 14L, gap = 2L)2settings[["gap"]] <- NULL3value <- settings[["gap"]]4cat(if (is.null(value)) 8L else value, "\n", length(settings), "\n", sep = "")Swift1var settings = ["font": 14, "gap": 2]2settings["gap"] = nil3print(settings["gap"] ?? 8)4print(settings.count)Structs and Independent Copies
An ordinary R list can act as a record, and replacing a field in an assigned copy preserves the first list's value. That copy-on-modify behavior differs from environment-backed objects. A list's fields and shape remain dynamically chosen rather than defined…
R1first <- list(direction = "north")2second <- first3second$direction <- "west"4cat(first$direction, second$direction, sep = " ")5cat("\n")Swift1struct Heading {2 var direction: String3 mutating func turnWest() { direction = "west" }4}
Syllabus
10 chapters48 lessons1 project48 challenges99 quiz questionsStarting Swift
5 lessonsText and Decisions
5 lessonsControl Flow
5 lessonsFunctions and Arrays
5 lessonsCollection Tools
5 lessonsValues and References
5 lessonsFailures and Abstraction
6 lessonsDesigning Types
6 lessonsParcel Desk Project
4 lessonsR Ports
2 lessonsFrequently asked questions
Who is Swift for R Developers for?
For people who already write R and want to learn Swift. Each lesson starts from R you know and shows how Swift does the same thing, so you skip the beginner basics.
Do I need to know Swift already?
No. The course starts with your first Swift program. You need R basics: variables, conditions, loops, functions and collections.
How many lessons does Swift for R Developers have?
Swift for R Developers contains 48 interactive lessons you can complete at your own pace.
What does Swift for R Developers cover?
The course is organized into chapters covering: Starting Swift, Text and Decisions, Control Flow, Functions and Arrays, and more.
Is Swift for R Developers free?
Yes. Every course on Coddy is free to start. You can complete lessons in your browser without any downloads or signup.