How do I write map that works for all types in Swift?

Spasiu

As an exercise, I'm implementing a map function that takes an array and a function and applies the function to all elements of the array, but I don't know how to declare it such that it works for any type of array. I can do something like

func intMap(var arr: [Int], fun: (Int) -> Int) -> [Int] {
    for i in 0 ..< arr.count {
        arr[i] = fun(arr[i])
    }
    return arr
}

intMap([1,2,3], {x in return x * x})

But this only works for int.

What is the type signature for Swift's built-in map?

Edit:

So I was missing the fact that I can declare param type signatures without declaring their types explicitly.

func myMap<T>(var arr: [T], fun: (T) -> T) -> [T] {
    for i in 0 ..< arr.count {
        arr[i] = fun(arr[i])
    }
    return arr
}

myMap([1,2,3], fun: {
    x in return x * x
})
Abizern
  • Create a new Playground
  • Just under where it has import UIKit type import Swift
  • Command click on the word Swift

This will open the Swift library and you can see all the type definitions there.

And you can see:

extension CollectionType {
/// Return an `Array` containing the results of mapping `transform`
/// over `self`.
///
/// - Complexity: O(N).
@warn_unused_result
@rethrows public func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]

Edited to add

Alternatively, you can write a more generalised map

func myMap<T, U>(var arr: [T], fun: T -> U) -> [U] {
    var a: [U] = []
    for i in 0 ..< arr.count {
        a.append(fun(arr[i]))
    }

    return a
}

Which returns a new array, of a possibly different type, which you can see for yourself by putting this in your playground.

let a = [1, 2, 3]
let b = myMap(a, fun: { x in Double(x) * 2.1 })
a
b

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I remove all map annotations in swift 2

From Dev

How do I store Enum types into a map?

From Dev

How do I write Swift infix functions?

From Dev

How do I write a swift array that can make the same UIImage appear for all my dynamic cells?

From Dev

How do I map keys for specific file types in Vim?

From Dev

How do I catch stack errors of all types?

From Dev

Guice: how do I bind generics for ALL types?

From Dev

How do I catch stack errors of all types?

From Dev

How do I put different types in a dictionary in the Swift Language?

From Dev

How do I write a new #%datum function to catch all strings?

From Dev

How do I write the DataTemplates for a TreeView when all the items are in a wrapper?

From Dev

How do I write the DataTemplates for a TreeView when all the items are in a wrapper?

From Dev

How do I write all lines from less to a file?

From Dev

How do I catch and log all attempts to write to /dev/null?

From Dev

How do i write this SQL query for an ALL condition?

From Dev

How do I write a regex to match all single characters in a string?

From Dev

How do I write an SQL query with a WHERE = 'all'?

From Java

How do I write a custom init for a UIView subclass in Swift?

From Dev

How do I write a build pre-action script in Swift?

From Dev

How do I write a generic recursive function that takes a CollectionType in Swift?

From Dev

How to write Swift generic functions of enum types?

From Dev

How do I map two [String] into one [Record] in swift?

From Dev

How do I map two [String] into one [Record] in swift?

From Dev

How do I write a Dart web app that uses spawnUri and works with both Dartium and dart2js?

From Dev

How do I write a Dart web app that uses spawnUri and works with both Dartium and dart2js?

From Dev

Core Data: How do I delete all objects with an attribute in Swift?

From Dev

How do I write a list/array of custom types into HDF5 file?

From Dev

How do I write my REST API call when expecting two types of return Objects

From Dev

How do I write a batch script that iterates through all png files in all subfolders and does an action?

Related Related

  1. 1

    How do I remove all map annotations in swift 2

  2. 2

    How do I store Enum types into a map?

  3. 3

    How do I write Swift infix functions?

  4. 4

    How do I write a swift array that can make the same UIImage appear for all my dynamic cells?

  5. 5

    How do I map keys for specific file types in Vim?

  6. 6

    How do I catch stack errors of all types?

  7. 7

    Guice: how do I bind generics for ALL types?

  8. 8

    How do I catch stack errors of all types?

  9. 9

    How do I put different types in a dictionary in the Swift Language?

  10. 10

    How do I write a new #%datum function to catch all strings?

  11. 11

    How do I write the DataTemplates for a TreeView when all the items are in a wrapper?

  12. 12

    How do I write the DataTemplates for a TreeView when all the items are in a wrapper?

  13. 13

    How do I write all lines from less to a file?

  14. 14

    How do I catch and log all attempts to write to /dev/null?

  15. 15

    How do i write this SQL query for an ALL condition?

  16. 16

    How do I write a regex to match all single characters in a string?

  17. 17

    How do I write an SQL query with a WHERE = 'all'?

  18. 18

    How do I write a custom init for a UIView subclass in Swift?

  19. 19

    How do I write a build pre-action script in Swift?

  20. 20

    How do I write a generic recursive function that takes a CollectionType in Swift?

  21. 21

    How to write Swift generic functions of enum types?

  22. 22

    How do I map two [String] into one [Record] in swift?

  23. 23

    How do I map two [String] into one [Record] in swift?

  24. 24

    How do I write a Dart web app that uses spawnUri and works with both Dartium and dart2js?

  25. 25

    How do I write a Dart web app that uses spawnUri and works with both Dartium and dart2js?

  26. 26

    Core Data: How do I delete all objects with an attribute in Swift?

  27. 27

    How do I write a list/array of custom types into HDF5 file?

  28. 28

    How do I write my REST API call when expecting two types of return Objects

  29. 29

    How do I write a batch script that iterates through all png files in all subfolders and does an action?

HotTag

Archive