Coder Social home page Coder Social logo

Comments (5)

jaywcjlove avatar jaywcjlove commented on June 15, 2024

Reduce

let items = [2.0, 4.0, 5.0, 7.0]
let total = items.reduce(10, +)
print(total) // 28.0
let codes = ["abc", "def", "ghi"]
let text = codes.reduce("", +)
print(text) // abcdefghi
let name = ["alan", "brian", "charlie"]
let scv = name.reduce("===") { text, name in
  "\(text), \(name)"
}
print(scv) // ===, alan, brian, charlie

from swiftui-example.

jaywcjlove avatar jaywcjlove commented on June 15, 2024

FlatMap

let collections = [[5, 2, 7], [4, 8], [9, 1, 3]]
let flat = collections.flatMap { $0 }
print(flat) // [5, 2, 7, 4, 8, 9, 1, 3]
let people: [String?] = ["Tom", nil, "Peter", nil, "Harry"]
let valid = people.compactMap { $0 }
print(valid) // ["Tom", "Peter", "Harry"]
let collections = [[5, 2, 7], [4, 8], [9, 1, 3]]
let onlyEven = collections.flatMap { intArray in
  intArray.filter { $0 % 2 == 0 }
}
print(onlyEven) // [2, 4, 8]
let collections = [[5, 2, 7], [4, 8], [9, 1, 3]]
let onlyEven = collections.flatMap { $0.filter { $0 % 2 == 0 }}
print(onlyEven) // [2, 4, 8]
let numbers = ["42", "19", "notANumber"]
let ints = numbers.compactMap { Int($0) }
print(ints) // [42, 19]

from swiftui-example.

jaywcjlove avatar jaywcjlove commented on June 15, 2024

Chaining 链式语法

let marks = [4, 5, 8, 2, 9, 7]
let totalPass = marks.filter { $0 >= 7}.reduce(0, +)
print(totalPass) // 24
let numbers = [20, 17, 35, 4, 12]
let evenSquares = numbers.map { $0 * $0 }.filter { $0 % 2 == 0 }
print(evenSquares) // [400, 16, 144]

from swiftui-example.

jaywcjlove avatar jaywcjlove commented on June 15, 2024
let languages = ["English", "Tamil", "German"]
for language in languages {
     print(language)
}
// Output:
// English
// Tamil
// German
let languages = ["English", "Tamil", "German"]
languages.forEach { (language) in
    print("Language", language)
}
// Output:
// Language English
// Language Tamil
// Language German
let languages = ["English", "Tamil", "German"]
for (index, element) in languages.enumerated() {
    print("Index:", index, "Language:", element)
}
// Output:
// Index: 0 Language: English
// Index: 1 Language: Tamil
// Index: 2 Language: German
let languages = ["English", "Tamil", "German"]
for i in 0 ..< languages.count {
    print("Language \(i+1):", languages[i])
}
// Output:
// Language 1: English
// Language 2: Tamil
// Language 3: German
let languagesDict = [
    "England":"English", "Germany":"German", "Mexico":"Spanish"
]
for (country, language) in languagesDict {
    print("The \(country)'s Language is \(language)")
}

// Output:
// The England's Language is English
// The Mexico's Language is Spanish
// The Germany's Language is German
for i in 0...5 {
    print(i)
}
// Output:
// 0
// 1
// 2
// 3
// 4
// 5
for i in 0..<5 {
    print(i)
}
// Output:
// 0
// 1
// 2
// 3
// 4

倒序

for i in (0...5).reversed() {
    print(i)    
}
// Output:
// 5
// 4
// 3
// 2
// 1
// 0
for i in stride(from: 0, to: 10, by: 2) {
    print(i)
}
// Output:
// 0
// 2
// 4
// 6
// 8
for i in stride(from: 0, through: 10, by: 2) {
    print(i)
}

// Output:
// 0
// 2
// 4
// 6
// 8
// 10
var evenNumbers = [Int]()
for number in (0...100) {
    guard evenNumbers.count < 10 else {
        break
    }

    guard number % 2 == 0 else {
        continue
    }
    evenNumbers.append(number)
}
print(evenNumbers) // Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
let evenNumbers = (0...100).filter { number -> Bool in
    return number % 2 == 0
}.prefix(10)

print(evenNumbers) // Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

from swiftui-example.

jaywcjlove avatar jaywcjlove commented on June 15, 2024
let nationals = ["Brits", "Spain", "Italy"]
let languages = ["English", "Spanish", "Italian"]
for i in 0 ..< nationals.count {
    var string = "\(nationals[i]) speaks"
    for _ in 1 ... 3 {
        string += " \(languages[i])"
    }
    print(string)
}
// Output:
// Brits speaks English English English
// Spain speaks Spanish Spanish Spanish
// Italy speaks Italian Italian Italian

from swiftui-example.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.