Skip to content

VSL By Example

Vihan edited this page Mar 19, 2018 · 3 revisions

Introduction to VSL using examples:

func main() {
     print("Hello, World!") // Prints Hello, World!
}
const helloWorld = "Hello, World!"
func main() {
     print(helloWorld) // Prints Hello, World!
}
import IO

func main() {
     let name = prompt("Enter your name: ")
     print("Hello #{name}")
}
func main() {
    let likesGoats = prompt("Do you like goats: ", type: .boolean)
    if likesGoats {
        print("good choice")
    } else {
        print("ono :(")
    }
}
func main() {
    let array: Int[] = []
    if let lastItem: Int? = array.lastItem {
        print("Last item is #{lastItem}")
    } else {
        print("No last item in this array")
    }
}
func randTwoInts(from array: Int[]) -> (Int, Int) {
    return (array.randomItem(), array.randomItem())
}
func partialAddition(x: Int) -> (Int) -> Int {
    return (y: Int) => x + y
}

func main() {
    let increment = partialAddition(x: 1)
    let two = increment(1)
}
func main() {
    let zeroToFour = [0, 1, 2, 3, 4]
    let oneToFive = zeroToFour.map { ? + 1 }
}
import IO

class Goat {
    let name: String
    let shoeSize: Int

    func greet() {
        print("My name is #{self.name} and my shoe size is #{self.shoeSize}")
    }
}

func main() {
     let goat = Goat(name: "Sir Goat IV", shoeSize: 9)
     goat.greet()
}
interface GoatConvertable {
     let asGoat: Goat { get }
}

class Goat: GoatConvertable {
    let name: String
    let shoeSize: Int

    let asGoat: Goat { return self }
}

class HornedAnimalWithBeard: GoatConvertable {
     let name: String
     let shoeSize: Int

     let asGoat: Goat { return Goat(name: name, shoeSize: shoeSize) }
}
func sayHi(to goat: Goat) {
    print("Hello #{goat}!")
}