Reputation: 1321
I have a piece of code. I am not getting whats going inside in this code. Can anybody explain it?
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"]
Upvotes: 4
Views: 16288
Reputation: 6092
If we take the parts of this code one after the other:
let wordFreqs = [("k", 5), ("a", 7), ("b", 3)]
You start with an array of tuples.
From the Swift documentation:
A tuple type is a comma-separated list of types, enclosed in parentheses.
and:
Tuples group multiple values into a single compound value. The values within a tuple can be of any type.
In this case, the tuples are "couples" of 2 values, one of type String and 1 of type Int.
let res = wordFreqs.filter(
{
(e) -> Bool in
This part applies a filter on the array. You can see here that the closure of the filter takes an element e (so, in our case, one tuple), and returns a Bool. With the 'filter' function, returning true means keeping the value, while returning false means filtering it out.
if e.1 > 3 {
return true
} else {
return false
}
The e.1
syntax returns the value of the tuple at index 1.
So, if the tuple value at index 1 (the second one) is over 3, the filter returns true (so the tuple will be kept) ; if not, the filter returns false (and therefore excludes the tuple from the result).
At that point, the result of the filter will be [("k", 5), ("a", 7)]
}).map { $0.0 }
The map function creates a new array based on the tuple array: for each element of the input array ($0), it returns the tuple value at index 0. So the new array is ["k", "a"]
print(res)
This prints out the result to the console.
These functions (filter, map, reduce, etc.) are very common in functional programming.
They are often chained using the dot syntax, for example, [1, 2, 3].filter({ }).map({ }).reduce({ })
Upvotes: 12
Reputation: 6859
Your e
object representing (String, int)
type. As you can see in array inside [("k", 5), ("a", 7), ("b", 3)]
.
First of all you are using filter
method so that's why you have to return true
or false
values. In this case you check if e.1
(means int value) is greater than 3, if not you return false. After all that process the filter
method return filtered array of (String,int) objects.
Next step is map
function. In your case is simple it is just map all array values to first object of your tuple (String, int).
To understand better your code could look like this:
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"]
Upvotes: 1
Reputation: 72460
wordFreqs
is array of tuple
.
Tuples
A tuple is a group of zero or more values represented as one value.
For example ("John", "Smith") holds the first and last name of a person. You can access the inner values using the dot(.) notation followed by the index of the value:
var person = ("John", "Smith")
var firstName = person.0 // John
var lastName = person.1 // Smith
Now in your case you have tuple with type (String, Int)
and with filter
you are comparing the e.1 > 3
(Here e
is holds the tuple value from the array iteration with filter
) means thats second(Int)
value is greater than 3.
Now after that your using map
on the filter
result and just retuning the String($0.0)
from the tuple.
//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
}
Upvotes: 1
Reputation: 77661
// 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)
Note... if I was writing this I would shorten it to something like...
let res = wordFreqs.filter { $0.1 > 3 }
.map { $0.0 }
Upvotes: 8