Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
Completely not a question, but holy Christ, Erlang's list comprehensions make Python's list comprehensions look like for loops and I had to post somewhere. :)

Adbot
ADBOT LOVES YOU

TasteMyHouse
Dec 21, 2006

Misogynist posted:

Completely not a question, but holy Christ, Erlang's list comprehensions make Python's list comprehensions look like for loops and I had to post somewhere. :)

I think the word "for" in the python list comps makes them look like for loops :) :wooper:

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Is there already a thread on Amazon web services? I'm beginning to play around with some of their stuff, and it sure would be nice to have one.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
I'm suddenly really interested in functional programming languages and am already learning Erlang, OCaml and Haskell. What's Scala good at?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Misogynist posted:

What's Scala good at?
Being less lovely than Java when you have to run on the JVM for whatever reason.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

I'm writing a program that converts an image into a JPEG. It turns it from RGB to YUV, then does a 4:2:0 chroma subsample, and then does an 8x8 DCT.

The problem is, I have no idea if the images it's outputting are correct.

edit: Nevermind, figured it out, forgot to increment a variable!

Acer Pilot fucked around with this message at 12:09 on Jun 28, 2011

qntm
Jun 17, 2009
Other than performance, are there any good reasons not to use exceptions for flow control?

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Exceptions have roughly the readability characteristics of a goto.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Taken too far it's really easy to end up with incomprehensible spaghetti code that might as well be using goto for everything.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

In Python, it's pretty common to use exceptions as flow control. Just pick what is the most readable and practical...

qntm
Jun 17, 2009
The only reason goto is ever a problem is when it causes execution to jump backwards through a program. Exceptions are basically glorified breaks or returns, which I'm certain everybody loves.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

qntm posted:

Other than performance, are there any good reasons not to use exceptions for flow control?

Keep in mind what a reader of your code will likely expect. If an exception in the given language or domain typically means "terrible things happening", using them for flow control makes it harder for the reader to understand your code. If an exception is raised whenever an iterator completes, however, your reader may be used to seeing exceptions used this way.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


pokeyman posted:

Keep in mind what a reader of your code will likely expect. If an exception in the given language or domain typically means "terrible things happening", using them for flow control makes it harder for the reader to understand your code. If an exception is raised whenever an iterator completes, however, your reader may be used to seeing exceptions used this way.

Exactly. It's not syntactically or semantically wrong to use exceptions for control flow, but it's pragmatically very bad.

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?
All the C# guidance I've seen is along the lines of "Never use exceptions for flow control. No not even then. We mean it." and in a C# context I can't see any reason why you'd want to. Depends on the language I guess? But in most cases I've seen exceptions ruin readability when used in that way.

baquerd
Jul 2, 2007

by FactsAreUseless
code:
    private class TrueResult extends Exception { };
    private class FalseResult extends Exception { };

    public void isOdd(Integer num) throws Exception {
        try {
            isNull(num);
        } catch (TrueResult t) {
            throw new IllegalArgumentException();
        } catch (FalseResult f) {
            if (num % 2 == 0) {
                throw new FalseResult();
            } else {
                throw new TrueResult();
            }
        }
    }
    
    public void isNull(Integer num) throws Exception {
        if (num == null) {
            throw new TrueResult();
        } else {
            throw new FalseResult();
        }
    }

rolleyes
Nov 16, 2006

Sometimes you have to roll the hard... two?
Kill it with fire!

Zombywuf
Mar 29, 2008

code:
struct number {};
struct integer : public number {
  int value;
};
struct floating : public number {
  float value;
};
void print(const number &x) {
  try {
    throw x;
  } catch (integer i) {
    print_int(i.value);
  } catch (floating f) {
    print_float(f.value);
  }
}

qntm
Jun 17, 2009
Those are grotesque.

More, please.

pseudorandom name
May 6, 2007

A good start, but perhaps it could be made into a more generalized solution with templates?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Zombywuf posted:

