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

You can eliminate the need for the second fact function by providing a default for the accumulator.

Code
Diff
  • tailrec fun fact(n: Int, accumulator: Int = 1): Int = if (n == 0) accumulator else fact(n - 1, n * accumulator)
    
    • tailrec fun fact(n: Int, accumulator: Int): Int = if (n == 0) accumulator else fact(n - 1, n * accumulator)
    • fun fact(n: Int): Int = fact(n, 1)
    • tailrec fun fact(n: Int, accumulator: Int = 1): Int = if (n == 0) accumulator else fact(n - 1, n * accumulator)
Functional Programming
Declarative Programming
Programming Paradigms
Monads
Data Structures
Code
Diff
  • #import combinators.lc
    B  = \ f g x . f (g x)
    BB = \ f g x y . f (g x y)
    C  = \ f x y . f y x
    I  = \ x . x
    K  = \ x _ . x
    KI = \ _ x . x
    S  = \ f g x . f x (g x)
    T  = \ x f . f x
    V  = \ x y f . f x y
    W  = \ f x . f x x
    Y = \ f . f (Y f)
    
    #import church-boolean.lc
    True = K
    False = KI
    and = W C
    
    #import church-number.lc
    succ = S B
    is-zero = V (K False) True
    add = \ m n . \ f x . n f (m f x)
    sub = #inline scott-stream.lc
          T  V           \ cons   .
          T (T K)        \ head   .
          T (T KI)       \ tail   .
          T (Y (S cons)) \ repeat .
          # factor out go
          T (S (C cons) (B succ head)) \ go .
          \ m n . head (n tail (m go (repeat 0)))
    le = BB is-zero sub
    eq = \ m n . and (le m n) (le n m)
    
    #import scott-pair.lc
    Pair = V
    fst = T K
    
    #import state-monad.lc
    return = Pair
    bind = C
    fmap = \ fn ma . bind ma \ a . return (fn a)
    lift2 = \ fn ma mb . bind ma \ a . bind mb \ b . return (fn a b)
    get = W Pair
    put = B K (Pair ())
    evalState = B fst
    modify = \ fn . bind get (B put fn)
    gets = \ fn . bind get (B return fn)
    
    #import scott-list.lc
    nil = K
    cons = BB K V
    foldr = \ fn z xxs . xxs z \ x xs . fn x (foldr fn z xs)
    filterM = \ p . foldr ( \ x . lift2 (V (cons x) I) (p x) ) (return nil)
    
    #import scott-int-int-map.lc
    Empty = \ empty _branch . empty
    Branch = \ i x l r . \ _empty branch . branch i x l r
    # index :: Map Number Number -> Number -> Number | ()
    index = \ bst i . bst () \ j x l r .
      eq i j
        x
      (le i j
        (index l i)
      # else
        (index r i)
      )
    # insert-with : (Number -> Number -> Number) -> Number -> Number -> Map Number Number -> Map Number Number
    #    (new value -> old value -> newer value) -> key    -> value  ->              (map -> map)
    insert-with = \ fn i x bst . bst (Branch i x Empty Empty) \ j y l r .
      eq i j
        (Branch i (fn x y) l r)
      (le i j
        (Branch j y (insert-with fn i x l) r)
      # else
        (Branch j y l (insert-with fn i x r))
      )
    
    # deleteNth :: List Number -> Number -> List Number
    delete-nth = \ xs n .
      T ( \ x .
            bind (modify (insert-with add x 1)) \ _ .
                  fmap (C le n) (gets (C index x))
        ) \ go .
      evalState (filterM go xs) Empty
    • #import combinators.lc
    • B = \ f g x . f (g x)
    • BB = \ f g x y . f (g x y)
    • C = \ f x y . f y x
    • I = \ x . x
    • K = \ x _ . x
    • KI = \ _ x . x
    • S = \ f g x . f x (g x)
    • T = \ x f . f x
    • V = \ x y f . f x y
    • W = \ f x . f x x
    • Y = \ f . f (Y f)
    • #import church-boolean.lc
    • True = K
    • True = K
    • False = KI
    • not = C
    • and = W C
    • #import scott-pair.lc
    • Pair = V
    • fst = T K
    • #import church-number.lc
    • succ = S B
    • is-zero = V (K False) True
    • add = \ m n . \ f x . n f (m f x)
    • sub = #inline scott-stream.lc
    • T V \ cons .
    • T (T K) \ head .
    • T (T KI) \ tail .
    • T (Y (S cons)) \ repeat .
    • # factor out go
    • T (S (C cons) (B succ head)) \ go .
    • \ m n . head (n tail (m go (repeat 0)))
    • le = BB is-zero sub
    • eq = \ m n . and (le m n) (le n m)
    • #import scott-list.lc
    • nil = K
    • cons = BB K V
    • foldr = \ fn z xxs . xxs z \ x xs . fn x (foldr fn z xs)
    • foldl = \ fn z xxs . xxs z \ x xs . foldl fn (fn z x) xs
    • length = foldr (K succ) 0
    • reverse = foldl (C cons) nil
    • filter = \ p . foldr ( \ x z . p x (cons x z) z ) nil
    • #import scott-assoc-list.lc # TODO: replace with scott-map for performance
    • empty = nil
    • # index = lookup : assocList i x -> i -> x | ()
    • index = \ assocs j . assocs () \ assoc xs . assoc \ i x . eq i j x (index xs j)
    • insert-with = \ fn j y assocs . assocs (cons (Pair j y) nil) \ assoc xs . assoc \ i x . eq i j (cons (Pair i (fn x y)) xs) (cons assoc (insert-with fn j y xs))
    • # -- State --
    • #import scott-state-monad.lc
    • # State s a = State { runState :: s -> (,) a s }
    • # State = (,) a # forget the type parameters, the wrapper and the unwrapper
    • #import scott-pair.lc
    • Pair = V
    • fst = T K
    • # return : a -> State s a
    • # return = \ a . \ s . Pair a s
    • #import state-monad.lc
    • return = Pair
    • # bind : State s a -> (a -> State s b) -> State s b
    • # bind = \ ma fn . \ s . let (a,s') = ma s in fn a s'
    • # bind = \ ma fn . \ s . ma s \ a s' . fn a s'
    • # bind = \ ma fn s . ma s fn
    • bind = C
    • # get :: State s s
    • # get = \ s . Pair s s
    • fmap = \ fn ma . bind ma \ a . return (fn a)
    • lift2 = \ fn ma mb . bind ma \ a . bind mb \ b . return (fn a b)
    • get = W Pair
    • # put :: s -> State s ()
    • # put s = \ _ . Pair () s
    • put = B K (Pair ())
    • # -- helpers --
    • # fmap :: (Monad m) => (a -> b) -> m a -> m b
    • # liftM fn ma = do a <- ma ; return (fn a)
    • fmap = \ fn ma .
    • bind ma \ a .
    • return (fn a)
    • # (<*>) :: (Monad m) => m (a -> b) -> m a -> m b
    • # ap mfn ma = do fn <- mfn ; a <- ma ; return (fn a)
    • ap = \ mfn ma .
    • bind mfn \ fn .
    • bind ma \ a .
    • return (fn a)
    • # liftM2 fn x y = fn <$> x <*> y
    • lift2 = \ fn ma mb . ap (fmap fn ma) mb
    • # join :: (Monad m) => m (m a) -> m a
    • # join mma = mma >>= id
    • join = \ mma . bind mma I
    • # modify :: (s -> s) -> State s ()
    • # modify = put . fn =<< get
    • evalState = B fst
    • modify = \ fn . bind get (B put fn)
    • # gets :: (s -> t) -> State s t
    • # gets = return . fn =<< get
    • gets = \ fn . bind get (B return fn)
    • # runState : State s a -> s -> (a,s)
    • # runState = I # there are no wrappers
    • # evalState :: State s a -> s -> a
    • # evalState ma s = fst (ma s)
    • evalState = B fst
    • # filterM :: (Applicative m) => (a -> m Bool) -> [a] -> m [a]
    • # filterM p = foldr (\ x -> liftA2 (\ flg -> if flg then (x:) else id) (p x)) (pure [])
    • #import scott-list.lc
    • nil = K
    • cons = BB K V
    • foldr = \ fn z xxs . xxs z \ x xs . fn x (foldr fn z xs)
    • filterM = \ p . foldr ( \ x . lift2 (V (cons x) I) (p x) ) (return nil)
    • # -- solution --
    • # deleteNth :: [Int] -> Int -> [Int]
    • # deleteNth xs n = evalState (filterM go xs) empty where
    • # go :: Int -> State (IntMap Int) Bool
    • # go x = do
    • # modify $ insertWith (+) x 1
    • # (<= n) <$> gets (! x)
    • #import scott-int-int-map.lc
    • Empty = \ empty _branch . empty
    • Branch = \ i x l r . \ _empty branch . branch i x l r
    • # index :: Map Number Number -> Number -> Number | ()
    • index = \ bst i . bst () \ j x l r .
    • eq i j
    • x
    • (le i j
    • (index l i)
    • # else
    • (index r i)
    • )
    • # insert-with : (Number -> Number -> Number) -> Number -> Number -> Map Number Number -> Map Number Number
    • # (new value -> old value -> newer value) -> key -> value -> (map -> map)
    • insert-with = \ fn i x bst . bst (Branch i x Empty Empty) \ j y l r .
    • eq i j
    • (Branch i (fn x y) l r)
    • (le i j
    • (Branch j y (insert-with fn i x l) r)
    • # else
    • (Branch j y l (insert-with fn i x r))
    • )
    • # deleteNth :: List Number -> Number -> List Number
    • delete-nth = \ xs n .
    • T ( \ x .
    • bind (modify (insert-with add x 1)) \ _ .
    • fmap (C le n) (gets (C index x))
    • ) \ go .
    • evalState (filterM go xs) empty
    • evalState (filterM go xs) Empty
Code
Diff
  • def find_first_sub_string(text: str, 
                              sub: str):
        return index if ~(index:= text.find(sub)) else None
    • def find_first_sub_string(text: str, sub: str):
    • index = text.find(sub)
    • return index if index != -1 else None
    • def find_first_sub_string(text: str,
    • sub: str):
    • return index if ~(index:= text.find(sub)) else None
Code
Diff
  • power = pow
    • power=lambda n,p,r=1:power(n,p-1,r*n)if p else r
    • power = pow
Code
Diff
  • fun List<Int>.remove(vararg nums: Int) = filter { !nums.contains(it) }
    • def remove(integers, values):
    • return list(filter(lambda x: x not in values,integers))
    • fun List<Int>.remove(vararg nums: Int) = filter { !nums.contains(it) }