Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
varnie committed Feb 26, 2025
1 parent 4ab1ec8 commit f87ab31
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
7 changes: 4 additions & 3 deletions lib/challenge46.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ defmodule Challenge46 do
A logical expression in two variables can then be written as in the following example: and(or(A,B),nand(A,B)).
Now, write a predicate table/3 which prints the truth table of a given logical expression in two variables.
"""
def table(a, b, expr_fn) do
result = expr_fn.(a, b)
"#{a} #{b} #{result}"
def table(input_fn) do
for a <- [true, false],
b <- [true, false],
do: "#{a} #{b} #{input_fn.(a, b)}"
end

def tbl_impl(a, b) do
Expand Down
18 changes: 4 additions & 14 deletions test/challenge46_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@ defmodule Challenge46Test do
use ExUnit.Case

test "table" do
assert Challenge46.table(false, true, &Challenge46.tbl_and(&1, Challenge46.tbl_or(&1, !&2))) ==
"false true false"
assert Challenge46.table(fn a, b -> Challenge46.tbl_and(a, Challenge46.tbl_or(a, b)) end) ==
["true true true", "true false true", "false true false", "false false false"]

assert Challenge46.table(true, true, &Challenge46.tbl_and(&1, &2)) == "true true true"
assert Challenge46.table(true, true, &Challenge46.tbl_and/2) == "true true true"

assert Challenge46.table(false, true, &Challenge46.tbl_and(Challenge46.tbl_or(&1, &2), &1)) ==
"false true false"

assert Challenge46.table(true, false, &Challenge46.tbl_and(Challenge46.tbl_or(&1, &2), &1)) ==
"true false true"

assert Challenge46.table(true, false, fn a, b ->
Challenge46.tbl_and(Challenge46.tbl_or(a, b), a)
end) == "true false true"
assert Challenge46.table(fn a, b -> Challenge46.tbl_or(a, b) end) ==
["true true true", "true false true", "false true true", "false false false"]
end
end

0 comments on commit f87ab31

Please sign in to comment.