code:
void print(const number &x) {
  try {
    throw x;

FWIW, this slices the object and will not be caught by either clause. Throwing a pointer wouldn't work either, because throws in C++ always use the static type of the exception expression.

Pucklynn
Sep 8, 2010

chop chop chop
I'm learning to program as a hobby, and I spend seven hours a day parked in front of a computer at work. My work really doesn't require much, so I spend a lot of that time here on the forums. I don't have admin access to the computers, so I can't install Ruby or Python on them, so no matter how much time I spend reading about it, I can't get any actual programming practice during that time.

My question is, are there any ways I can practice programming over the internet? IE, set up a server on my home computer or something and upload files to it? I could just write stuff all day and test it when I get home, but I feel like I wouldn't really get there with that approach.

At the very least, is there a web-based version of IRB (interactive Ruby something something) where I could test my code?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Pucklynn posted:

I'm learning to program as a hobby, and I spend seven hours a day parked in front of a computer at work. My work really doesn't require much, so I spend a lot of that time here on the forums. I don't have admin access to the computers, so I can't install Ruby or Python on them, so no matter how much time I spend reading about it, I can't get any actual programming practice during that time.

My question is, are there any ways I can practice programming over the internet? IE, set up a server on my home computer or something and upload files to it? I could just write stuff all day and test it when I get home, but I feel like I wouldn't really get there with that approach.

At the very least, is there a web-based version of IRB (interactive Ruby something something) where I could test my code?

There's lots of online python interpreters. Just google "online python interpreter".

There's also Portable Python which doesn't require installation.

mr_jim
Oct 30, 2006

OUT OF THE DARK

You could try http://codepad.org/, which has ruby and python interpreters. It's not quite the same as IRB, but it's pretty neat.

Pucklynn
Sep 8, 2010

chop chop chop
CodePad just made me cackle and clap my hands together like an underage evil mastermind. Thank you!

MutantBlue
Jun 8, 2001

Pucklynn posted:

CodePad just made me cackle and clap my hands together like an underage evil mastermind. Thank you!

Also look into Ideone, it's what I use for testing code snippets.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

Pucklynn posted:

My question is, are there any ways I can practice programming over the internet? IE, set up a server on my home computer or something and upload files to it? I could just write stuff all day and test it when I get home, but I feel like I wouldn't really get there with that approach.

At the very least, is there a web-based version of IRB (interactive Ruby something something) where I could test my code?
http://tryruby.org/

ToxicFrog
Apr 26, 2008


Pucklynn posted:

I'm learning to program as a hobby, and I spend seven hours a day parked in front of a computer at work. My work really doesn't require much, so I spend a lot of that time here on the forums. I don't have admin access to the computers, so I can't install Ruby or Python on them, so no matter how much time I spend reading about it, I can't get any actual programming practice during that time.

My question is, are there any ways I can practice programming over the internet? IE, set up a server on my home computer or something and upload files to it? I could just write stuff all day and test it when I get home, but I feel like I wouldn't really get there with that approach.

At the very least, is there a web-based version of IRB (interactive Ruby something something) where I could test my code?

Others have already linked you to online interpreters. Other approaches you can use:

- Portable Cygwin does not require admin rights to run, and is a complete linuxy development environment, including python, ruby, lua, etc interpreters and C/C++ compilers. Requires some comfort with the command line.
- You can also set up a server at home and use a portable version of PuTTY or NXClient to talk to it from work, either simple uploading/downloading of files, or doing actual development remotely.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
DJ Bernstein's primegen utility contains this snippet in its source:

code:
void byte_copy(to,n,from)
register char *to;
register unsigned int n;
register char *from;
{
  for (;;) {
    if (!n) return; *to++ = *from++; --n;
    if (!n) return; *to++ = *from++; --n;
    if (!n) return; *to++ = *from++; --n;
    if (!n) return; *to++ = *from++; --n;
  }
}
Can anyone explain why the loop is doing... uh, whatever it is it's trying to accomplish by putting that same check four consecutive times?

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Misogynist posted:

DJ Bernstein's primegen utility contains this snippet in its source:

code:
void byte_copy(to,n,from)
register char *to;
register unsigned int n;
register char *from;
{
  for (;;) {
    if (!n) return; *to++ = *from++; --n;
    if (!n) return; *to++ = *from++; --n;
    if (!n) return; *to++ = *from++; --n;
    if (!n) return; *to++ = *from++; --n;
  }
}
Can anyone explain why the loop is doing... uh, whatever it is it's trying to accomplish by putting that same check four consecutive times?

Unrolling the loop 4x manually

Between that, the fact that register is used (almost all compilers ignore it entirely), and pre-C89 function syntax is used, I'm tempted to think this is originally from something much older, but I wouldn't expect djb to do that given his nature, sooooooooo



Also the argument order is kinda curious imo

Nippashish
Nov 2, 2005

Let me see you dance!

Misogynist posted:

Can anyone explain why the loop is doing... uh, whatever it is it's trying to accomplish by putting that same check four consecutive times?

It's been unrolled.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
Note that it's formatted in a confusing way for no good reason. This would've made it obvious that the if's aren't actually consecutive and that n is changing between each:
code:
void byte_copy(to,n,from)
register char *to;
register unsigned int n;
register char *from;
{
  for (;;) {
    if (!n) return;
    *to++ = *from++;
    --n;
    if (!n) return;
    *to++ = *from++;
    --n;
    if (!n) return;
    *to++ = *from++;
    --n;
    if (!n) return;
    *to++ = *from++;
    --n;
  }
}

ChiralCondensate
Nov 13, 2007

what is that man doing to his colour palette?
Grimey Drawer

Mustach posted:

Note that it's formatted in a confusing way for no good reason.
Actually, there's a very good reason: having the same characters right on top of each other makes it easy at a glance to see that the four lines are exactly the same.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

Nippashish posted:

It's been unrolled.
I figured as much but was confused about why this would be done by hand instead of letting the compiler optimize it, then I realized that the compiler won't unroll an infinite loop for obvious reasons. Thanks.

baquerd
Jul 2, 2007

by FactsAreUseless

Misogynist posted:

I figured as much but was confused about why this would be done by hand instead of letting the compiler optimize it, then I realized that the compiler won't unroll an infinite loop for obvious reasons. Thanks.

Even very modern compilers working with finite loops can benefit amazingly from manual loop unrolling, especially if you want to target specific hardware.

MrMoo
Sep 14, 2000

The code is inefficient on current hardware though, you get better performance with by prefetching the source buffer in parallel and filling the cache line:

code:
void byte_copy(to,n,from)
register char *to;
register unsigned int n;
register char *from;
{
  int i = 0, count8 = n >> 3;
  if (count8) {
    while (count8--) {
      to[i]   = from[i];
      to[i+1] = from[i+1];
      to[i+2] = from[i+2];
      to[i+3] = from[i+3];
      to[i+4] = from[i+4];
      to[i+5] = from[i+5];
      to[i+6] = from[i+6];
      to[i+7] = from[i+7];
      i += 8;
    }
    n %= 8;
  }
  while (n--) {
    to[i] = from[i];
    i++;
  }
}
That's assuming the pointers are aligned, if they are not you can do a loop to align up first. If both pointers have different alignment you are screwed no matter what.

Similarly you can get more performance by copying with longer words but not always. Best performance is to use the assembler instruction for string copy if the string is long enough to overcome the instruction startup costs. On latest Nehalem chipsets as has been posted about a lot it's actually faster to copy in reverse than forward too.

MrMoo fucked around with this message at 16:10 on Jul 1, 2011

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

ChiralCondensate posted:

Actually, there's a very good reason: having the same characters right on top of each other makes it easy at a glance to see that the four lines are exactly the same.
That sounds like a worthless reason to me.

shrughes
Oct 11, 2008

(call/cc call/cc)

Mustach posted:

That sounds like a worthless reason to me.

You are a source code canonicalizationist and you will burn in hell.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
Are there any Haskell implementations that run from a USB key without too much screwing around?

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES
EDIT: Wrong thread.

Adbot
ADBOT LOVES YOU

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

shrughes posted:

You are a source code canonicalization and you will burn in hell.
I just don't like seeing people miss return statements that are hidden within the same line as two other statements.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply