# Jes' Codex

jes5199 of blog
Posts I Like

Math doesn’t work correctly:

> 212227119877294391 * 86400

=> 359548130941076096

On non-broken rubies:

> 212227119877294391 * 86400

=> 18336423157398235382400

(This has something to do with Fixnums that are larger than 32 bits failing to jump to Bignums correctly. I do not know whether it affects other 64bit versions of ruby)

wooo

I realized that I’ll never be able to finish inventing a programming language if I don’t learn a LISP first.

So I’ve been playing with Project Euler with a Clojure repl, and … well, it’s not ruby and it’s not Haskell.

But it has a lot of lazy data structures, so I decided to pretend I was writing Haskell anyway and see what happens.

Here’s a Haskell-ish way to generate the fibonacci numbers:

`let fib = 1 : 1 : (map (uncurry (+)) (zip (drop 1 fib) fib))`

So I decided to port it directly to Clojure. It wouldn’t be possible to do in a language that didn’t have lazy sequences, but I was able to do it in Clojure pretty straightforwardly. Here’s a before-and-after, so you can see how the two definitions relate:

`````` let  fib    =            1:1 : (map (uncurry      (+)) ( zip                     (drop 1  fib )  fib    ) )
(defn fib [] ( lazy-cat [ 1 1 ] (map (partial apply + ) ( partition 2 (interleave (drop 1 (fib)) (fib) ) ) ) ) )

``````

Gosh, you know, after a while you don’t even see those parentheses.

Let’s see how it works!

` `

`Prelude> take 30 fib[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040]`

` `

And the Clojure:

` `

`user=> (take 30 (fib))java.lang.OutOfMemoryError: Java heap space(1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393`

` `

Well, there you have it.

OK, so (for @keystricken’s birthday, [hi, Mon!]) I made a thing (with the help of Shapeways’s Selective Laser Sintering machine)

Which is really just a physical projection of a 3D model I made in CAD:

which was really challenging since I had never really used 3D CAD before at all.

after some flailing about, I came up with a process that looked like this:

1. Draw something on paper
2. Scan it in
3. Use The GIMP’s filters and levels a to turn it into a monochrome (like seriously just black and white, not greyscale) image.
4. Use Inkscape’s “Trace Bitmap” command to turn the drawings into SVG vector graphics
5. Import the SVG into FreeCAD “as geometry”
6. Notice that FreeCAD has mysteriously failed to correctly understand some of the shapes
7. curse, reopen the SVG in Inkscape, stare at the control points of the paths until I notice that there’s two points on the same pixel (don’t ask me how I can see that), delete one, and save again
8. Import the SVG into FreeCAD “as geometry”
9. Extrude the geometry into solids
10. Notice that FreeCAD has failed to understand that white shapes inside of black shapes are actually “holes”
11. Use the FreeCAD “Boolean Difference” function to generate new solids that are black shapes with the white shapes subtracted … one pair at a time.
12. Get confused as hell, and try to backtrack even though “Undo” always seems to be disabled
13. Cut and paste these solids into the main document
14. Using my high-school algebra, figure out what angle to rotate the solids to
15. Notice that every pasted object seems to have a new and different understanding of where the point 0,0,0 is, so that math on the coordinates is meaningless.
16. Try to use the mouse to move a shape: either mysteriously fling an object way, way outside the visible scope of the document, or accidentally hit “ESC” while dragging the object (which is not ‘cancel’ like my muscle memory suggests, but rather “please crash the program”)
17. Give up on the mouse and painstakingly, manually move each object into an X/Y/Z coordinate triple using the numeric entry boxes that puts it into a place that doesn’t look completely wrong.
18. (Eventually) export the CAD model as VRML
19. Try to open the VRML file in Meshworks
20. Meshwork says it’s not VRML! Run `file` on the file
22. gunzip the file
23. Open the VRML file in Meshworks
24. drag it around in Meshworks, and marvel at how it’s both easier to manipulate and harder to see details on, compared to FreeCAD
25. Export the model to STL
26. Upload the STL file to Shapeways
27. Wait for an automated email
28. Holy crap it worked.
29. Give Shapeways some money
30. Wait a couple weeks

I think I can probably do better in the future. It seems like it should be possible to make a toolchain that does (image)->Trace->Extrude->Rotate->Translate->Export without human intervention.

If I had that, I’d actually work on writing a program that solved the interesting parts of the sundial (most of which I solved on paper for this prototype)

