bkietz commented on code in PR #36496:
URL: https://github.com/apache/arrow/pull/36496#discussion_r1307693610


##########
cpp/src/arrow/extension/fixed_shape_tensor.cc:
##########
@@ -104,6 +104,38 @@ bool FixedShapeTensorType::ExtensionEquals(const 
ExtensionType& other) const {
          permutation_equivalent;
 }
 
+std::string FixedShapeTensorType::ToString() const {
+  std::stringstream ss;
+  ss << "extension<" << this->extension_name()
+     << "[value_type=" << value_type_->ToString() << ", shape=[";
+  std::string separator;
+  for (auto v : shape_) {
+    ss << separator << v;
+    separator = ",";
+  }
+  ss << "]";
+  if (!permutation_.empty()) {
+    ss << ", permutation=[";
+    std::string p_separator;
+    for (auto v : permutation_) {
+      ss << p_separator << v;
+      p_separator = ",";
+    }
+    ss << "]";
+  }
+  if (!dim_names_.empty()) {
+    ss << ", dim_names=[";
+    std::string d_separator;
+    for (std::string v : dim_names_) {
+      ss << d_separator << v;
+      d_separator = ",";
+    }
+    ss << "]";
+  }
+  ss << "]>";
+  return ss.str();
+}
+

Review Comment:
   The better API idea is to vendor `fmt` to access [c++20's improved string 
utils](https://fmt.dev/latest/api.html#_CPPv4I0EN3fmt4joinE9join_viewIN6detail10iterator_tI5RangeEEN6detail10sentinel_tI5RangeEEERR5Range11string_view)
   
   ```
   std::vector<int> v = {1, 2, 3};
   fmt::print("{}", fmt::join(v, ", "));
   // Output: "1, 2, 3"
   ```
   
   In lieu of that, we could add a similar template which relies on lazy 
stringification through `std::ostream`:
   
   ```
   template <typename Range, typename Separator>
   struct JoinStrings {
     const Range& range_;
     const Separator& separator_;
   
     template <typename Os> // template to dodge inclusion of <ostream>
     friend Os& operator<<(Os& os, JoinStrings l) {
       bool first = true;
       for (const auto& element : l.range_) {
         if (first) {
           first = false;
         } else {
           os << l.separator_;
         }
         os << ToChars(element); // use ToChars to avoid locale dependence
       }
       return os;
     }
   };
   
   void use() {
     std::vector<int> v{1,2,3};
     std::stringstream ss;
     ss << JoinStrings{v, ", "};
   }
   ```
   
   ... note that this template can only be used with an ostream, but it seems 
that's mostly where the resulting output would end up anyway



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to