How to map over and call a list of ringed reporters? (Or solve a better way.)

image

Hi! I want to collect the truth table output from “and gate” called with all four combinations of the two boolean inputs, i.e., [false, false, false, true]. Here is what I tried. Thanks in advance.

Here’s a Python program showing what I’m after.

def truth_table(fn):
    bools = False, True
    for a in bools:
        for b in bools:
            yield a, b, fn(a, b)

def and_gate(in1: bool, in2: bool) -> bool:
    return in1 and in2

print(list(truth_table(and_gate)))
[(False, False, False), (False, True, False), (True, False, False), (True, True, True)]

So, first of all, here's how to say what you were trying to say, in Snap!:


(I'm too lazy to fill in the rings...)

But being as I am too lazy to enumerate the four cases, I'd load the APL library and do this:

and then use that to make the result you want:


I confess that FLATTEN OF LIST ... is a little complicated; it's because you wanted the two inputs to AND in the result table as well as their AND. I'll have to think about how we can make that more friendly.

I love this. Seeing crossproduct gave me a huge smile. Thanks, Brian. And thanks, @dardoro.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.