Massive filter in Swift3

I have a piece of code. I do not understand what is going on inside this code. Can anyone explain this?

 let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]

        let res = wordFreqs.filter(
        {
            (e) -> Bool in

            if e.1 > 3 {
                return true
            } else {
                return false
            }

        }).map { $0.0 }

        print(res)

Gives output:

["k","a"]
+4
source share
4 answers

If we take parts of this code one by one:

let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]

You start with an array of tuples .

In the Swift documentation:

A tuple type is a comma-separated list of types enclosed in parentheses.

and

Tuples group multiple values ​​into one composite value. The values ​​in the tuple can be of any type.

In this case, tuples are "pairs" of two values, one of the String type and one of the Int type.


        let res = wordFreqs.filter(
        {
            (e) -> Bool in

. , e (, , ) Bool. "filter" true , false .


            if e.1 > 3 {
                return true
            } else {
                return false
            }

e.1 1. , 1 () 3, true ( ); , false (, , ). [("k", 5), ("a", 7)]


        }).map { $0.0 }

map : ($ 0) 0. , ["k", "a"]


        print(res)

.


(, , ..) . , , [1, 2, 3].filter({ }).map({ }).reduce({ })

+10
// this creates an array of tuples
let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]

let res = wordFreqs.filter {
    (e) -> Bool in

    // this filters the array
    // it removes any items that have the second part of the tuple
    // of 3 or less
    if e.1 > 3 {
        return true
    } else {
        return false
    }
}.map {
    // this "maps" the array and returns the first part of the tuple
    $0.0
}

print(res)

... , - ...

let res = wordFreqs.filter { $0.1 > 3 }
                   .map { $0.0 }
+8

wordFreqs - tuple.

, .

( "", "" ) . (.) , :

var person = ("John", "Smith")

var firstName = person.0 // John
var lastName = person.1 // Smith

(String, Int) filter e.1 > 3 ( e filter) , second(Int) 3.

map filter String($0.0) .

    //array of tuples
    let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]

    let res = wordFreqs.filter(
    {
        (e) -> Bool in
        //Comparing the Second Int value of tuple in filter
        if e.1 > 3 {
            return true
        } else {
            return false
        }

    })
    //Mapping the result of filter
    .map { 
         //return the String from the tuple
         $0.0 
    }
+1

e, (String, int). [("k", 5), ("a", 7), ("b", 3)].

, filter, true false. , e.1 ( int) 3, false. filter (String, int).

- map. (String, int).

, :

let filteredArray = wordFreqs.filter
({
    (e) -> Bool in
    return e.1 > 3
})// the filteredArray is [("k", 5), ("a", 7)]


let finalValue = filteredArray.map { 
  $0.0
}// here you're creating a new array with String. $0 represents object from filteredArray

print(finalValue) // ["k", "a"]
+1
source

Source: https://habr.com/ru/post/1667354/


All Articles