Dictionary as a generic element of Array











up vote
1
down vote

favorite












I have an extension for an Array:



extension Array where Element == [String:Double] {
func values (keyOrder : [String]) -> [[Double]] {
return self.map { element in
return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
}
}
}


It works pretty well, but only if Dictionary Key is String and Value is Double. I can imagine this function could work exactly same way for Dictionary of any types, like [AnyHashable:Any] but I have no clue how to define header, is it possible?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I have an extension for an Array:



    extension Array where Element == [String:Double] {
    func values (keyOrder : [String]) -> [[Double]] {
    return self.map { element in
    return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
    }
    }
    }


    It works pretty well, but only if Dictionary Key is String and Value is Double. I can imagine this function could work exactly same way for Dictionary of any types, like [AnyHashable:Any] but I have no clue how to define header, is it possible?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have an extension for an Array:



      extension Array where Element == [String:Double] {
      func values (keyOrder : [String]) -> [[Double]] {
      return self.map { element in
      return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
      }
      }
      }


      It works pretty well, but only if Dictionary Key is String and Value is Double. I can imagine this function could work exactly same way for Dictionary of any types, like [AnyHashable:Any] but I have no clue how to define header, is it possible?










      share|improve this question













      I have an extension for an Array:



      extension Array where Element == [String:Double] {
      func values (keyOrder : [String]) -> [[Double]] {
      return self.map { element in
      return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
      }
      }
      }


      It works pretty well, but only if Dictionary Key is String and Value is Double. I can imagine this function could work exactly same way for Dictionary of any types, like [AnyHashable:Any] but I have no clue how to define header, is it possible?







      swift generics






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 14 hours ago









      Łukasz

      253113




      253113
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          One useful trick you can use in situations like this is to move the where clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key and Value placeholder types:



          extension Array {
          func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
          return map { element in
          return keys.compactMap { element[$0] }
          }
          }
          }





          share|improve this answer





















          • Wonderful! No one-time used protocol! Kiss!
            – Łukasz
            10 hours ago


















          up vote
          -1
          down vote













          Use can use value of dictionary double as an Any Type. You can try below code.



          extension Array where Element == [String: Any] {
          func values (keyOrder : [String]) -> [[Any]] {
          return self.map { element in
          return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
          }
          }
          }





          share|improve this answer





















          • But what to do with Key?
            – Łukasz
            14 hours ago










          • You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
            – Anand Verma
            13 hours ago












          • I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
            – Łukasz
            13 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371946%2fdictionary-as-a-generic-element-of-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          One useful trick you can use in situations like this is to move the where clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key and Value placeholder types:



          extension Array {
          func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
          return map { element in
          return keys.compactMap { element[$0] }
          }
          }
          }





          share|improve this answer





















          • Wonderful! No one-time used protocol! Kiss!
            – Łukasz
            10 hours ago















          up vote
          2
          down vote



          accepted










          One useful trick you can use in situations like this is to move the where clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key and Value placeholder types:



          extension Array {
          func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
          return map { element in
          return keys.compactMap { element[$0] }
          }
          }
          }





          share|improve this answer





















          • Wonderful! No one-time used protocol! Kiss!
            – Łukasz
            10 hours ago













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          One useful trick you can use in situations like this is to move the where clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key and Value placeholder types:



          extension Array {
          func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
          return map { element in
          return keys.compactMap { element[$0] }
          }
          }
          }





          share|improve this answer












          One useful trick you can use in situations like this is to move the where clause from the extension declaration to the method declaration. This allows you to introduce new generic placeholders for the nested dictionary's Key and Value placeholder types:



          extension Array {
          func nestedValues<Key, Value>(orderedBy keys: [Key]) -> [[Value]] where Element == [Key: Value] {
          return map { element in
          return keys.compactMap { element[$0] }
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 10 hours ago









          Hamish

          43.4k797147




          43.4k797147












          • Wonderful! No one-time used protocol! Kiss!
            – Łukasz
            10 hours ago


















          • Wonderful! No one-time used protocol! Kiss!
            – Łukasz
            10 hours ago
















          Wonderful! No one-time used protocol! Kiss!
          – Łukasz
          10 hours ago




          Wonderful! No one-time used protocol! Kiss!
          – Łukasz
          10 hours ago












          up vote
          -1
          down vote













          Use can use value of dictionary double as an Any Type. You can try below code.



          extension Array where Element == [String: Any] {
          func values (keyOrder : [String]) -> [[Any]] {
          return self.map { element in
          return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
          }
          }
          }





          share|improve this answer





















          • But what to do with Key?
            – Łukasz
            14 hours ago










          • You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
            – Anand Verma
            13 hours ago












          • I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
            – Łukasz
            13 hours ago















          up vote
          -1
          down vote













          Use can use value of dictionary double as an Any Type. You can try below code.



          extension Array where Element == [String: Any] {
          func values (keyOrder : [String]) -> [[Any]] {
          return self.map { element in
          return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
          }
          }
          }





          share|improve this answer





















          • But what to do with Key?
            – Łukasz
            14 hours ago










          • You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
            – Anand Verma
            13 hours ago












          • I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
            – Łukasz
            13 hours ago













          up vote
          -1
          down vote










          up vote
          -1
          down vote









          Use can use value of dictionary double as an Any Type. You can try below code.



          extension Array where Element == [String: Any] {
          func values (keyOrder : [String]) -> [[Any]] {
          return self.map { element in
          return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
          }
          }
          }





          share|improve this answer












          Use can use value of dictionary double as an Any Type. You can try below code.



          extension Array where Element == [String: Any] {
          func values (keyOrder : [String]) -> [[Any]] {
          return self.map { element in
          return (0..<keyOrder.count).compactMap {element[keyOrder[$0]]}
          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 14 hours ago









          Anand Verma

          25026




          25026












          • But what to do with Key?
            – Łukasz
            14 hours ago










          • You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
            – Anand Verma
            13 hours ago












          • I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
            – Łukasz
            13 hours ago


















          • But what to do with Key?
            – Łukasz
            14 hours ago










          • You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
            – Anand Verma
            13 hours ago












          • I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
            – Łukasz
            13 hours ago
















          But what to do with Key?
          – Łukasz
          14 hours ago




          But what to do with Key?
          – Łukasz
          14 hours ago












          You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
          – Anand Verma
          13 hours ago






          You can't set key of different type in single Dictionary. You have to explicitly casting keys into String. like int a = 10; dict["(a)"] = "Set Key as an String from Int". (map["group_id"], TransformOf<String, Int>(fromJSON: { String($0!) }, toJSON: { $0.map { Int($0)! } }))
          – Anand Verma
          13 hours ago














          I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
          – Łukasz
          13 hours ago




          I don't understand you. I can have different Dictionaries, key must me hashable. [AnyHashable:Any]. But I don't know is it possible to define them as a type in extension header to use them inside a function to have an output same type as is defined as Element Dictionary value.
          – Łukasz
          13 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371946%2fdictionary-as-a-generic-element-of-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]