Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 169 170 [171] 172 173 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 888151 times)

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2550 on: June 18, 2012, 06:03:11 pm »

I finally finished my personal C++ challenge. I have implemented function tables in a somewhat-nice way for C++, using a combination of C++ code, C preprocessor macros, a bash script, a Python script, and an often-overlooked binutils tool (ctags).

Now for the next (harder) step: member-function tables. And then, object tables.

AND THEN THE WORLD!

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2551 on: June 18, 2012, 06:54:23 pm »

Logged

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2552 on: June 18, 2012, 07:50:32 pm »

Spoiler (click to show/hide)

Why are you using nested classes? IME that's rarely the right way to solve a problem, and it can lead to confusing code...

<opinion type="personal">Inner classes should seldom be public, and almost never have public constructors.</opinion>
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2553 on: June 18, 2012, 07:55:56 pm »

Virex, I tried learning LISP a while back. It didn't work. But, I did get really good at matching up parentheses.

Anyway I'll probably be showing off my success here soon.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2554 on: June 18, 2012, 08:43:17 pm »

Virex, I tried learning LISP a while back. It didn't work. But, I did get really good at matching up parentheses.


Huh, I let my editor (usually emacs) do the parentheses matching. I couldn't do it by hand if my life depended on it :P
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2555 on: June 18, 2012, 10:15:11 pm »

I spent the first few years of my programming experience using only the Windows 95 default editor. The good news: I'm now officially awesome at finding any sort of bugs in arbitrarily obfuscated code.
Logged

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2556 on: June 19, 2012, 09:50:20 am »

I once tried to write a stack-based program to make sure all of my parenthesis matched, but it wouldn't compile. It turns out I forgot a parenthesis. :P
Logged

Xegeth

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2557 on: June 19, 2012, 03:42:27 pm »

Venting about c++ ahead! 'Cause I feel like it.

Two things I loathe about it:

1) No easy way to maintain local scope and eliminate redundancy. If you've a function that does the same identical block of code multiple times throughout, the normal way to eliminate redundancy is to turn that block of code into a function call. This means you lose local scope. The ways around this are 1) Pass your local variables as arguments (sloppy), 2) Turn your locals into globals (retch), or 3) use macros (potentially long compile times, ahoy!). I've started gravitating toward globals and I feel dirty.

An alternative way around the problem would be creating a new class and moving the block of code into that. You'd only have to pass the local variables or references to them into it once at the start of the function, and it's only slightly slower than using global variables.
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2558 on: June 19, 2012, 03:56:08 pm »

Hmm... c++ doesn't support code blocks and lambdas? Or do they just not inherit scope?

(It's clearly been too long)
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2559 on: June 19, 2012, 04:09:21 pm »

Hmm... c++ doesn't support code blocks and lambdas? Or do they just not inherit scope?

(It's clearly been too long)

Not 100% sure what you mean by code blocks.
C++11 does have lambdas, but that is brand spanking new.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2560 on: June 19, 2012, 04:16:42 pm »

Code block is just another term for a closure.

Spoiler: From Wikipedia (click to show/hide)

Have looked it up, apparently some versions of C++ support them.

It may not be exactly what he's describing here, of course. I'm not really thinking clearly and maybe that is all completely irrelevant.
Logged

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #2561 on: June 19, 2012, 04:31:22 pm »

Closures were gonna be in C++11 but they couldn't agree on a good usage and syntax for them so postponed it. Some compilers have special builds to to support them because experimental implementations were made to test the ideas and implementations.

Lambas are in C++11 and gcc and visual studio 2010 support them, you manually specify which variables get dragged by value or by reference, or if all in parent scopes are: http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B
« Last Edit: June 19, 2012, 04:32:54 pm by MorleyDev »
Logged

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2562 on: June 19, 2012, 06:35:57 pm »

Well lambas look interesting, if they work how I think they do.

What I'd love would be a local function that inherits local scope. IE,
Code: [Select]
void foo()
{
    int counter = 0;
    int counter2 = 0;
    void IncrementCounters()
    {
         counter++;
         counter2++;
    }
   

    //do random stuff
    IncrementCounters();
    //do other stuff
    IncrementCounters();
}

Not the most practical example but that's pretty much what I want. Most of the time when I have this problem, I'm doing very different things between the redundant code ("random stuff" and "other stuff" in the example would be quite different, or at least not something iterable), so a loop wouldn't work to eliminate it.
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2563 on: June 19, 2012, 06:51:17 pm »

Well lambas look interesting, if they work how I think they do.

What I'd love would be a local function that inherits local scope. IE,
Code: [Select]
void foo()
{
    int counter = 0;
    int counter2 = 0;
    void IncrementCounters()
    {
         counter++;
         counter2++;
    }
   

    //do random stuff
    IncrementCounters();
    //do other stuff
    IncrementCounters();
}

Not the most practical example but that's pretty much what I want. Most of the time when I have this problem, I'm doing very different things between the redundant code ("random stuff" and "other stuff" in the example would be quite different, or at least not something iterable), so a loop wouldn't work to eliminate it.

This should work:

Code: [Select]
void foo()
{
    int counter = 0;
    int counter2 = 0;
    auto IncrementCounters = [&]()
    {
         counter++;
         counter2++;
    }
   

    //do random stuff
    IncrementCounters();
    //do other stuff
    IncrementCounters();
}
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2564 on: June 19, 2012, 07:09:25 pm »

Actually you can have a closure without having an anonymous function, at least in some languages (lisp code because fuck parenthesis haters)
Code: [Select]
(let ((ID 0))
    (defun get-ID ()
        (1+ ID)))


The function get-ID increments the ID variable every time it is called and returns the result of incrementing it. The ID variable is external to get-ID and that's why it doesn't go out of scope after get-ID exits, instead it will persist until we unintern get-ID.  It is however local to the let-block in which get-ID was defined, so no other function can access it.


We can also give multiple functions access to ID:
Code: [Select]

(let ((ID 0))
    (defun get-ID ()
        (1+ ID))
    (defun reset-ID ()
        (setf ID 0))
    (defun set-ID (x)
        (setf ID x)))
Here, there are 3 functions that can access ID. get-ID, which returns it, reset-ID, which sets it to 0 and set-ID, which sets it to specific value.


On another note, for anyone wanting to learn how a computer works, or what this enigmatic thing called assembly language is, have a look at this book (warning, direct pdf link), it explains the basic of computers and programming using assembly language.
« Last Edit: June 19, 2012, 07:20:19 pm by Virex »
Logged
Pages: 1 ... 169 170 [171] 172 173 ... 796