Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

How can I improve my truth table generator?

Name: Anonymous 2012-01-04 21:50

class TrueClass
        def implies(x) x end
        def nand(x) not x end
        def xnor(x) x end
        def xor(x) not x end
        def nor(x) false end
        def not(x) !x end
end
class FalseClass
        def implies(x) true end
        def nand(x) x end
        def xnor(x) not x end
        def xor(x) x end
        def nor(x) not x end
        def not(x) x end
end
module Logic
        class TruthTableRow
                attr_accessor :cell
                def initialize(p, q, message)
                        @cell = [p, q, p.send(message, q)]
                end
        end
        class TruthTable
                attr_accessor :rows, :message
                def initialize(message)
                        @rows = [TruthTableRow.new(true, true, message),
                                TruthTableRow.new(true, false, message),
                                TruthTableRow.new(false, true, message),
                                TruthTableRow.new(false, false, message)]
                        @message = message
                end
                def printTable
                        rows.each do |a|
                                print "#{a.cell[0]} #{lambda{if a.cell[0] then " " else "" end}.call}#{message.to_s} #{a.cell[1]} #{lambda{if a.cell[1] then " " else "" end}.call}: #{a.cell[2]}\n"
                        end
                end
        end
        KeywordList = ["and", "or", "nand", "nor" "xor", "xnor", "implies", "not"]
        def Logic.mainProgram(x)
                if KeywordList.include?(x) then
                        print "\n\tP #{x.upcase} Q\n"
                        TruthTable.new(x.to_sym).printTable
                else
                        print "#{x.capitalize} is not a logic gate\n"
                end
        end
end

if ARGV.length >= 1 then
        Logic::mainProgram(ARGV[0].to_s.downcase)
else
        print "Wrong number of arguments\n"
end


Made this lovely piece of OOP in a functional programming class... which was more of a logic class because we haven't seen a single line of code yet this quarter.

Name: Anonymous 2012-01-04 22:15

Your mom

Name: Anonymous 2012-01-04 22:40

Rewrite in a non- gay language

Name: Anonymous 2012-01-04 22:59

>>3

But that would inevitably mean less use of OOP, unless I used smalltalk.

I do not use smalltalk.

Name: Anonymous 2012-01-04 23:00

>>4
Use Haskell.

Name: Anonymous 2012-01-04 23:25

>>5

But you said to rewrite it in a non-gay language.

Name: Anonymous 2012-01-04 23:28

>>6
No I said that and I'm not 5san

Name: Anonymous 2012-01-04 23:49

>>6

Haskell is a bisexual language.

Name: Anonymous 2012-01-05 0:01

lambda{if a.cell[0] then " " else "" end}.call
WTF learn to conditional expression
also indent more plox

Name: Anonymous 2012-01-05 0:08

Just use Lisp and complete your homosexuality.

Name: Anonymous 2012-01-05 0:40

Peck my tubs

Name: Anonymous 2012-01-05 0:54

def truthTable
  bools = [false,true]
  puts "A\tB\tY"
  for a in bools
    for b in bools
      puts "#{a}\t#{b}\t#{yield a, b}"
    end
  end
end

# use like this
truthTable {|a, b| a or b}

Name: Anonymous 2012-01-05 1:03

By writing it in APL.
(⍉2 2⊤⍳4),⍪(4/2)⊤(1 7 14 8 6 9 11 3)[0⌷⍒+/(7⍴⍞,7⍴' ')⍷8 7⍴'and    or     nand   nor    xor    xnor   impliesnot    ']

Name: Anonymous 2012-01-05 1:50

>>12

Wow, that's... ingenious...

Nice use of blocks!

Name: Anonymous 2012-01-05 3:18


def truthTable2(&b)
  n = b.arity

  # header
  c = 'A'
  n.times do
    print c + "\t"
    c = c.next
  end
  puts 'Y'

  #body
  0.upto(2**n-1) do |i|
    line = i.to_s(2).ljust(n,'0').split(//).map {|c| c == '1' ? true : false}
    puts "#{line.map(&:to_s).join("\t")}\t#{yield(*line)}"
  end
end

#usage
truthTable2 {|a,b,c,d| a or b or c or d}
truthTable2 {|a| not a}

Name: Anonymous 2012-01-05 3:32

oops, should be rjust

[code]
def truthTable(&b)
  n = b.arity

  # header
  c = 'A'
  n.times do
    print c + "\t"
    c = c.next
  end
  puts 'Y'

  # body
  0.upto(2**n-1) do |i|
    line = i.to_s(2).rjust(n,'0').split(//).map {|c| c == '1' ? true : false}
    puts "#{line.map(&:to_s).join("\t")}\t#{yield(*line)}"
  end
end

# usage
truthTable {|a,b,c,d| a or b or c or d}
truthTable {|a| not a}
[code]

Name: Anonymous 2012-01-05 3:33



def truthTable(&b)
  n = b.arity

  # header
  c = 'A'
  n.times do
    print c + "\t"
    c = c.next
  end
  puts 'Y'

  # body
  0.upto(2**n-1) do |i|
    line = i.to_s(2).rjust(n,'0').split(//).map {|c| c == '1' ? true : false}
    puts "#{line.map(&:to_s).join("\t")}\t#{yield(*line)}"
  end
end

# usage
truthTable {|a,b,c,d| a or b or c or d}
truthTable {|a| not a}

Don't change these.
Name: Email:
Entire Thread Thread List