Skip to content

Commit

Permalink
Using Ulf Wiger's ct_expand to replace cross/2, giving a huge perform…
Browse files Browse the repository at this point in the history
…ance boost.

Thanks to Matthew Evans for the idea:
http://groups.google.com/group/erlang-programming/msg/8abc337adde2b0d1
  • Loading branch information
Andreas Pauley committed Mar 28, 2011
1 parent 00edacd commit 10cb1a1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
1 change: 1 addition & 0 deletions sudoku
Expand Up @@ -4,6 +4,7 @@
main(Args) ->
{ok, sudoku} = compile:file(sudoku, [native]),
{ok, unittests} = compile:file(unittests, [native]),
{ok, ct_expand} = compile:file(ct_expand, [native]),
run_solver(Args).

run_solver([]) ->
Expand Down
17 changes: 7 additions & 10 deletions sudoku.erl
@@ -1,29 +1,26 @@
-module(sudoku).
-import(lists, [member/2, filter/2, map/2, flatmap/2, sort/1, all/2, sum/1]).
-compile(export_all).
-compile({parse_transform, ct_expand}).

-define(digits, "123456789").
-define(rows, "abcdefghi").
-define(cols, ?digits).

cross(SeqA, SeqB) ->
%% Cross product of elements in SeqA and elements in SeqB.
[[X,Y] || X <- SeqA, Y <- SeqB].

squares() ->
%% Returns a list of 81 square names, including "a1" etc.
cross(?rows, ?cols).

ct_expand:term([[X,Y] || X <- ?rows, Y <- ?cols]).
col_squares() ->
%% All the square names for each column.
[cross(?rows, [C]) || C <- ?cols].
ct_expand:term([[[X,Y] || X <- ?rows, Y <- [C]] || C <- ?cols]).
row_squares() ->
%% All the square names for each row.
[cross([R], ?cols) || R <- ?rows].
ct_expand:term([[[X,Y] || X <- [R], Y <- ?cols] || R <- ?rows]).
box_squares() ->
%% All the square names for each box.
[cross(Rows, Cols) || Rows <- ["abc", "def", "ghi"],
Cols <- ["123", "456", "789"]].
ct_expand:term([[[X,Y] || X <- R, Y <- C] ||
R <- ["abc", "def", "ghi"],
C <- ["123", "456", "789"]]).

unitlist() ->
%% A list of all units (columns, rows, boxes) in a grid.
Expand Down
5 changes: 0 additions & 5 deletions unittests.erl
Expand Up @@ -12,7 +12,6 @@
%% Test the functionality using Joe Armstrong's Micro Lightweight Unit Testing:
%% http://armstrongonsoftware.blogspot.com/2009/01/micro-lightweight-unit-testing.html
test() ->
ok = test_cross(),
ok = test_squares(),
ok = test_unitlist(),
ok = test_units(),
Expand All @@ -34,10 +33,6 @@ test() ->
ok = test_stats(),
ok.

test_cross() ->
["a1","a2","b1","b2"] = cross("ab", "12"),
ok.

test_squares() ->
81 = length(squares()),
ok.
Expand Down

0 comments on commit 10cb1a1

Please sign in to comment.