language-agnostic

Converting EBNF to BNF

It's been a few years since my computer-language class and so I've forgotten the finer points of BNF's and EBNF's and I don't have a textbook next to me. Specifically, I've forgotten how to convert an EBNF into BNF. From what little I remember, I know that one of the main points is to convert { term } into <term> | <many-terms>. But I d...

Articles about replication schemes/algorithms?

I'm designing a distributed system with a certain flow of data in it. I'd like to guarantee that at least N nodes have almost-current data at any given time. I do not need complete consistency, only eventual consistency (t.i. for any time instant, the current snapshot of data should eventually appear on at least N nodes. It is tricky to ...

Derived class linker - is this wrong?

We have this situation: Window Keyboard ^ ^ | / ApplicationWindow so class Window { } class Keyboard { } class AppWindow : public Window, public Keyboard { } Now, Keyboard wants to access a property in ApplicationWindow, for example,...

Why do static Create methods exist?

I was wondering, why do static Create methods exist? For instance, why use this code: System.Xml.XmlReader reader = System.Xml.XmlReader.Create(inputUri); over this code: System.Xml.XmlReader reader = new System.Xml.XmlReader(inputUri); I cannot find the rationale for using one over the other, and can't find any relation between c...

Self-Configuring Classes W/ Command Line Args: Pattern or Anti-Pattern?

I've got a program where a lot of classes have really complicated configuration requirements. I've adopted the pattern of decentralizing the configuration and allowing each class to take and parse the command line/configuration file arguments in its c'tor and do whatever it needs with them. (These are very coarse-grained classes that a...

Fix common library functions, or abandon then?

Imagine i have a function with a bug in it: Pseudo-code: void Foo(LPVOID o) { //implementation details omitted } The problem is the user passed null: Object bar = null; ... Foo(bar); Then the function might crash due to a access violation; but it could also happen to work fine. The bug is that the function should have been...

Calculating which line is in front at the point where two line projections intersect

Two lines in 3d are projected onto 2d screen, and their projections intersect at a point which can be calculated. If the endpoints of the lines in 3d space are known and their intersection point in the projection plane is also known, how can I determine which line is in front at this intersection point? ...

Why does the arrow go up in inheritance?

When you draw an inheritance diagram you usually go Base ^ | Derived Derived extends Base. So why does the arrow go up? I thought it means that "Derived communicates with Base" by calling functions in it, but Base cannot call func...

Should you always write code for else cases that "can never happen"?

Take some code like if (person.IsMale()) { doGuyStuff(); } else { doGirlStuff(); } (Yes, I realize this is bad OO code, it's an example) Should this be written to explicitly check if person.isFemale(), and then add a new else that throws an exception? Maybe you're checking values in an enum, or something like that. You think ...

Does a programmer's "document template" with tags exist for Windows?

I was wondering (if possible) if there was a program/tool/utility that when I create a new file and provide it with an extension that it creates the appropriate tags automatically? For example, a new file I create called index.php would have the appropriate tags auto-generated inside: <?php ?> I hope you get the idea. Any infor...

Creating an Improved Digital Zoom

Hey, Ok, so I have a given video source (for the sake of the example, it is a camera). It does not have optical zoom, but we supply digital zoom instead. Now this digital zoom is pretty simple, simply cropping the image to a specified portion, and filling the screen with that portion. The problem is that the zoomed video can have pre...

How to pass non-fatal warnings from a library

A library function parses a file and returns an object. If a parser encounters unknown data, missing values etc., it shouldn't throw an exception and stop parsing (because this is not fatal), but there should be a way to pass information about these things to a caller (so that warnings can be displayed in the UI, for example). How can th...

Examples of monoids/semigroups in programming

It is well-known that monoids are stunningly ubiquitous in programing. They are so ubiquitous and so useful that I, as a 'hobby project', am working on a system that is completely based on their properties (distributed data aggregation). To make the system useful I need useful monoids :) I already know of these: Numeric or matrix sum ...

Rewriting UNIX cal(1)

Hello, Today I was testing out SRFI 19 and wrote a simple version of the UNIX cal(1) command. Here's a version in R6RS Scheme which runs in Ikarus, Ypsilon, and Larceny. A few example runs. Schemers: How would you write it? Use your favorite implementation. Ruby and Python: I'm guessing that y'all have elegant date and time libraries...

Are licenses relevant for small code snippets?

When I'm about to write a short algorithm, I first check in the base class library I'm using whether the algorithm is implemented in it. If not, I often do a quick google search to see if someone has done it before (which is the case, 19 times out of 20). Most of the time, I find the exact code I need. Sometimes it's clear what license...

Properties of bad fibonacci algorithm

I was looking at the canonical bad fibonacci algorithm the other day: public static int fib(int n) { // Base Case if (n < 2) return 1; else return fib(n-1) + fib(n-2); } I made the interesting observation. When you call fib(n), then for k between 1 and n fib(k) is called precisely fib(n-k+1) times (or ...

Serializing persistent/functional data structures

Persistent data structures depend on the sharing of structure for efficiency. For an example, see here. How can I preserve the structure sharing when I serialize the data structures and write them to a file or database? If I just naively traverse the datastructures, I'll store the correct values, but I'll lose the structure sharing....

How to keep confirmation messages after POST while doing a post-submit redirect?

Hello, I'm looking for advise on how to share certain bits of data (i.e. post-submit confirmation messages) between individual requests in a web application. Let me explain: Current approach: user submits an add/edit form for a resource if there were no errors, user is shown a confirmation with links to: submit a new resource (for "a...

Heuristic to identify if a series of 4 bytes chunks of data are integers or floats

What's the best heuristic I can use to identify whether a chunk of X 4-bytes are integers or floats? A human can do this easily, but I wanted to do it programmatically. I realize that since every combination of bits will result in a valid integer and (almost?) all of them will also result in a valid float, there is no way to know for su...

Coding Competition, language agnostic guidelines?

Hi there: I might be doing a coding competition soon, I was wondering if anyone made one and what where the guidelines/ process. I'd like to make the competition appealing to all devs, and I m trying to come up with ideas as to how. the scenario is: There is an event running and we(of the coding competition) will have a room that we can...