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

Exercício:

A função "a" deve retornar uma promise sem usar a classe Promise.

Modifique a função "b" de forma que execute a promise de "a" sem usar then/catch.

function a() {
  return new Promise(function (resolve, reject) {
    resolve('ok');
  });
}

function b() {
  a().then(function(res) {
    console.log(res);
  }).catch(function(err) {
    console.log(err);
  })
}
module Example where
//
Testing
Frameworks

A slightly hacky way to reproduce the timing information after the runner got the outermost "Test" group removed.

module Example where

add = (+)

Just a demo that passing -Ox flags does work (GHC 8 only).

{-# OPTIONS_GHC -O2 -optc-O3 #-}

module Example where

factorial n = product [1..n]

index.js is a combination of preloaded, code and test cases with a bit of error handling. Other three files are required modules to run the tests.

const fs = require('fs')
//console.log(this)
const file1 = fs.readFileSync('/home/codewarrior/index.js')
console.log(file1.toString())
const file2 = fs.readFileSync('/runner/frameworks/javascript/cw-2.js')
console.log(file2.toString())
const file3 = fs.readFileSync('/runner/frameworks/javascript/chai-display.js')
console.log(file3.toString())
const file4 = fs.readFileSync('/runner/frameworks/javascript/display.js')
console.log(file4.toString())
UnnamedFailed Tests

fast-check

function add1(a, b) {
  return a + b;
}

function add2(a, b) {
  return a + b + 1;
}

function add3(a, b) {
  throw new Error('boo');
}

We are working for a Delivery company.This Kata requires the user to calculate the closest daily routes for Delivery based on the nearest location first to be delivered in the daily routes of deliveries to be made.

There is a single method - public List<List> ClosestXDestinations(int numDestinations,
List<List> allLocations, int numDeliveries), which takes in 3 parameters.

  1. numDestinations - the number of destinations for the driver to deliver products to for today.
  2. allLocations - A list of list having all locations of x and y coordinates of locations.
  3. numDeliveries - the number of deliveries to be made today.
  
The closest destination is calculated as below.

Example:
--------
input:

numDestinations = 3
allLocations = [[1,2],[3,4],[1,-1]]
numDeliveries = 1

Output:

[[1,-1]]

Explanation:
The distance of the truck from location [1,2] is squareroot(5) = 2.236
The distance of the truck from location [3,4] is squareroot(25) = 5
The distance of the truck from location [1,-1] is squareroot(2) = 1.141
numDeliveries is 1, hence the output is [1,-1]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

public class NearestDelivery {

	Integer val = 0;
	int x = 0, y = 0;

	public List<List<Integer>> ClosestXDestinations(int numDestinations,
			List<List<Integer>> allLocations, int numDeliveries) {
		TreeMap<Double, List<Integer>> sortedMap = new TreeMap<>();
		for (int i = 0; i < allLocations.size(); i++) {
			List<Integer> list = allLocations.get(i);
			if (!list.isEmpty()) {
				if (list.size() == 1) {
					x = ((Integer) list.get(0)).intValue();
					y = 0;
				} else if (list.size() == 2) {
					x = ((Integer) list.get(0)).intValue();
					y = ((Integer) list.get(1)).intValue();
				}

				val = x * x + y * y;
				double dVal = Math.sqrt(val);
				sortedMap.put(dVal, list);
			}
		}
		ArrayList<List<Integer>> as = new ArrayList<>(sortedMap.values());
		while (as.size() > numDeliveries) {
			as.remove(as.size() - 1);
		}
		return as;
	}


}

The given code barely passes at the limit of 23, even with -O2 enabled on Preloaded. GHC actually does lots of GC during compilation, so I guess we can apply this article to reduce GC times. Unfortunately, +RTS option is not available in OPTIONS_GHC so I need kazk's help here.

{-# LANGUAGE GADTs         #-}
{-# LANGUAGE TypeFamilies  #-}
{-# LANGUAGE TypeOperators #-}

{-# OPTIONS_GHC -Wall -O2 #-}

module Kata.AdditionCommutes
  ( plusCommutes ) where

import Kata.AdditionCommutes.Definitions
  ( Z, S
  , Natural(..), Equal(..)
  , (:+:))

-- | x == x
refl :: Natural n -> Equal n n
refl NumZ     = EqlZ
refl (NumS n) = EqlS (refl n)

-- | a == b -> b == a
sym :: Equal a b -> Equal b a
sym EqlZ     = EqlZ
sym (EqlS p) = EqlS (sym p)

-- | a == b && b == c -> a == c
(<&>) :: Equal a b -> Equal b c -> Equal a c
(<&>) EqlZ EqlZ         = EqlZ
(<&>) (EqlS a) (EqlS b) = EqlS (a <&> b)

-- | s(a) + b == a + s(b)
shove :: Natural a -> Natural b -> Equal (S a :+: b) (a :+: S b)
shove NumZ m     = EqlS (refl m)
shove (NumS n) m = EqlS (shove n m)

-- | a + b == b + a
plusCommutes :: Natural a -> Natural b -> Equal (a :+: b) (b :+: a)
plusCommutes NumZ  NumZ = EqlZ
plusCommutes a (NumS b) = sym (shove a b) <&> EqlS (plusCommutes a b)
plusCommutes (NumS a) b = EqlS (plusCommutes a b) <&> shove b a
kazkFailed Tests

Forth Testing

Experiments to add test support for Forth (Codewars/codewars-runner-cli#625).

The test framework part was originally contributed by @nomennescio.

\ Test Framework (ttester + extension)
decimal
s" test/ttester.fs" included

: #ms ( dmicroseconds -- len c-addr ) <# # # # [char] . hold #s #> ;

: describe#{ ( len c-addr -- ) cr ." <DESCRIBE::>" type cr utime ;
: it#{ ( len c-addr -- ) cr ." <IT::>" type cr utime ;
: }# ( -- ) utime cr ." <COMPLETEDIN::>" 2swap d- #ms type ."  ms" cr ;

create EXPECTED-RESULTS 32 cells allot
variable RESULTS
variable DIFFERENCES

: <{ T{ ;
: }>
  depth ACTUAL-DEPTH @ = if
    depth START-DEPTH @ > if
      depth START-DEPTH @ - dup RESULTS ! 0 do
        dup EXPECTED-RESULTS i cells + !
        ACTUAL-RESULTS i cells + @ <> DIFFERENCES +!
      loop

      DIFFERENCES @ if
        cr ." <FAILED::>expected: "
        RESULTS @ 0 do EXPECTED-RESULTS i cells + @ . loop
        ." <:LF:>  actual: "
        RESULTS @ 0 do ACTUAL-RESULTS i cells + @ . loop
        cr

      else
        cr ." <PASSED::>Test Passed" cr
      then
    then
  else
    cr ." <FAILED::>Wrong number of results. Expected:<:LF:>" ACTUAL-DEPTH @ . ." <:LF:>got:<:LF:>" depth . cr
  then
F} ;

\ Solution
: solution ( a b -- a*b ) * ;

\ Tests
s" Basic Tests" describe#{
  s" zeros" it#{
    <{ 0 0 solution -> 0 }>
    <{ 0 1 solution -> 0 }>
    <{ 1 0 solution -> 0 }>
  }#

  s" non-zeros" it#{
    \ intentionally broken tests
    <{ 1 1 solution -> 2 }>
    <{ 3 5 solution -> 8 }>
  }#
}#