1. Solar noon is always north/up, but what clock time that is locally depends on how far east or west you are from the central meridian of your time zone. The clock dial is rotated by a number of degrees that’s your time zone’s central longitude minus your actual longitude.
2. In order to have evenly spaced shadows for each hour (see Equatorial Sundial), the dial should be inclined to an angle that’s 90 degrees minus your longitude. That means that your lower gnomon (which doubles as the support) should be at a length that can be calculated using trigonometry (uh, the cotangent of your latitude times the radius of the dial, I think)
3. The top dial is your “summer” dial and the lower dial is your “winter” dial. A nice cheat is to have the top dial already offset by 15 degrees so that it is set to Daylight Savings Time
4. The lower dial runs counter-clockwise. In this prototype, I cheated and just mirrored the lower dial. Mirrored Roman Numerals are actually sort of evil, though, since IV becomes VI, etc.

I’ve been trying to learn some old-school algorithms; the sort of things that CS people study but rarely come up at programming gigs.

The thing is, descriptions of algorithms online tend to suck. and open source implementations of textbook algorithms tend to suck.

So, I’m going to try to improve the situation. So, here’s my first attempt. A* algorithm in simple readable ruby: https://github.com/jes5199/astar/blob/master/astar.rb

In real code, we found a very, very slow loop. Changing two characters:

`+=` is about 100 seconds.
`<<` is around 1/3 of a second.

Using `+=` on a string creates a copy and then discards the original, while `<<` allows the ruby interpreter to intelligently resize the buffer.

The only gotcha is that `<<` will mutate your string object, which means that it’s not safe to use on a method’s parameters. `#dup` those strings, if you need to append to them.

organizations which design systems are constrained to produce designs which are copies of the communication structures of these organizations.
Rogues are very keen in their profession, and know already much more than we can teach them respecting their several kinds of roguery.

I love everything about this internet artifact I’ve found: http://en.wikipedia.org/wiki/Full_disclosure#History . Actually, I want you to have today’s snapshot of the wikipedia article - I suspect that eventually this page will get cleaned up into an encyclopedia article, and it’s just too nice as work of (oh, I don’t know) postmodern literature? cyberspace effluvia? bird cage lining?

————— Forwarded message —————
From: Markus Roberts
Date: Tue, Sep 14, 2010 at 1:57 PM
Subject: [Puppet-dev] Oops

Funny story: as the number of developers working on Puppet and related projects at PuppetLabs has increased, the average distance between them has dropped geometrically*, to the point that if we want to discuss something we’re working on with someone, we can (in many cases literally) reach out and tap a fellow developer on the shoulder.  This is great for us but it has had an unfortunate unintended consequence: we’ve been doing more and more face-to-face discussions and less and less on this list.

Oops.

This wasn’t intentional and (though it seems blindingly obvious in hindsight) wasn’t expected either.  It was, as one five year old put it, a blundersight.

So we’re going to try something we hope will fix things; office chat is still going to go on as normal — we’ll spare you our debates about whether we should try having lunch at the place with the cult videos on all the TVs again or who’s going to show the new developer the ropes — but if anyone starts to have a substantive discussion about Puppet & friends we’ll cut them off and tell them to tell it to the list.

That means that when we’re kicking around ideas you’ll get to see them and put in your two cents worth on equal footing with the developers in the office (since we really do want your input).  It also means that you’ll get to see some ideas that never go anywhere, and crazy things that are (mercifully) shot down in their first few hours.  But then you’ll also get to see the direction of our thinking long before some of it results in a line of code, and help us shape it before anything is settled.

Hopefully, it’ll be just like the old days.

— PuppetLab Developer Team:

Luke Kanies
Markus Roberts
Rein Henrichs
Jesse Wolfe
Igal Koshevoy
Matt Robinson
Nick Lewis
Paul Berry
Jacob Helwig

* Actually, it’s probably something like k*(sqrt(s^2/n)-r) for n developers of mean radius r in a s x s office, with k some constant around 3.

I participated in the 2010 ICFP programming contest a couple weeks ago, and my team (“vorpal”) did the best of any team I’ve been on yet: 16th place.

While I’m waiting for teammate Paul Berry to write a detailed retelling of our progress, it occurred to me that I want to share the most elegant function I’ve written in a long time:

`type Fuel = [[[Integer]]]`

`parseFuel :: String -> (Fuel, String)parseFuel = parseTritList \$ parseTritList \$ parseTritList \$ parseIntSchemeTwo`

where I was taking a string of trinary digits and creating a “Fuel”, which turned out to be, in the context of the puzzle, a list of lists of lists of integers.

It helps that the ICFP’s serialization format was elegantly recursive, but I can’t help but have the feeling that Haskell functions are composable in a way that other languages aren’t. Which is frustrating, because generally I don’t want to be writing Haskell, there’s just occasional moments like this where it’s a perfect fit.

In traditional haskell style, I’ll give you the type signature of the component functions, and leave the implementation as an exercise for the reader:

`parseIntSchemeTwo :: String -> (Integer, String)parseTritList :: (String -> (a, String)) -> String -> ([a], String)`