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

Pages: 1-

Match Macro

Name: Anonymous 2013-07-30 10:00

Can you write in C/C++ a destructuring match macro, like following?

;; Usage:
;;   (match '(1 2 3 4 5 6) ((a b . c) (list c b a)) (otherwise 'fuck-off))

(defun match-hole (key hole hit miss)
  (if (consp hole)
      (if (and (eql (car hole) 'quote)
               (= (length hole) 2))
          `(if (equal ',hole ,key)
               ,hit
               ,miss)
          (let ((x (gensym))
                (hit (match-hole key (cdr hole) hit miss)))
            `(if (consp ,key)
                 (let ((,x (car ,key))
                       (,key (cdr ,key)))
                   ,(match-hole x (car hole) hit miss))
                 ,miss)))
      (if (and (symbolp hole) hole (not (eql hole t)))
          (if (string= (symbol-name hole) "_")
              hit
              `(let ((,hole ,key))
                 ,hit))
          `(if (equal ',hole ,key)
               ,hit
               ,miss))))

(defmacro match (keyform &body cases)
  (let ((key (gensym))
        (b (gensym)))
    `(let ((,key ,keyform))
       (block ,b
         (tagbody
           ,@(reduce (lambda (next case)
                       (let ((miss (gensym))
                             (hit `(return-from ,b (progn ,@(cdr case)))))
                         `(,(match-hole key (car case) hit `(go ,miss)) ,miss ,@next)))
                     (cons nil (nreverse cases))))))))

Name: Anonymous 2013-07-30 10:09

what is C/C++ >>1-kun?

Name: Anonymous 2013-07-30 10:10

You can try using templates, but it will be far from maintainable or readable, and it wont work for anything non-trivial like this.

Name: Anonymous 2013-07-30 10:15

The only ones that would defend C++ here is Cudder, FrozenVoid, the GC is shit guy (whom wouldn't use it anyway) and the occasional imagereddit, and FrozenVoid will probably take your challenge and fail with an ungodly spaghetti of #defines named in a Huffman coded manner starting from single characters.

Name: Anonymous 2013-07-30 10:17

Name: Anonymous 2013-07-30 11:06

Nope >>1 . I don't even have an idea what is a "destructuring match macro" is or what it does. All i see a walls of parens,(in them something compares lists? or functions? no idea since i have no knowledge of Lisp/Scheme).  If you could explain the function of your code with some examples, i could try.

Name: Anonymous 2013-07-30 11:14

>>6
It matches C/C++ zero terminated arrays of symbols.

Say matches a single-linked list against a set of patterns, extracting specified parts and invoking associated handler on match. Think regular expressions, but with linked lists and compiled to machine code.

Name: Anonymous 2013-07-30 11:59

>>7
a C #define can't do this, but a regular function can.
It would sequentially process a list, then
check if list node matches regex or some conditions, then
calls a handler(could be function pointer from array of functions) with parameters.
Is this right?

Name: Anonymous 2013-07-30 12:09

>>4
Since when does Javashit have defines?

As far as I know, Frozenvoid didn't use C or SEPPLES, but always Javashit.

Name: Anonymous 2013-07-30 12:24

>>9
JavaScript is fast enough for infinite compression?

Name: Anonymous 2013-07-30 12:35

>>10
JavaScript is faster than microcode and asm. Its the only choice if you care about performance.

Name: Anonymous 2013-07-30 12:35

>>11
Hi, FrozenAnus.

Name: Anonymous 2013-07-30 12:40

>>12
You're just jealous that you don't have a JavaScript-machine, which outperforms your x86 CPU by several orders of magnitude with native JavaScript instructions. You might think about how to write more efficient code, but your machine microcode is bloated and slow, rendering such effort fruitless.

Name: Anonymous 2013-07-30 12:45

>>13
Oh, but I have a Javascript machine right here!
*grabs dick*

Name: Anonymous 2013-07-30 13:04

FrozenNegroid

Name: Anonymous 2013-07-30 13:07

FrozenNegroloid

Name: Anonymous 2013-07-30 14:22

In the future all computers will be JavaScript-machines and x86 will be emulated. Mark my words.

Name: Anonymous 2013-07-30 14:25

>>17
You finally learned to capitalize, Javashit kike. Your troll posts are still as bad as always, and you've said that like a million times already, though.

Name: Anonymous 2013-07-30 15:09

AI research is only possible in JavaScript. Mentifex knows it.

Name: Anonymous 2013-07-30 15:38

Have you read JSTGP today?

Name: Anonymous 2013-07-30 16:56

>>9
No, he did write C code, he used an abomination of a header file containing every library he ever used, which, I believe, was called void.h.

Name: Anonymous 2013-08-05 2:42

>>11
Is this a troll or its somehow related to OpenCL RiverTrail outperforming x86 asm?

Name: Anonymous 2013-08-05 2:48

>>22
x86 is obsolete shit kept on life support. Once Intel integrates JS in the hardware using the full power and knowledge of all their engineers, it WILL out perform any type of assembly.

Name: Anonymous 2013-08-05 2:51

>>23
sadly, even JS would be better than x86...

Name: Anonymous 2013-08-05 3:17

>>24
>LE POWERFUL PARADIGMS LEEEEEEEEEELLLLLLL!!!!!  XXXXDDDDD
>LE FUNCTIONAL WEBAPP FACE!!!! E/G/IN /G/ROSKI!!!!!!!!

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