Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


struct Planet {

var name: String

var distanceFromSun: Double

}

let planets = [

Planet(name: "Mercury", distanceFromSun: 0.387),

Planet(name: "Venus", distanceFromSun: 0.722),

Planet(name: "Earth", distanceFromSun: 1.0),

Planet(name: "Mars", distanceFromSun: 1.52),

Planet(name: "Jupiter", distanceFromSun: 5.20),

Planet(name: "Saturn", distanceFromSun: 9.58),

Planet(name: "Uranus", distanceFromSun: 19.2),

Planet(name: "Neptune", distanceFromSun: 30.1)

]

let result1 = planets.map { $0.name }

let result2 = planets.reduce(0) { $0 + $1.distanceFromSun }

What are the types and values of the result1 and result2 variables? Explain why.



struct Planet { var name: String var distanceFromSun: Double } let planets = [ ..

Answer / iosraj

result1 is an array of strings, containing the list of the planet names result2 is a double, calculated as the sum of the distance of all planets

The map method of the Array<T> struct type performs a transformation of the source array into an array of another type, whose values are obtained by executing the closure passed as parameter to each element of the array. In the above code, the closure returns the name property, so the map method in the above code returns an array of planet names.

Given an initial value and a closure, the reduce method of the Array<T> struct type returns a single value obtained by recursively applying the closure to each element of the array. The closure takes the value calculated at the previous step (or the initial value if it’s the first iteration) and the current array element, and is expected to return a value of the same type of the initial value.

In the above code, the closure returns the sum of what calculated at the previous step, plus the value of the distanceFromSun property for the current element. The end result is the sum of the distances of all planets.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Apple iOS Swift Interview Questions

How to create a tuple in swift?

0 Answers  


What is block in swift?

0 Answers  


Who created swift?

0 Answers  


How proficient are you in objective-c and swift? Can you briefly describe their differences?

0 Answers  


How to declare an empty dictionary in swift?

0 Answers  


What are the best ways of achieving concurrency in ios?

0 Answers  


What is lazy stored procedure in swift and when is it used?

0 Answers  


What will you do if your app is prone to crashing?

0 Answers  


What is race condition in swift?

0 Answers  


What is enum in swift?

0 Answers  


Is swift a functional language?

0 Answers  


What is data type in swift?

0 Answers  


Categories