Skip to content

Commit

Permalink
Simplify code using lists:usort, thanks to a tip from Ahmed Omar
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Pauley committed Mar 29, 2011
1 parent 9125147 commit f092e55
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion sudoku.erl
Expand Up @@ -196,7 +196,7 @@ is_solved(Puzzle) ->
is_unit_solved(Puzzle, Unit) ->
UnitValues = flatmap(fun(S) -> values(Puzzle, S) end, Unit),
(length(UnitValues) == 9)
and (gb_sets:from_list(UnitValues) == gb_sets:from_list(?digits)).
and (lists:usort(UnitValues) == ?digits).

to_string(Puzzle) ->
{ValuesDict, _} = Puzzle,
Expand Down

4 comments on commit f092e55

@kostis
Copy link

@kostis kostis commented on f092e55 Mar 29, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably also want to use =:= instead of == throughout.

@apauley
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think read somewhere that =:= is more equal than == :-)
In comparisons like these, would you use =:= because it is more correct/better style, or because you would expect better performance?

@kostis
Copy link

@kostis kostis commented on f092e55 Mar 29, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both, actually.
The use of =:= is more correct because the test will not accidentally succeed if a float somehow ends up there,
and you can expect slightly better performance because the compiler does not have to (generate code that) checks for floats.

@apauley
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool:
faa8a31

Please sign in to comment.