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

Pages: 1-4041-

I want to code in Scheme

Name: Anonymous 2013-03-21 17:48

But I don't really know how to write idiomatic code. Is anyone here willing to rewrite some of my python code, so I can see how it's done?

Also, what is the code-tag on this board?

Name: Anonymous 2013-03-21 17:50

But I don't really know how to write idiomatic code. Is anyone here willing to rewrite some of my python code, so I can see how it's done?
Some of us will ignore you, some of us will mock you, and some of us will help you.

Also, what is the code-tag on this board?
[code][/code]

Name: Anonymous 2013-03-21 17:51

Why don't you... try?

Name: Anonymous 2013-03-21 17:52

this morning I shat a really good python

Name: 3 2013-03-21 17:52

As in, just fucking do it. Don't pussy-foot around. Write a program. If it's not idiomatic, who gives a shit? It's not going into production. Keep writing until it is comfortable-looking.

Name: Anonymous 2013-03-21 17:54

>>5
trial and error combined with cargo cult programming is best.
biology agreed, dont bother him about it.

Name: Anonymous 2013-03-21 18:06

>>6
The fuck are you on about?

Name: Anonymous 2013-03-21 18:08

>>7
this morning I shat a really good python

Name: Anonymous 2013-03-21 18:12

>>5
pussy-foot
Keep your oppressive sexist terminology off of this board, cave-dweller.

Name: Anonymous 2013-03-21 18:13

OP here. Sorry for the wait. It took a while to find this code and remove the unimportant parts. It's java related.

Partial translations are appreciated too.


from functools import partial as inf

# ...

nbit = {
    jint: "i",
    jlong: "l",
    jfloat: "f",
    jdouble: "d",
    jref: "a",
    jbool: "b",
    jchar: "c",
    jshort: "s"
}

def jvmop(func, name=None, sig=tuple(), **kw):
    if not hasattr(func, "sig"): func.sig = sig
    func.__name__ = name or func.__name__.replace("_", "")
    func.__dict__.update(kw)
    return func

class ConstantIndexOperand(Operand):
    __slots__ = ('index', )
    def __init__(self, index): self.index = index
    def __repr__(self): return '<ConstantIndexOperand(index=%r)>' % self.index


# ...

class JVMReader:

    def disasm(self):
        op = self.readbyte()
        widemode = op == 0xC4
        if widemode:
            op = self.readbyte()

        i = self.instructions[op]

        if widemode:
            sig = [(b'>H', o[1]) if o[0] == b'>B' else o for i in i.sig]
        else:
            sig = i.sig

        args = [t(*self.read(fmt)) for fmt, t in sig]

        return i, args

    # ...

    @inf(jvmop, sig=[(b'>H', ConstantIndexOperand)])
    def anewarray(self, type):
        count = self.stack.pop()
        self.stack.append(ty.Array(type))

    def binop_gen(type, op):
        binop_impl = lambda self: self.stack.append(ssa.BinOp(op, *self.pop(2))) # pop two elements
        return jvmop(binop_impl, name=nbit[type]+op)

    def cast_gen(source, target):
        cast_impl = lambda self: self.stack.append(ssa.Cast(target, source, self.stack.pop()))
        return jvmop(cast_opc, name="{}2{}".format(nbit[source], nbit[target]))

    # ...
   
    instructions = {
        #...
        0xBD: anewarray,
        #...
    }

    jtypes = (jint, jlong, jfloat, jdouble, jref)
    jnumtypes = jtypes[:4]

    numops = ("add", "sub", "mul", "div", "rem", "neg")

    # ...

    for i, (source, target) in enumerate(itertools.permutations(jnumtypes, 2)):
        instructions[0x85+i] = cast_gen(source, target)

    for i, (op, jtype) in enumerate(itertools.product(numops, jnumtypes)):
        instructions[0x60+i] = binop_gen(jtype, op)


to be continued…

Name: Anonymous 2013-03-21 18:13

>>9
what are you gonna do, take a pic and post it on your twatter, then get canned for it?

Name: Anonymous 2013-03-21 18:15

>>9
Keep your oppressive pseudofeminist dialectic off of this board, corner rounder.

