Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Logic gates in Ruby

NAND = lambda { |i, j| !(i && j) }
NOT = lambda { |i| NAND[i, i] }
AND = lambda { |i, j| NOT[NAND[i, j]] }
OR = lambda { |i, j| NAND[NAND[i, i], NAND[j, j]] }
NOR = lambda { |i, j| NOT[OR[i, j]] }
XOR = lambda { |i, j| NAND[NAND[i, NAND[i, j]], NAND[j, NAND[i, j]]] }
XNOR = lambda { |i, j| NOT[XOR[i, j]] }

XOR[true, true] => false
XOR[true, false] => true
XOR[false, true] => true
XOR[false, false] => false

rpatch XOR diff/patch script

REBOL []


do-by-type: func [
    value
    actions [block!]
    /default
        def-action [block!]
    /local action
][
    either action: select actions type?/word value [
        ;print mold action
        do bind/copy action 'value
    ][
        if default [do bind/copy def-action 'value]
    ]
]

to-binary-ex: func [value] [
    do-by-type value [
        binary! [value]
        string! [to-binary value]
        file!   [read/binary value]
    ]
]

xor-diff: func [
    a [binary! string! file!]
    b [binary! string! file!]
    /local bin-a bin-b
][
    bin-a: to-binary-ex a
    bin-b: to-binary-ex b
    bin-a xor bin-b
]

make-patch: func [
    a [binary! string! file!]
    b [binary! string! file!]
][
    compress xor-diff a b
]

apply-patch: func [
    data  [binary! string! file!]
    patch [binary! string!]
    /local result
][
    result: xor-diff data decompress patch
    ; If they gave us a string as the source, return a string.
    ;?? If they give us a file, should we write back out to it?
    either string? data [to-string result][result]
]


a: "Now is the time for all good men to come to the aid of their country..."
b: {Now is the time for all good men to come to the aid of their country...
whether 'tis nolber to suffer the slings and arrows...}
;d1: xor-diff a b
p1: make-patch a b
bp: apply-patch a p1



change-dir %./test-files

files: sort read %.
print mold files
print [files/1 files/2]
fp-1-2: make-patch files/1 files/2
fp-2-3: make-patch files/2 files/3
fp-3-4: make-patch files/3 files/4
fp-a-b: make-patch files/5 files/6

pp-a-b: apply-patch files/5 fp-a-b

change-dir %..


halt


« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS