MasterMind Kata in Ioke

Ioke is a new language that I’ve been wanting to learn for a few weeks. I like the vision statement for this language: “Make it as expressive as possible. Period“. Ola Bini, its author, doesn’t want to focus on performances early because it would break expressiveness. Take a look a primitives types versus plain Objects in Java to understand why.

However I had no idea of what interesting code to use it on. Difficult to learn a programming language when you don’t know what to program.

So, as I try to go to the Coding Dojo every Monday, I felt that Ioke could be used to prepare the MasterMind Kata (link in French).

Here is the code, reworked after the Dojo to make it clearer:

countCorrectColorAndPosition = method(secret, guess,
  secret zip(guess) count(equals?)

countCorrectColorWrongPosition = method(secret, guess,
  pairsWithWrongPosition = secret zip(guess) filter(different?)

  secret distinct map(color,
    countWrongPositionForOneColor(pairsWithWrongPosition, color)) sum

countWrongPositionForOneColor = method(pairsWithWrongPosition, color,
  [pairsWithWrongPosition count(first == color),
    pairsWithWrongPosition count(second == color)] min
Mixins Enumerable sum = method(self inject(+))
Mixins Enumerable distinct = method(set(*self))
Mixins Enumerable equals? = method(self inject(==))
Mixins Enumerable different? = method(self inject(!=))

  it("should recognize when no peg is correct position or color",
        ["N","N","N","N"]) should == 0
  it("should recognize when one peg is correct position and color",
        ["B","N","N","N"]) should == 1
  it("should recognize when all pegs are correct positions and colors",
        ["B","B","B","B"]) should == 4

  it("should recognize when no peg is correct color",
        ["N","N","N","N"]) should == 0
  it("should recognize when a peg is correct color and wrong position",
        ["N","V","N","N"]) should == 1
  it("should recognize when there are more pegs of a given color in the guess than in the secret, and ignore these pegs",
        ["N","V","V","N"]) should == 1
  it("should ignore pegs with correct position and color",
        ["V","V","N","N"]) should == 1
  it("should recognize when pegs are correct colors for multiple colors",
        ["R","V","N","N"]) should == 2

(For a better reading).

First part is the code itself.
Second part, I had to write a few Mixins that could be included in standard Ioke library (remember it’s a very young language)
Last part are the unit tests, BDD style. I’m more used to TDD so forgive me if it’s not pure BDD.

Hope this will make you either want to practice Ioke or come to the Dojo.

3 thoughts on “MasterMind Kata in Ioke”

  1. “Hope this will make you either want to practice Ioke or come to the Dojo.”

    => Or come to the dojo to show a Ioke Kata/Randori 😉

  2. Interesting solution. It’s really nice to see Ioke work so cleanly in such a functional style 🙂 I’m looking forward to reading further coding dojo solutions in Ioke….

Comments are closed.