Name: Anonymous 2013-03-21 18:17

>>9
Shalom. hyIHBT

Name: Anonymous 2013-03-21 18:21

>>10
I really wanted to help you. I expected snippets of 5 lines of code. I am not reading 100 lines of your Python, sorry.

Name: OP 2013-03-21 18:24

Here are a few things I'm interested in specifically:

How do I attach metadata to a function that can be read without calling the function. (but don't prevent the function to be called in the usual way.)

By using for i, (op, jtype) in enumerate(itertools.product(numops, jnumtypes)): I avoid nesting two loops and adding a counter-variable. What do lisp-people use here?

For pretty-printing I make sure that the opcodes have a proper name, and that certain ints are printing together with the role they have (like ConstantIndexOperand). Is there a way to define how certain objects are serialized/pretty-printed in scheme?

Name: Office Assistant Paperclip 2013-03-21 18:29

>>15

                        _.-;:q=._
                      .' j=""^k;:\.
                     ; .F       ";`Y
                    ,;.J_        ;'j
                  ,-;"^7F       : .F           _________________
                 ,-'-_<.        ;gj. _.,---""''               .'
                ;  _,._`\.     : `T"5,                       ;
                : `?8w7 `J  ,-'" -^q. `                     ; 
                 \;._ _,=' ;   n58L Y.                     .'
                   F;";  .' k_ `^'  j'                     ; 
                   J;:: ;     "y:-='                      ;  
                    L;;==      |:;   jT\                  ;
                    L;:;J      J:L  7:;'       _         ;
                    I;|:.L     |:k J:.' ,  '       .     ;
                    |;J:.|     ;.I F.:      .           :
                   ;J;:L::     |.| |.J  , '   `    ;    ;
                 .' J:`J.`.    :.J |. L .    ;         ;
                ;    L :k:`._ ,',j J; |  ` ,        ; ;
              .'     I :`=.:."_".'  L J             `.'
            .'       |.:  `"-=-'    |.J              ;
        _.-'         `: :           ;:;           _ ;
    _.-'"             J: :         /.;'       ;    ;
  ='_                  k;.\.    _.;:Y'     ,     .'
     `"---..__          `Y;."-=';:='     ,      .'
              `""--..__   `"==="'    -        .'
                       ``""---...__    itz .-'
                                   ``""---'


It looks like you're using Scheme to do real work.

Would you like help?

* Use Common Lisp.

* Seriously, use Common Lisp.

* Try another Lisp dialect.

* Use Common Lisp.

Name: Anonymous 2013-03-21 18:31

>>16
How long did it take to make that?
Also lisp a big smelly shit

Name: OP 2013-03-21 18:32

>>16
Yeah, whatever. CL is fine too. How could I know which is better?

Name: Anonymous 2013-03-21 18:36

>>15,18
JUST TRY IT
LOOK IT UP OR SOMETHING
NOBODY IS GOING TO HOLD YOUR HAND
IHBT

Name: Anonymous 2013-03-21 18:36

>>16
Fuck you I laughed so hard that my cat woke up and frowned at me.

Name: Anonymous 2013-03-21 18:37

>>17
That's copypasta, I've already seen it.

Name: Anonymous 2013-03-21 18:55

>>16
VIP quality

Name: Anonymous 2013-03-21 20:04

>>10
Jasper, James, JINT!

Name: Anonymous 2013-03-22 0:13

>>18

Some people seem to think Clojure is good for actual work, despite it being bundled with all your favorite security holes from the java run time.

Some people think SBCL is good, despite it giving 50 MB compiled binaries.

Some people think Clisp is good a plot of the jews.
http://www.clisp.org/impnotes/faq.html#faq-menorah-why

use pico lisp

Name: Anonymous 2013-03-22 1:21

>>24
Jasper, James, JLONG!

Name: Anonymous 2013-03-22 1:34

>>15
Read sicp

Name: Anonymous 2013-03-22 2:46

no libraries, but at least you can reinvent the wheels with style.


(define (product . args)
  (cond ((null? args) '()) ;; invalid call, really.
        ((null? (cdr args)) (map list (car args))) ;; make singletons
        (else (let* ((rest-products (apply product (cdr args)))
                     (front-list (car args))
                     (grouped-products (map (lambda (front-elem)
                                              (map (lambda (rest-product)
                                                     (cons front-elem rest-product))
                                                   rest-products))
                                            front-list)))
                (apply append grouped-products)))))


It's not very fast though. Use Haskell instead.

Name: Anonymous 2013-03-22 2:46

>>27
Haskell
Surely you're joking Mr Feynman!

Name: Anonymous 2013-03-22 3:08

>>28
WYPGP!

Name: Anonymous 2013-03-22 4:14

>>29
What your privileges gay programmer? o.0

Name: Anonymous 2013-03-22 4:27

>>30
WATCH YOUR PRIVACY GNU PROGRAMMER!

Name: Anonymous 2013-03-22 6:40

>>27
>>1 is a python programmer, speed isn't an issue

Name: Anonymous 2013-03-22 15:49

>>27
``In Racket'':

(define (cartesian-product . lists)
  (if (empty? lists)
      (list '())
      (append-map (lambda (element)
                    (map (curry cons element)
                         (apply cartesian-product (rest lists))))
                  (first lists))))
(define (cartesian-product . lists)
  (if (empty? lists)
      (list '())
      (append-map (lambda (element)
                    (map (curry cons element)
                         (apply cartesian-product (rest lists))))
                  (first lists))))

(define (map-indexed f list)
  (map f (range (length list)) list))

(map-indexed cons (cartesian-product numops jnumtypes))


It would have been a bit shorter using list comprehensions (racket's library syntax gives you the cartesian product for free), but I guessed that you would have preferred it this way, as it uses higher order functions directly.

The logic is O(n2) complexity on both this and your Python version, though. You are building a cartesian product after all.

Clojure has more list manipulation functions too, by the way.

Name: Anonymous 2013-03-22 16:26

>>1
how to write idiomatic code.
1. use closures instead of objects.
2. use macros to model target domain.
3. avoid plain assoc lists, because they impede encapsulation and referential transparency.

So if your goal is to write a C/C++ compiler, then keep parsed info inside closures and use macros to implement assembler.

Name: Anonymous 2013-03-23 0:05

OP tries to implement a scheme interpreter
He can't even get proper tail recursion working

HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA

never change /prog/

Name: Anonymous 2013-03-23 0:55

>>33
Aw, shit, just noticed that I pasted the function twice. Fiddlesticks.

Name: Anonymous 2013-03-23 1:14

>>36
it doesn't matter. chill

Name: Anonymous 2013-03-26 12:51

The gentoomen library has some nice resources on Scheme, and rosetta code has nice code examples. The only bad thing about the language is the lack of libraries and the diffrences between implementations. Doesn't matter though, no one would be so crazy to use Scheme to write robust enterprise-quality software.

Name: Anonymous 2013-03-26 12:59

>>38
We've read our SICP, HtDP and The Little/Seasoned Schemer. Now please go the fuck back to /g/ and take >>1 with you.

Name: Anonymous 2013-03-26 14:12

prod=foldr(\x->((:)<$>x<*>))[[]]

*Main> prod [[1,2,3],[4,5],[6,7,8]]
[[1,4,6],[1,4,7],[1,4,8],[1,5,6],[1,5,7],[1,5,8],[2,4,6],[2,4,7],[2,4,8],[2,5,6],[2,5,7],[2,5,8],[3,4,6],[3,4,7],[3,4,8],[3,5,6],[3,5,7],[3,5,8]]

Name: Anonymous 2013-03-26 15:17

REPENT FOR YOUR SINS AND LEARN ENTERPRISE LISP!

Name: a.k.a. scala 2013-03-26 15:17

a.k.a. scala

Name: Anonymous 2013-03-26 15:30

>>40
(\x->((:)<$>x<*>))[[]]
valid haskell code.

Name: Anonymous 2013-03-26 15:36

>>24
Just use SCHEME, you silly faggit ;)

Name: Anonymous 2013-03-26 18:19

>>43
I improved it a little, just for you.

prod=foldr((<*>).((:)<$>))[[]]

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