Partial Application in Actionscript 3.0

UNFINISHED, but I’ll just post it for now, cause who knows when I’ll get around to this again!

One thing I don’t like doing is labeling things as “advanced”. I remember back in the day when I was just-barely-a-teenager and I was figuring out C. I read through a book or two and got to the topic of “linked lists”. I remember back then thinking that sort of stuff was considered, in my and the book’s opinion, “advanced”–and that I thought was pretty hardcore knowing how to do it. I knew that knowing it didn’t necessarily make me the best programmer in the world… but I had to have definitely been somewhere in the top 10. Maybe even the top 5.

Today, I’m a tiny bit wiser than I was back then.

For functional programmers, currying and/or partial application may be well understood early in the learning process. Actionscript 3.0, an ECMAScript language, is actually extremely versitile, even when it comes to those crazy Functional Programming topics. Still, AS3’s “classical OOP” appearance can make some of those functional programming paradigms a rough fit.

So I won’t say that this stuff is “advanced”, but if you’re not intamitely familiar with AS3, and if you’re not already comfortable with closures and partial application from other languages, then this is probably going to hurt, at least a little bit.

For the record, I’m a beginner at this sort of stuff. Keep your eyes peeled for mistakes!

Continue Reading »

Actionscript 3.0
Tutorials

Comments (7)

Permalink

Closures in Actionscript 3.0

For right now, this is a direct copy of a forum post (with a hard character limit on posts). I might come back and spiff it up some, but then again maybe not. I DO WHAT I WANT!

It’s on closures, and it’s stuff you need to know before looking at the stuff on partial application. You don’t necessarily need to know the stuff I address here specifically, just what a closure is and stuff.

First, before I even start to get into this, I want to say that Actionscript is an ECMAScript derivative. Javascript also falls into this category, so you can often find tutorials, examples, etc which translate easily from one language to another. A VERY good explanation of Closures and a few of their uses can be found here, but is biased towards Javascript, and isn’t the most accessible thing out there ;)

Second, the uses of a “closure” are not really a clear-cut. If someone asked you “what can a class be used for?”, there would be no single definite way to answer the question. Closures, in fact, can almost be thought of as another way of representing a class (but don’t think of them that way!), but offer other practical uses as well. In this example, we’ll discuss what a closure is and then use one to help solve the common problem of “passing arguments to event handlers”, as well as pick up a nifty trick or two.

Continue Reading »

Actionscript 3.0
Tutorials

Comments (1)

Permalink

Implicit Conversion and Performance in Actionscript 3.0

I remember when I started messing with this Flash/AS3 business a while ago, I read about these “new” uint and int types that would supposedly perform better than the “Number” type. THEN I read a bunch of articles by “reputable” people posting about how the performance gains didn’t exist, and which gave seemingly random benchmarks on some tests run using them.

So I looked into it and, barring some crazy patching by Adobe since those original articles were written (over a year ago as of this posting), I’m left to believe that they were just plain wrong. I think that this was due to a misunderstanding of the implicit conversions that happen, even though the authors of these articles *seemed* to be aware of the issue. Who knows.

But here are my results:

UINT time: 90
INT time: 92
NUMBER time: 303

WOW, seems pretty much like what you would expect, huh? Isn’t that just CRAZY?

Continue Reading »

Actionscript 3.0
General

Comments (4)

Permalink

Base 2 Fraction Fiasco

This stuff is fairly basic, and exists as a base for future posts mostly.

If you’re an old, early-twenties fogey such as myself, you can remember back to a time when it cost a few grand to buy a computer which, instead of using a hard disk drive, took floppy disks the size of your head. Still, even in that ancient age, people knew that “computers talk in ones and zeros”. That’s right. If you don’t know the first thing about computers, you still know they speak in binary.

These ones and zeros work really well for integral values, but computers will choke and gag on fractional values more often than you realize. And I’m not just talking about ultra tiny fractions representing the width of a grain of sand in miles… even for simple stuff. Consider the following classic example:

#include <iostream>

int main(int argc, char *argv[])
{
	float one_tenth = 0.1f;
	float total = 0.0f;

	for(int i=0;i<1000;i++)
		total += one_tenth;
	std::cout << total;

	std::cin.get();
}

The program is pretty straightforward. Take a variable (set to 0) and add 0.1 to it a thousand times. If you don’t feel like whipping out a calculator and adding 0.1 over and over again, then trust me–the answer will be 100. So we hit compile, kick back, and get ready to bask in the glow of the 100 that’s about to be printed to our screen:

99.99

Ahh… enjoy that beautiful 100 glow. Wait, 99.99? What? I mean it’s just adding 0.1 a bunch, so there wouldn’t be any rounding errors, right? RIGHT?!?

The price is WRONG!
Continue Reading »

Uncategorized

Comments (4)

Permalink

Fast Fourier Transform Overview and Really Rough Beat Detection

So with all of this Guitar Hero and Rock Band garbage going around lately, every kid out there wants to make a rhythm based Flash game. It turns out that there have been some fairly complex attempts at this, and it also turns out that I didn’t know that until AFTER I started looking into it on my own.

The story goes that some random person on a forum asked for some help with this. I suggested using Actionscript’s FFT option on the computeSpectrum function. Some other random person acted like the idea wasn’t worth investigating, so I decided to take a stab at it myself to prove this person wrong. My attempt would be pretty trivial and straightforward, but hopefully a “good start”

Turned out that it was really easy to make excellent beat detection this way. And by “excellent”, I mean “absolutely awful”.

