Created
February 24, 2017 01:38
-
-
Save kozross/25a6d1b782042530edc15f3d0b464299 to your computer and use it in GitHub Desktop.
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
#= I have the following sauce code: =# | |
module TruthTable | |
import Base.size, Base.getindex, Base.linearindexing | |
export Table, random | |
immutable Table | |
p :: Int | |
unpack :: BitVector | |
end | |
function random(frac :: Int, props :: Int) :: Table | |
frac ∈ 1:1:7 || error("frac should be in [1,7]") | |
props ≥ 3 || error("Must have at least 3 propositions") | |
len = 2 ^ props | |
to_set = (frac * len) ÷ 8 | |
raw = falses(len) | |
while to_set > 0 | |
i = rand(1:len) | |
if !getindex(raw, i) | |
raw[i] = true | |
to_set = to_set - 1 | |
end | |
end | |
return Table(props, raw) | |
end | |
size(t :: Table) = (length(t.unpack), t.p + 1) | |
function getindex(t :: Table, r :: Int) | |
if 1 ≤ r ≤ length(t.unpack) | |
return t.unpack[r] | |
end | |
error("Row out of bounds: $r") | |
end | |
function getindex(t :: Table, r :: Int, c :: Int) | |
if c == (t.p + 1) | |
return t.unpack[r] | |
elseif 1 ≤ c ≤ t.p | |
return isodd((r - 1) >> (t.p - c)) | |
end | |
error("Indices out of bounds: $r, $c") | |
end | |
linearindexing(::Type{Table}) = LinearSlow() | |
end | |
#= However, when I try to do something like `TruthTable.random(2,3)[1,2]`, I get a BoundsError. | |
What did I not define correctly here? =# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment