Tuesday, September 29, 2009

No One Cares

What happens to the operand stack in the JVM when an exception is caught locally? The internet seems to say that it's emptied. The JVM Specification doesn't say.

... I care.

Monday, September 28, 2009

Java Riddle: instanceof and casting

Is it possible that the System.out.println line ever gets executed?

void brokenFunction(Object stringMaybe) {
if (stringMaybe instanceof String)
String stringForSure = (String)stringMaybe;

Friday, September 25, 2009

It's-A* Me

youNeedToWatchThis = youCareAboutVideoGames * youCareAboutProgramming

Ai Mario

proof it's real

What does Turing Complete mean?

Consulting the magnifluous fount of perpetual enlightenment:

Turing completeness:

A computational system that can compute every Turing-computable function is called Turing-complete (or Turing-powerful). Alternatively, such a system is one that can simulate a universal Turing machine.

Thank you, circular definitions.

In English:

A turing-complete programming language is a "real" programming language. The quick test is whether it has control structures that can go backwards: loops and/or recursion. That's pretty much it.


C/C++, Java, Python, Ruby: Yes
a calculator: No
spreadsheet formulas (Excel, etc.): No
Lisp, Haskell, Prolog: Yes
assembly languages: Yes

Who cares:

The big deal with turing-complete programming languages is that any problem you can solve with one of them, you can solve with any other of them. So you could theoretically write gcc in BrainF if you had enough patience and system resources. You cannot, however, write gcc in Excel formulas.

Name that/those Programming Language(s)

What non-esoteric, turing-complete programming languages allow this fragment of a line to show up in source code?
code: " 1 2 "
(not including the quotes. can't be inside a literal string or comment.)
To help you out, C is NOT an answer.

There are many answers. How many can you come up with? So far I'm at 5.

Thursday, September 24, 2009

The Beauty of Python

OOffice pastes tables into plain text 1 cell per line with no indication of row breaks. Dumb. this prompted me to convert the text into a list of tuples in python. I got so far as getting a list of cell data, then I needed to take every 4 items in the list and make a tuple out of them.

Here's the problem I was faced with:

input: ["a", "b", "c", "d", "w", "x", "y", "z", ...]
output: [("a", "b", "c", "d"), ("w", "x", "y", "z"), ...]

After asking around in the python irc channel, Andy got the following solution, and while writing this post, I also found it in Python's documentation for the zip function:

output = zip(*[iter(input)]*4)

It's hard to overstate my satisfaction, this is such an intricate, concise solution.

What is going on

from the inside out:
iter(input): this makes an iterator object that traverses the input list. The iterator stores the state of which item it's on, and what comes next. [iter(input)]: this makes a list with 1 item in it, the iterator.
[iter(input)]*4: this takes the list, duplicates it 4 times, and concats it all together. ex: [1,2]*3 => [1,2,1,2,1,2]. An important thing to note in this step is that just the one iterator object with its state information occupies all four slots in the list.
zip: python's documentation. Note that the function takes lists as separate arguments
zip(*[iter(input)]*4): the * expands the list to its right into an argument list for the zip function. So it's equivalent to giving 4 arguments to the zip function. Here's where the magic happens.

the zip functions traverses each of its four arguments in parallel. Before an iterator has been moved, its "next" item is the list's first item. The zip function asks the first list's iterator for the "next" item, giving the first item in the list. Then the zip function asks the second iterator for its "next" item, which is typically the first item in the second list. However, the second iterator is the same object as the first iterator, and its "next" item is the second item in the list, not anyone's first item. And so the zip function is tricked into thinking that it is traversing 4 separate lists of length 2 each when really it is traversing over a single list of length 8. The result is exactly what we want.

Tuesday, September 15, 2009

I beat n 88.4

I don't care if you don't care, this is getting blogged.

Saturday, September 12, 2009

postfix/prefix lol

C/C++ and Java


int a = 0;
int b = a++ + ++a;

Not valid:

int a = 0;
int b = a+++++a;

Too bad, right? It looks so fun.

Friday, September 11, 2009

Logical XOR operator

operator bitwise logical
not ~ !
and & &&
or | ||
xor ^ ???????

Where's the logical XOR operator?? One does exist in Java. Do you know what it is?

Java Quiz

In order for a method to be inlined, the method must NOT be overridable (virtual in C++ terms).

Under what 5 conditions is a method not overridable?

1. The method is explicitly marked as "final".
2. The method is _________.
3. The method is _________.
4. The method's class is _________.
5. The method's class is _________.

The answer is on the internet if you want to cheat.

Thursday, September 10, 2009

Selfish Promotion: Jax

When I look at the following code:

Scanner scanner;
try {
scanner = new Scanner(filename);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
ArrayList lines = new ArrayList();
while (scanner.hasNextLine())

All I can think of is what a shame it is that I have to declare scanner before the try-catch block (and that the curlies are mandatory).

Here's the equivalent in my experimental programming language Jax:

Scanner scanner = try new Scanner(filename)
catch (FileNotFoundException e)
throw new RuntimeException(e);
ArrayList lines = new ArrayList();
while (scanner.hasNextLine())

That first line shows how try-catch blocks evaluate to an expression. Everyone should be thrilled about Jax so I have motivation to continue working on it!

Tuesday, September 1, 2009

Failure Shorthand

Why type
throw new NullPointerException();
when you could just type
throw null;