JAXB: How should I marshal complex nested data structures?

I’ve solved the problem without XmlAdapter’s. I’ve written JAXB-annotated objects for Map, Map.Entry and Collection. The main idea is inside the method xmlizeNestedStructure(…): Take a look at the code: public final class Adapters { private Adapters() { } public static Class<?>[] getXmlClasses() { return new Class<?>[]{ XMap.class, XEntry.class, XCollection.class, XCount.class }; } public static Object … Read more

Dojo require() and AMD (1.7)

The dependency array format: define([“a”, “b”, “c”], function (a, b, c) { }); can indeed be annoying and error-prone. Matching up the array entries to function parameters is a real pain. I prefer the require format (“Simplified CommonJS Wrapper”): define(function (require) { var a = require(“a”); var b = require(“b”); var c = require(“c”); }); … Read more

How to implement Swift-like enums with associated values in JavaScript?

You can use a discriminated union for this. This example uses Typescript, but the concept would be similar for Javascript just without the type safety. interface UPCA { kind: “UPCA”; numberSystem: number; manufacturer: number; item: number; checkDigit: number; } interface QRCode { kind: “QRCode”; data: string; } interface Other { kind: “Other”; } type Barcode … Read more

Best algorithm to test if a linked list has a cycle

Have two pointers iterating through the list; make one iterate through at twice the speed of the other, and compare their positions at each step. Off the top of my head, something like: node* tortoise(begin), * hare(begin); while(hare = hare->next) { if(hare == tortoise) { throw std::logic_error(“There’s a cycle”); } hare = hare->next; if(hare == … Read more

How does structural recursion differ from generative recursion?

The key difference between structural and generative recursion is where a recursive procedure gets the data that it works on and how it processes that data. Specifically, for structural recursion, a recursive call is made on a subset of the original input data. Whereas for generative recursion, a recursive call is made on data that … Read more

Fast way to implement pop_front to a std::vector

I would expect: template<typename T> void pop_front(std::vector<T>& vec) { assert(!vec.empty()); vec.front() = std::move(vec.back()); vec.pop_back(); } to be the most efficient way of doing this, but it does not maintain the order of the elements in the vector. If you need to maintain the order of the remaining elements in vec, you can do: template<typename T> … Read more