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.
class Component { constructor(dom) { this.dom = dom; } onCreate() { console.log('onCreate from parent class'); return 'missing'; } static on(event, callback) { callback(); } async emit(event, data) {} } class Title extends Component { onCreate() { super.onCreate() return 'super!' } }
- class Component {
- constructor(dom) {
- this.dom = dom;
- }
- onCreate() {
- console.log('onCreate from parent class');
- return 'missing';
- }
- static on(event, callback) {
- callback();
- }
- async emit(event, data) {}
- }
- class Title extends Component {
- onCreate() {
- super.onCreate()
- return 'super!'
- }
- }
let results = [ {materia: {conteudo: {titulo: 'São Paulo'}}, tags: [1, 2, 3]}, {materia: {conteudo: {}}, tags: [3, 2]}, {materia: {conteudo: {titulo: 'Rio de Janeiro'}}, tags: [3, 2]}, ]; for (const { materia: { conteudo: {titulo = 'Brasil'}}} of results) { console.log(titulo); }
- let results = [
- {materia: {conteudo: {titulo: 'São Paulo'}}, tags: [1, 2, 3]},
- {materia: {conteudo: {}}, tags: [3, 2]},
- {materia: {conteudo: {titulo: 'Rio de Janeiro'}}, tags: [3, 2]},
- ];
for (const result of results) {let titulo = result.materia.conteudo.titulo || 'Brasil';- for (const { materia: { conteudo: {titulo = 'Brasil'}}} of results) {
- console.log(titulo);
- }
// Exercício 1 const titulo = "UOL - O melhor conteúdo"; // Exercício 2 const tags = [] tags.push(...['A', 'B']); // Exercício 3 let descricao = "Em 1999"; descricao += " em São Paulo"; // Exercício 4 let materia = {titulo: "Barão de Limeira"}; materia.titulo = "Alameda " + materia.titulo; // Exercício 5 for (let i = 10; i--;) { console.log(i); } // Exercício 6 for (let tag of ['A', 'B']) { console.log(tag); } // Exercício 7 for (let j = [].length; j < [].length; j--) { if (j === -1) { console.log('Não encontrei'); } } // Exercício 8 let a = 123; { a *= 2; } console.log(a); // Exercício 9 let state = 'active'; function stop() { state = 'paused'; } stop(); // Exercício 10 const TRUE = !0;
- // Exercício 1
var titulo = "UOL - O melhor conteúdo";- const titulo = "UOL - O melhor conteúdo";
- // Exercício 2
var tags = []- const tags = []
- tags.push(...['A', 'B']);
- // Exercício 3
var descricao = "Em 1999";- let descricao = "Em 1999";
- descricao += " em São Paulo";
- // Exercício 4
var materia = {titulo: "Barão de Limeira"};- let materia = {titulo: "Barão de Limeira"};
- materia.titulo = "Alameda " + materia.titulo;
- // Exercício 5
for (var i = 10; i--;) {- for (let i = 10; i--;) {
- console.log(i);
- }
- // Exercício 6
for (var tag of ['A', 'B']) {- for (let tag of ['A', 'B']) {
- console.log(tag);
- }
- // Exercício 7
for (var j = [].length; j--;) {}- for (let j = [].length; j < [].length; j--) {
- if (j === -1) {
- console.log('Não encontrei');
- }
- }
- // Exercício 8
var a = 123;- let a = 123;
- {
- a *= 2;
- }
- console.log(a);
- // Exercício 9
var state = 'active';- let state = 'active';
- function stop() {
- state = 'paused';
- }
- stop();
- // Exercício 10
var TRUE = !0;- const TRUE = !0;
The idea here is to provide the ability to process a batch of callbacks. It provides the following capabilities:
- dynamic batch list, can be exanded at any time
- each callback is isolated, if an error is thrown it will not prevent the next callback from being called
- each callback has a timeout, so that if it does not call the next function itself, it will eventually be called automatically.
- batches can be ran at intervals, either forever, or up to configured amount of times
- events are provided to hook into batchStart, batchEnd, error and stopped
- nextTick can automatically be called for each item within the batch
// interval processor's job is to run a batch of callback items, one at a time. It runs these batches
// at a given interval, and will continue to run batches either forever or until a maxBatches value is reached.
var Processor = function(interval)
{
this.nextTick = true;
this.interval = interval;
this.timeout = 50;
this.items = [];
this.maxBatch = null;
this.events = {error: [], batchStart: [], batchEnd: [], stopped: [], timeout: []};
console.log('Interval Processor created with ' + interval + 'ms interval');
}
Processor.prototype.on = function on(event, callback)
{
this.events[event].push(callback);
}
// returns a new function that ensures that the passed in fn only gets called once no matter
// how many times the returned function is called.
function once(fn)
{
var called = false;
return function()
{
if (!called)
{
if (fn) fn();
called = true;
}
}
}
function fire(processor, event, arg)
{
processor.events[event].forEach(function(fn)
{
fn(processor, arg);
});
}
function nextTick(run)
{
return function(i)
{
process.nextTick(function()
{
run(i);
});
}
};
// add a new callback to the processor chain.
Processor.prototype.add = function add(item)
{
var self = this;
this.items.push(function(next)
{
var _next = once(function(){
clearTimeout(timeout);
next();
});
// set a timeout so that if for some reason the item fails and doesn't call its callback,
// we make sure that the chain doesn't break.
var timeout = setTimeout(function()
{
fire(self, 'timeout')
_next();
}, self.timeout);
try
{
item(_next);
}
catch(ex)
{
fire(self, 'error', ex);
_next();
}
});
}
// stop any additional iterations from happening
Processor.prototype.stop = function stop()
{
this.started = false;
clearTimeout(this._timeout);
fire(this, 'stopped');
}
// start the loop
Processor.prototype.start = function start()
{
// if not started yet and there are items to process
if (!this.started && this.items.length > 0)
{
var self = this;
self.started = true;
self.batch = 0;
function run(i)
{
i = i || 0;
// call the runStart event
if (i == 0)
{
self.batch++;
fire(self, 'batchStart');
}
// if not the end of the loop
if (i < self.items.length)
{
// run the current item, pass in the next run as the next function
self.items[i](function(){ run(++i);})
}
// if no more items to process then check if still running and if so then schedule the next batch
else if (self.started)
{
fire(self, 'batchEnd');
if (self.maxBatches && self.maxBatches == self.batch)
{
self.stop();
}
else
{
self._timeout = setTimeout(run, self.interval);
}
}
}
// if nextTick is true then wrap each run within a nextTick call
if (this.nextTick)
{
run = nextTick(run);
}
// run the first batch now
run();
return true;
}
return false;
}
module.exports = Processor;
describe('Processor', function(){
var processor = new Processor(25);
var a = 0, b = 0;
processor.add(function(next){
console.log(1);
a++;
next();
});
processor.add(function(next){
console.log(2);
b++;
throw 'Example error';
});
processor.on('timeout', function(){
console.log('Timed out');
});
processor.on('batchEnd', function(){
console.log("Batch ended");
});
processor.on('error', function(_, error){
console.log('Error was raised: ' + error.toString());
});
it('should run only n amount of maxBatches', function(){
processor.maxBatches = 3;
processor.on('stopped', function(){
Test.assertEquals(a, 3);
Test.assertEquals(b, 3);
});
processor.start();
});
});
public class Program { public static String digitToText(int digit) { final String[] nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; return nums[digit]; } }
const nums = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']function digitToText(digit) {return nums[digit]}- public class Program {
- public static String digitToText(int digit) {
- final String[] nums = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
- return nums[digit];
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void test() { assertEquals("zero", Program.digitToText(0)); assertEquals("one", Program.digitToText(1)); assertEquals("two", Program.digitToText(2)); assertEquals("three", Program.digitToText(3)); assertEquals("four", Program.digitToText(4)); assertEquals("five", Program.digitToText(5)); assertEquals("six", Program.digitToText(6)); assertEquals("seven", Program.digitToText(7)); assertEquals("eight", Program.digitToText(8)); assertEquals("nine", Program.digitToText(9)); } }
// Since Node 10, we're using Mocha.// You can use `chai` for assertions.const chai = require("chai");const assert = chai.assert;// Uncomment the following line to disable truncating failure messages for deep equals, do:// chai.config.truncateThreshold = 0;// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.// Uncomment the following to use the old assertions:// const Test = require("@codewars/test-compat");- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
describe("Solution", function() {it("should test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);assert.strictEqual(digitToText(1), 'one')assert.strictEqual(digitToText(2), 'two')assert.strictEqual(digitToText(3), 'three')assert.strictEqual(digitToText(4), 'four')assert.strictEqual(digitToText(5), 'five')assert.strictEqual(digitToText(6), 'six')assert.strictEqual(digitToText(7), 'seven')assert.strictEqual(digitToText(8), 'eight')assert.strictEqual(digitToText(9), 'nine')assert.strictEqual(digitToText(0), 'zero')assert.strictEqual(digitToText(11), undefined)});});- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void test() {
- assertEquals("zero", Program.digitToText(0));
- assertEquals("one", Program.digitToText(1));
- assertEquals("two", Program.digitToText(2));
- assertEquals("three", Program.digitToText(3));
- assertEquals("four", Program.digitToText(4));
- assertEquals("five", Program.digitToText(5));
- assertEquals("six", Program.digitToText(6));
- assertEquals("seven", Program.digitToText(7));
- assertEquals("eight", Program.digitToText(8));
- assertEquals("nine", Program.digitToText(9));
- }
- }
Optimized it a bit
function sumNechet(a, b) { let sum = 0; for(let i = a; i <=b; i++) { if(нечетное(i)) sum += i; } return sum; } function нечетное(число) { return число % 2 === 1; }
- function sumNechet(a, b) {
- let sum = 0;
- for(let i = a; i <=b; i++) {
- if(нечетное(i)) sum += i;
- }
- return sum;
- }
- function нечетное(число) {
- return число % 2 === 1;
- }
fn add_arr(arr: &[i32]) -> i32 { arr.iter().sum() }
function addArr(arr){if(arr.length === 0) return nulllet final = 0arr.forEach(num => {final += num})return final- fn add_arr(arr: &[i32]) -> i32 {
- arr.iter().sum()
- }
#[test] fn test() { assert_eq!(add_arr(&[1, 2, 3, 4, 5]), 15); assert_eq!(add_arr(&[1, 100]), 101); assert_eq!(add_arr(&[]), 0); }
const chai = require("chai");const assert = chai.assert;describe("Solution", function() {it("should test for something", function() {assert.strictEqual(addArr([1, 2, 3, 4, 5]), 15);assert.strictEqual(addArr([1, 100]), 101)assert.strictEqual(addArr([]), null)});});- #[test]
- fn test() {
- assert_eq!(add_arr(&[1, 2, 3, 4, 5]), 15);
- assert_eq!(add_arr(&[1, 100]), 101);
- assert_eq!(add_arr(&[]), 0);
- }