You can check out the finished product here. There’s no preloader, so just wait (2.5mb). Not impressed? Might want to skip this. Think it’s at least *sort of* cool and *potentially useful*? Read on, youngin’!
Continue Reading »

Actionscript 3.0
Tutorials

Comments (3)

Permalink

Iterator Invalidation

These days there’s this well-seated OOP craze in the CS world. OOP is far from new, but in the relatively recent past (10-20 years?) it has gone from “Oh, that” to rock stardom. Everyone loves to “abstract away from implementation details”. The idea sounds great on paper, but sometimes it’ll bite ya in the ass.

The standard C++ library has a bunch of built in data containers to make your life easier. The most common one is probably the vector. Vectors store their contents in a contiguous block of memory. They can be iterated through very efficiently and make your cache much happier. Vectors also come with a bunch of operations. Some of my favorites are the push_back and pop functions.

How do these functions work internally? Ahh, that’s just a tiny implementation detail! Or IS it?

Let’s say we have the following program:

#include <iostream>
#include <vector>

int main(int argc, char* argv[])
{
	std::vector<char> vowels;

	std::cout << "Setting up our vector of vicious vowels!" << std::endl;
	vowels.push_back('a');
	vowels.push_back('e');
	vowels.push_back('i');
	vowels.push_back('o');

	char* favorite_vowel = &vowels[2]; // Our favorite vowel is i!

	std::cout << "Favorite vowel: " << *favorite_vowel << std::endl;

	std::cout << "WAIT!  We forgot to include 'u'.  Let's stick it on the end of the array!" << std::endl;

	vowels.push_back('u');

	std::cout << "Okay, but my favorite vowel is still: " << *favorite_vowel << std::endl;

	std::cin.get();

	return 0;
}

Your output may be different than mine, but when I compile and run this program, I get the following:

Setting up our vector of vicious vowels!
Favorite vowel: i
WAIT!  We forgot to include 'u'.  Let's stick it on the end of the array!
Okay, but my favorite vowel is still: 3

Wait a second… my favorite vowel isn’t 3. In fact, 3 isn’t a vowel–it isn’t even a letter! And what’s more, there is no 3 in our entire program. So where the hell is our 3 coming from?
Continue Reading »

C++
General

Comments (0)

Permalink

Function Overloading and Default Parameters

One post I responded to on a forum was regarding overloaded functions and default parameters–was there any performance difference? I was pretty sure there wasn’t, but I put some time into figuring it out and illustrating the concepts, so I figured I might as well post it here. Most of THIS post is THAT post verbatim. So if you’ve read that post, then there’s nothing new here, really.

“Overloading functions” is a little bit misleading. Having two identically named functions with different parameters doesn’t produce one magical function that sorts itself out with no runtime cost (but overloaded functions don’t have any runtime cost). Overloading a function just puts the work of writing separate function names on the compiler–two independent functions are still generated. For instance, in this program:

Continue Reading »

C++
General

Comments (0)

Permalink

Pointer Decay in C++

“Arrays are really just pointers”.

This is one of the biggest “gotchas” out there. It’s one that, at the end of the day, really isn’t even that big of a deal. You could live happily for the rest of your life convinced that arrays and pointers are the exact same thing (though I wouldn’t recommend it). But arrays aren’t pointers. They decay into pointers. Plus you can’t change where they point to, but that really isn’t the point here..

Before I go on, I want to pick this very obvious spot to point out that everything I’m about to explain was pulled almost entirely from this thread on gamedev, including most of the program below (with other, clearer versions in the thread). Reading through that post will probably benefit you much more than reading through this one, especially the parts on the extra indirection after a function call towards the end, which won’t be mentioned here.

Anyway, take the following code:

#include <iostream>
#include <string>

static const size_t ARRAY_SIZE=5;

void display_array_info(int size, const std::string &title)
{
	std::cout << title << std::endl;
	std::cout << "Array size: " << size << std::endl;
}

template <typename T, size_t U>
void reference(const T (&some_array)[U])
{
	display_array_info(sizeof(some_array), "By Reference:");
}
template <typename T, size_t U>
void value(const T some_array[])
{
	display_array_info(sizeof(some_array), "By \"Value\":");
}

template <typename U>
void pointer(const U* const some_array)
{
	display_array_info(sizeof(some_array), "By Pointer:");
}

int main(int argc, char *argv[]) {
	int integer_array[ARRAY_SIZE] = { 2, 4, 6, 8, 10 };

	std::cout << "From Main:" << std::endl;
	std::cout << "Array size: " << sizeof(integer_array) << std::endl;

	pointer<int>(integer_array);
	value<int, ARRAY_SIZE>(integer_array);
	reference<int, ARRAY_SIZE>(integer_array);

	std::cin.get();

	return 0;
}

Which, for me, will output:

From Main:
Array size: 20
By Pointer:
Array size: 4
By "Value":
Array size: 4
By Reference:
Array size: 20

(the array takes up 20 bytes in total…)

So what exactly is going on here?
Continue Reading »

C++
General

Comments (0)

Permalink

Starting up

Heya!

Thank you one-click Dreamhost install. If you wanna know a little bit more about why this site is taking up space on the internet, check out the About page.

For the time being, I’m just going to copy and paste some replies I’ve made on some forums. Needless to say they’ll be a rough fit at first, as I wasn’t authoring them for this site. I’ll edit them in the coming days until they fit more a little bit better, then see about moving forward :)

Uncategorized

Comments (0)

Permalink