Last active
October 2, 2017 04:54
-
-
Save gregberns/a4220635fbab0715ce3b417084fbb4ed to your computer and use it in GitHub Desktop.
Diamond Kata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Diamond = { | |
let inc_char c => char_of_int (int_of_char c + 1); | |
let rec range_rec l a b => a == b ? l @ [a] : range_rec (l @ [a]) (inc_char a) b; | |
let range a b => range_rec [] a b; | |
let rec replicate a s i => i < 1 ? a : replicate (a ^ s) s (i - 1); | |
let addSpaces i => replicate "" " " i; | |
let line p j c => { | |
let cs = Char.escaped c; | |
addSpaces p ^ cs ^ (j < 1 ? "" : addSpaces j ^ cs) | |
}; | |
let createLine t i c => { | |
let p = t - i - 1; | |
let m = i * 2 - 1; | |
line p m c | |
}; | |
let diamond c => { | |
let r = range 'A' (Char.uppercase c); | |
let f = createLine (List.length r); | |
let top = List.mapi f r; | |
let bottom = List.tl (List.rev top); | |
top @ bottom | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Jest; | |
open Expect; | |
open! Expect.Operators; | |
open Diamond; | |
describe | |
"Diamond" | |
( | |
fun () => { | |
test "inc_char" (fun () => expect (Diamond.inc_char 'A') === 'B'); | |
test "range same" (fun () => expect (Diamond.range 'A' 'A') |> toEqual []); | |
test "range" (fun () => expect (Diamond.range 'A' 'B') |> toEqual ['A', 'B']); | |
test "range" (fun () => expect (Diamond.range 'A' 'D') |> toEqual ['A', 'B', 'C', 'D']); | |
test "replicate 0" (fun () => expect (Diamond.replicate "" "1" 0) |> toEqual ""); | |
test "replicate 1" (fun () => expect (Diamond.replicate "" "1" 1) |> toEqual "1"); | |
test "replicate 111" (fun () => expect (Diamond.replicate "" "1" 3) |> toEqual "111"); | |
test "addSpaces 0" (fun () => expect (Diamond.addSpaces 0) |> toEqual ""); | |
test "addSpaces 1" (fun () => expect (Diamond.addSpaces 1) |> toEqual " "); | |
test "line A" (fun () => expect (Diamond.line 0 0 'A') |> toEqual "A"); | |
test "line B" (fun () => expect (Diamond.line 0 1 'B') |> toEqual "B B"); | |
test "line B" (fun () => expect (Diamond.line 1 1 'B') |> toEqual " B B"); | |
test "line C" (fun () => expect (Diamond.line 0 3 'C') |> toEqual "C C"); | |
test "createLine A" (fun () => expect (Diamond.createLine 1 0 'A') |> toEqual "A"); | |
test "createLine B" (fun () => expect (Diamond.createLine 2 1 'B') |> toEqual "B B"); | |
test "createLine B" (fun () => expect (Diamond.createLine 3 1 'B') |> toEqual " B B"); | |
test "createLine C" (fun () => expect (Diamond.createLine 3 2 'C') |> toEqual "C C"); | |
test "createLine C" (fun () => expect (Diamond.createLine 4 2 'C') |> toEqual " C C"); | |
test "diamond A" (fun () => expect (Diamond.diamond 'A') |> toEqual ["A"]); | |
test "diamond B" (fun () => expect (Diamond.diamond 'B') |> toEqual [" A", "B B", " A"]); | |
test | |
"diamond C" | |
( | |
fun () => expect (Diamond.diamond 'C') |> toEqual [" A", " B B", "C C", " B B", " A"] | |
); | |
test | |
"diamond D" | |
( | |
fun () => | |
expect (Diamond.diamond 'D') |> | |
toEqual [" A", " B B", " C C", "D D", " C C", " B B", " A"] | |
) | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment