Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

Ad
Ad
Code
Diff
  • using System.Linq;
    
    public class Program
    {
      public static int FindSquaresInArray(int[] arr){
        return arr.Sum(x => x * x);
      } 
    }
    • using System.Linq;
    • public class Program
    • {
    • public static int FindSquaresInArray(int[] arr) => arr.Sum(x => x * x);
    • public static int FindSquaresInArray(int[] arr){
    • return arr.Sum(x => x * x);
    • }
    • }
Code
Diff
  • print(f'{chr(72)}{chr(101)}{2 * chr(108)}{chr(111)}{chr(44)}{chr(32)}{chr(87)}{chr(111)}{chr(114)}{chr(108)}{chr(100)}{2 * chr(33)}')
    • print(
    • chr(72) +
    • chr(101) +
    • 2 * chr(108) +
    • chr(111) +
    • chr(44) +
    • chr(32) +
    • chr(87) +
    • chr(111) +
    • chr(114) +
    • chr(108) +
    • chr(100) +
    • 2 * chr(33)
    • )
    • print(f'{chr(72)}{chr(101)}{2 * chr(108)}{chr(111)}{chr(44)}{chr(32)}{chr(87)}{chr(111)}{chr(114)}{chr(108)}{chr(100)}{2 * chr(33)}')

Constructor situation is a bit better now. Thunks are now copyable and are more flexibly constructible.

Background

C++ recreation of the Thunk API from this Kata.

Test cases are included to verify if it works as expected. The full extent of the class is used in those test cases.

Code
Diff
  • ;
    • //
    • ;

This algorithm of 'sum()' is SFINAE-friendly. By first testing if the corresponding types can be added with the type trait 'has_plus' because some types doesn't have the 'operator+' defined. If for any reason, the types doesn't have the addition operator available, the function will be disabled via 'std::enable_if' and will be discarded by SFINAE. In constrast, if the types can be added, the 'std::enable_if' trait will enable the function and returning the corresponding type of add the two types with the 'plus_result' trait.

Code
Diff
  • #include <vector>
    #include <numeric>
    #include <type_traits>
    #include <utility>
    
    namespace
    {
      template<typename, typename, typename = void>
      struct has_plus : std::false_type {};
    
      template<typename T1, typename T2>
      struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
    
      template<typename T1, typename T2>
      inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
    
      template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
      struct plus_result
      {
        using type = decltype(std::declval<T1>() + std::declval<T2>());
      };
      
      template<typename T1, typename T2>
      struct plus_result<T1, T2, false> {};
      
      template<typename T1, typename T2>
      using plus_result_t = typename plus_result<T1, T2>::type;
      
      template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
      plus_result_t<T, T> sum(const std::vector<T>& v)
      {
        return std::accumulate(v.cbegin(), v.cend(), T{});
      }
    }
    • #include <vector>
    • #include <numeric>
    • #include <type_traits>
    • #include <utility>
    • template <typename T> using add_t=decltype(T() + T());
    • namespace
    • {
    • template<typename, typename, typename = void>
    • struct has_plus : std::false_type {};
    • template <typename T = double>
    • add_t<T> sum(const std::vector<T>& v) {
    • return std::accumulate(v.cbegin(),v.cend(),add_t<T>{0});
    • template<typename T1, typename T2>
    • struct has_plus<T1, T2, std::void_t<decltype(std::declval<T1>() + std::declval<T2>())>> : std::true_type {};
    • template<typename T1, typename T2>
    • inline constexpr bool has_plus_v = has_plus<T1, T2>::value;
    • template<typename T1, typename T2, bool = has_plus_v<T1, T2>>
    • struct plus_result
    • {
    • using type = decltype(std::declval<T1>() + std::declval<T2>());
    • };
    • template<typename T1, typename T2>
    • struct plus_result<T1, T2, false> {};
    • template<typename T1, typename T2>
    • using plus_result_t = typename plus_result<T1, T2>::type;
    • template<typename T = double, typename = std::enable_if_t<has_plus_v<T, T>>>
    • plus_result_t<T, T> sum(const std::vector<T>& v)
    • {
    • return std::accumulate(v.cbegin(), v.cend(), T{});
    • }
    • }