Begin a new Kumite
Search
About
  • Filter by Language:
  • 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.

Here's a more idiomatic/golfy version.

There's a lot more golfing to go.

Also, introducing testing, so now peeps can refactor if they want.

Code
Diff
  • (ns bubble
      (:refer-clojure :exclude [sort]))
    
    (defn bubble [tanks]
      (if (or (nil? tanks) (nil? (next tanks)))
        [tanks false]
        (let [[truck & [jeep & _ :as convoy]] tanks]
          (if (<= truck jeep)
            (let [debrief (bubble convoy)] (cons (cons truck (first debrief)) (next debrief)))
            (cons (cons jeep (first (bubble (cons truck (next convoy))))) '(true))))))
    
    (defn sort [l]
      (let [bubbly (bubble l)]
        (if (second bubbly)
          (recur (first bubbly))
          (first bubbly))))
  • 1
    (defn goes-before? [val1 val2] (<= val1 val2))
    
    1+
    (ns bubble
    
    2+
      (:refer-clojure :exclude [sort]))
    
    22
    3
    ;; bubble: list of number -> list of number
    
    4
    ;; This was originally racket code, 
    
    5
    ;; so it used 'car' and 'cdr' instead of 'first' and 'next'
    
    6
    ;; Why can't clojure keep my theming?
    
    77
    (defn bubble [tanks]
    
    8
      ;; I'm using nested if statements here because I want to use let
    
    99
      (if (or (nil? tanks) (nil? (next tanks)))
    
    10
          (cons tanks '(false))
    
    11
          (let* [truck (first tanks) convoy (next tanks) jeep (first convoy)]
    
    12
             (if (goes-before? truck jeep)
    
    13
               (let [debrief (bubble convoy)] (cons (cons truck (first debrief)) (next debrief)))
    
    14
               (cons (cons jeep (first (bubble (cons truck (next convoy))))) '(true))
    
    15
               )
    
    16
            )
    
    17
          )
    
    18
      )
    
    19
      
    
    20
      ;; bubble-sort: list of number -> list of number
    
    21
    (defn bubble-sort [l]
    
    6+
        [tanks false]
    
    7+
        (let [[truck & [jeep & _ :as convoy]] tanks]
    
    8+
          (if (<= truck jeep)
    
    9+
            (let [debrief (bubble convoy)] (cons (cons truck (first debrief)) (next debrief)))
    
    10+
            (cons (cons jeep (first (bubble (cons truck (next convoy))))) '(true))))))
    
    11+
    12+
    (defn sort [l]
    
    2222
      (let [bubbly (bubble l)]
    
    23
        (if (first (next bubbly))
    
    24
            (bubble-sort (first bubbly))
    
    25
            (first bubbly)
    
    26
            )))
    
    27
            
    
    28
      (prn (bubble-sort (list 5 4 3 2 1)))
    
    14+
        (if (second bubbly)
    
    15+
          (recur (first bubbly))
    
    16+
          (first bubbly))))
    

Recent Moves:

I use this whenever I have to add another directory to my PATH environment variable in my .bashrc. This way I don't have duplicates.

I wrote this years ago and it's hacky and kind of dumb... I'm sure it can be improved.

add_PATH() {
        oIFS=$IFS
        IFS=':'
        t=(${PATH})
        unset IFS
        t=("$1" ${t[@]%%"$1"})
        # output the new array
        IFS=':'
        echo -n "${t[*]}"
        unset t
        IFS=$oIFS
}

echo "Original Path: $PATH"
export PATH=$(add_PATH /usr/local/bin)
echo "New Path: $PATH"
Code
Diff
  • #!/bin/bash       
    
    foo="Hello World!"
    for (( i=0; i<${#foo}; i++ ))
    do
      echo -n "$(tput setaf $i)${foo:$i:1}$(tput sgr0)"
    done
  • 1
    #!/bin/bash          
    
    2
    cat <<< "Hello Bash!"
    
    1+
    #!/bin/bash       
    
    2+
    3+
    foo="Hello World!"
    
    4+
    for (( i=0; i<${#foo}; i++ ))
    
    5+
    do
    
    6+
      echo -n "$(tput setaf $i)${foo:$i:1}$(tput sgr0)"
    
    7+
    done
    

Recent Moves:

"Hello Python!" script.

print "Hello Python!"
Arrays
Blocks
Object-oriented Programming
Functions
Control Flow
Basic Language Features
Fundamentals

A refactor of jhoffner's code. I edited the Array#sum code, so take a look at the changes.

Code
Diff
  • class Array
      def sum(&block)
        block = Proc.new {|n| n} unless block_given?
        inject(0) {|result, item| result + block.call(item) rescue result}
      end
      
      def mean(&block)
        sum(&block).to_f / size
      end
    end
  • 11
    class Array
    
    22
      def sum(&block)
    
    3
        inject(0) do |result, item|
    
    4
          item = block_given? ? yield(item) : item
    
    5
          result + (item || 0)
    
    6
        end
    
    3+
        block = Proc.new {|n| n} unless block_given?
    
    4+
        inject(0) {|result, item| result + block.call(item) rescue result}
    
    77
      end
    
    88
      
    
    99
      def mean(&block)
    
    1010
        sum(&block).to_f / size
    
    1111
      end
    
    1212
    end
    
Mathematics
Algorithms
Numbers
Code
Diff
  • cats :: [Integer]
    cats = 1 : [ c_n * 2 * (2 * n + 1) `div` (2 + n) |
                 (c_n, n) <- zip cats [0..] ]
    
    main = putStrLn $ show $ take 10 cats
  • 11
    cats :: [Integer]
    
    2
    cats = 1 : do
    
    3
      (c_n, n) <- zip cats [0..]
    
    4
      return $ c_n * 2 * (2 * n + 1) `div` (2 + n)
    
    5
      
    
    2+
    cats = 1 : [ c_n * 2 * (2 * n + 1) `div` (2 + n) |
    
    3+
                 (c_n, n) <- zip cats [0..] ]
    
    4+
    66
    main = putStrLn $ show $ take 10 cats
    

Recent Moves: