Skip to content

Add some showcase snippets #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions assets/snippets/Coding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Create types that automatically encode to JSON.

import Foundation

struct ScientificName: Codable {
var genus: String
var species: String
var subspecies: String?
}

let momiji = ScientificName(genus: "Acer", species: "palmatum")
let jsonData = try JSONEncoder().encode(momiji)

// {"genus":"Acer","species":"palmatum"}
16 changes: 16 additions & 0 deletions assets/snippets/IfLet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Easily reason about data that may or may not be present.

struct ScientificName {
var genus: String
var species: String
var subspecies: String?

var description: String {
var text = "\(genus) \(species)"
if let subspecies {
// subspecies is guaranteed to be non-nil here.
text += "subsp. \(subspecies)"
}
return text
}
}
12 changes: 12 additions & 0 deletions assets/snippets/MapReduce.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Use functional programming techniques.

import Foundation

func squareWave(phase: Float, overtones: UInt) -> Float {
return (0...overtones)
.map {
let harmonic = Float($0 * 2 + 1)
return sinf(phase * harmonic) / harmonic
}
.reduce(0, +)
}
18 changes: 18 additions & 0 deletions assets/snippets/MultilineString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Multiline strings with string interpolation.

let hello = "Hello, world!"
let multilineString = """
@@@
@@ @@@@
@@ @@@ @@@@@
@@@@@@@@@ @@@@@
@@@@@@@@@@ @@@@@@
@@@@@@@@@@ @@@@@@
@@@@@@@@@@@@@@@@@
@ @@@@@@@@@@@@@@@
@@@@@@ @@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@ @
\(hello)
"""