17 Jul 2023
if
statementsfor
loopswhile
loopsswitch
statements (in some languages, it’s case
statements)goto
statements (in some languages, it’s break
statements)Looping through the numbers 0 to 9 and printing them out:
Language: C
Language: Java
Now, the same code above in Assembly language:
In R we can use S3 classes:
Or something called S4 classes:
# Create a class
setClass("student",
slots = c(name = "character",
age = "numeric",
height = "numeric"))
# Create an object
specific_student <- new("student",
name = "John",
age = 30,
height = 1.8)
# Define a method
setMethod("show", "student", function(object) {
cat("Name:", object@name, "\n")
cat("Age:", object@age, "\n")
cat("Height:", object@height, "\n")
})
students <- list(
new("student", name = "John", age = 30, height = 1.8),
new("student", name = "Mary", age = 25, height = 1.6),
new("student", name = "Peter", age = 35, height = 1.7)
)
Then we can perform operations on the list of objects:
[1] 30
There are many approaches to writing functions. I am going to teach you the test-driven development (TDD) way.
mean()
my_mean()
.Task: write a function that scrapes the lyrics of any given song (URL) from AZ Lyrics.
🧑💻 Live coding of unit tests + function + debugging + documentation
LSE ME204 2023 (W02D01) | ARCHIVE