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]
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:
Anonymous2013-03-21 17:54
>>5
trial and error combined with cargo cult programming is best.
biology agreed, dont bother him about it.
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
>>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:
OP2013-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?
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:
Anonymous2013-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:
Anonymous2013-03-23 0:05
OP tries to implement a scheme interpreter He can't even get proper tail recursion working
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.