Permalink
Browse files

Using Ulf Wiger's ct_expand to replace cross/2, giving a huge perform…

…ance boost.

Thanks to Matthew Evans for the idea:
http://groups.google.com/group/erlang-programming/msg/8abc337adde2b0d1
  • Loading branch information...
Andreas Pauley
Andreas Pauley committed Mar 28, 2011
1 parent 00edacd commit 10cb1a106d6a1681155b64b158a21bfcf9b0488e
Showing with 8 additions and 15 deletions.
  1. +1 −0 sudoku
  2. +7 −10 sudoku.erl
  3. +0 −5 unittests.erl
View
1 sudoku
@@ -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([]) ->
View
@@ -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.
View
@@ -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(),
@@ -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.

0 comments on commit 10cb1a1

Please sign in to comment.