Immutable Collections for .NET
Jared Parsons has a created a library that provides, among other things, immutable collections.
Speak softly and carry a big laptop
Jared Parsons has a created a library that provides, among other things, immutable collections.
Reading this post about the Windows 7 interop samples, all I can think is “uh-oh, it’s Indexing Service all over again”.
Microsoft invented all the components for successful desktop search back in the year 2000. But they didn’t think about scenarios, and then Apple came with the same concept done right and got all the accolades. And now with the Sensor and Location APIs, Microsoft is continuing to use their old play book instead of learning and improving.
The following sentence tells it all:
For example, the following image shows an updated version of the MSDN Reader. This version of the MSDN Reader changes the way the application looks depending upon the amount of light the Ambient Light Sensor detects.
Changing the app colors based on the ambient light? Really? Is that the best you can do? Where is your imagination?
Why not make the applications smarter? Fix your fucking Bluetooth stack and drivers and make my computer know if I’m near it based on the location of my phone. The use that to (maybe) lock and unlock my computer, set the sound level for various notifications, and do other things I need.
And use your position to bend the manufacturers’ arms into adding more sensors to their laptops. I want microphone arrays. I want high-quality webcams. I want an RFID tag on my wristwatch. I want my computer and my phone to remind me to take out the trash when leaving the house because they figured out that my Wi-Fi signal is getting weaker.
I know that asking manufacturers to spend those extra $5 on a $2000 computer is tough when most of them don’t even want to spend an extra buck on a backlit keyboard, but without the sensors your sensor API is useless.
This morning I decided to use the reduce function in lieu of the missing ?? operator I talked about yesterday. This shaved off another line:
#light
open System.Text.RegularExpressions
let (+..) = Seq.append
let edits1 (w:string) =
seq { for i in { 0 .. w.Length - 1} -> w.Remove(i, 1) } +..
seq { for i in { 0 .. w.Length - 2 } -> w.Remove(i + 1, 1).Insert(i, w.[i + 1].ToString()) } +..
seq { for i in { 0 .. w.Length - 1 } do for c in { 'a' .. 'z' } -> w.Remove(i, 1).Insert (i, c.ToString()) } +..
seq { for i in { 0 .. w.Length } do for c in { 'a' .. 'z' } -> w.Insert (i, c.ToString()) }
let re = Regex ("[a-zA-Z]+", RegexOptions.Compiled)
let words = System.IO.File.ReadAllText "big.txt" |> re.Matches |> Seq.cast |> Seq.map (fun (x:Match) -> x.Value.ToLower())
let wordMap = words |> Seq.count_by id |> Map.of_seq
for arg in Seq.skip 1 Sys.argv do
let cor = [arg |> Seq.singleton; edits1 arg; edits1 arg |> Seq.map_concat edits1]
|> Seq.map (Seq.filter wordMap.ContainsKey) |> Seq.reduce (fun x y -> if Seq.is_empty x then y else x)
printf "%s\n" (if Seq.is_empty cor then arg else cor |> Seq.max_by (fun w -> wordMap.[w]))
Changes from yesterday’s code:
1. Simplified the edits1 function using string Insert and Remove functions. This makes the lines shorter.
2. The cascading cor1 –> cor2 –> cor3 has been replaced by a call to reduce.
3. Replaced the call to Seq.sort_by followed by a call to Seq.hd by Seq.max_by. I’m a moron.
4. The program now attempts to correct all passed arguments, one by one. It bugged me that the code would break for no command-line arguments because it was accessing Sys.argv.[1].
Notes:
1. The edits1 function can definitely be shaved by one line. But limitations of either F# or my knowledge are preventing from doing it in an elegant manner. To be continued…
2. In addition to standard implicit conversions (int to float, etc.), all languages should have an implicit conversion from char to string. I know that the ML language family eschews implicit conversions because of type inference, but it still strikes me as wrong. C++ has shown that it’s possible to have both implicit conversions and type inference (which it does for generic function calls), and though it does one or the other and the Koeing lookup complicates the language definition, it’s still both useful and safe.
[Update: I’ve trimmed the code to 15 lines. See the next post.]
I need to confess a dirty secret of mine: I like language shoot-outs.
I know they are not very scientific. I know they don’t show real-world code. I know that the results depend on the skill of individual contributors. But whether shoot-outs compare speed or brevity I still love them, for a simple reason: They let me see a lot of languages in action at once. If you look at the results as an order of magnitude thing rather than as an exact comparison, they become telling.
Peter Norvig (of the Teach Yourself Programming in Ten Years fame) recently posted a rough explanation of how Google’s spelling corrector works. Since I did something similar ten ago with the Serbian language (it’s depressing how statistics on large input samples can beat experts), I found this interesting.
As it goes on the internets, a lot of people couldn’t resist and rewrote Peter’s sample in their language of choice. While looking at the results it struck me as wrong that F# would do so much worse than C#. So when Ana and the kids went to bed last night I rewrote Lorenzo Stoakes’s C# code in sixteen lines of F#:
#light
open System.Text.RegularExpressions
let (+..) = Seq.append
let edits1 (w:string) =
seq { for i in { 0 .. w.Length - 1} -> w.Substring (0, i) + w.Substring (i + 1) } +..
seq { for i in { 0 .. w.Length - 2 } -> w.Substring(0, i) + w.Substring(i + 1, 1) + w.Substring(i, 1) + w.Substring(i + 2) } +..
seq { for i in { 0 .. w.Length - 1 } do for c in { 'a' .. 'z' } -> w.Substring(0, i) + c.ToString() + w.Substring(i + 1) } +..
seq { for i in { 0 .. w.Length - 1 } do for c in { 'a' .. 'z' } -> w.Substring(0, i) + c.ToString() + w.Substring(i) }
let re = Regex ("[a-zA-Z]+", RegexOptions.Compiled)
let words = System.IO.File.ReadAllText "big.txt" |> re.Matches |> Seq.cast |> Seq.map (fun (x:Match) -> x.Value.ToLower())
let wordMap = words |> Seq.count_by id |> Map.of_seq
let arg = Sys.argv.[1]
let cor1 = arg |> Seq.singleton |> Seq.filter wordMap.ContainsKey
let cor2 = if Seq.is_empty cor1 then edits1 arg |> Seq.filter wordMap.ContainsKey else cor1
let cor3 = if Seq.is_empty cor2 then edits1 arg |> Seq.map_concat edits1 |> Seq.filter wordMap.ContainsKey else cor2
(if Seq.is_empty cor3 then arg else cor3 |> Seq.sort_by (fun w -> - wordMap.[w]) |> Seq.hd) |> printf "%s\n"
A simple run from the command line shows it in action:
PS C:\vsp\spellfs\bin\Debug> ./spellfs droylea doyle
Some notes, in no particular order:
1. I have a nagging feeling that the edits1 function can be rewritten to be much shorter, but I can’t put my finger on it just yet.
2. The cor1->cor2->cor3 sequence makes me go yuck. I need to check whether the F# standard library has an equivalent of the C# ?? operator that would also work on sequences. I could add it here but it would increase the line count. (Lousy measure, I know, but I’m a competitive bastard.)
3. Somebody needs to rewrite this in one line of K and the go “nya nya nya” to all of us.
4. The recipe for winning the brevity shoot-outs is quite simple: a) be functional, b) have a large standard library with a lot of data structures and a shitload of algorithms, and c) have a lot of operators. If you don’t overdo it this is also a good recipe for succeeding in the real world. A lot of places in the code and not performance-critical.
Prompted by a blog post I watched Resident Evil: Degeneration last night. The computer-generated movie is so-so, so despite the current astronomical 9.0 rating I can’t really recommend it. However, what impressed me was the quality of the animation.
In some short scenes you can’t tell it’s computer-generated. I could show you a frame from this movie and a frame from the “normal” Resident Evil movie and you couldn’t tell which one had real humans and which one had computer-generated ones.
The two glaring problems were the human skin and walking. Other than that the level of almost-realistic detail is amazing.
So what happens in ten or twenty years when the computing power goes up between thousand and a million times? (And remember, ray-tracing and movie frames are easy to parallelize so the Moore’s law is going to hold for them.) Add technologies like this or this and it’s clear that both the quality of facial animations and textures will dramatically improve.
Which leads me to the following question: will there still be a need to actors in twenty years? Will the movie studios generate their own actors instead of paying them $20 million a pop even for sequels? They’ll probably still need somebody actor-ish to come up with distinct character personas (think Brad Pitt in 12 Monkeys or Heath Ledger in the Dark Knight), but it can be a fat bald guy instead of a rare individual that has both the good looks and the acting skill
![]() |
I think I figured out a better way to teach kids to ride a bicycle than the way my parents taught me. Here’s the recipe:
1. Take the pedals off the bicycle using a wrench.
2. Make sure the seat height is set so that they can set their feet firmly on the ground, but no lower.
3. Take them to some place that has a gentle slope.
4. Tell them to mount the bicycle and push themselves down the slope with their feet. When they built a bit of speed they should lift their feet and see how long they can cruise without their feet touching the ground.
5. After a while they should be able to navigate 10-20 meters without using their feet for support.
6. Put the pedals back on the bicycle, have them do the same thing as before but by putting their feet on the pedals.
7. Have them to turn the pedals while they are going down the slope. Congrats, your kid just learned to ride a bike.
I’ve used this technique to teach my 7-year old son and my 5-year old daughter how to do it. It took my son about an hour and my daughter a bit more than two hours.
Side note: Training wheels are useless. If they are in the same plane as the bike’s tires, then they will lift the back wheel off the ground if the pavement is not perfectly flat and the kid will be stuck. They make the bicycle unstable in corners because the kid can’t lean. They suck. If your kid is too young to ride without them get them a tricycle.
My colleague, Laza found a nasty leak in WinForms last week. It seems that if you turn on the AllowItemReorder property of the ToolStrip class, this class will stay rooted forever and will never be collected, together with whatever it’s referencing, which is usually the containing window due to event bindings.
The problem is due to an obvious bug in the drag & drop unhooking logic. If you want to work around it either implement your own drag & drop or use stupid reflection tricks to make it happen.
Here’s a test:
Find a couple of good-looking websites. Then take a look at their source code. Can you find one that didn’t mix content and presentation in their (X)HTML?
Fuck. I thought CSS was supposed to fix that, but CSS is so lame that people had to tweak their HTML in order to render properly with CSS. How is the current state of affairs an improvement over old-school HTML that mixed content and presentation?
Last time I’ve seen a fuck-up this bad was with the C++ standard. It seems that the C++ and W3 committees have taken the same path: don’t create a reference implementation, don’t create lots of tests with expected result sets, just write an long document and hope that vendors will get it right.
Oana has a good guide for calling any function from your code from the Visual Studio Immediate Window. As I’m not a fan of unit testing frameworks, I find this approach very appealing.
An added bonus is that I don’t have to install anything that mentions the word "integration" on my laptop. I like integration when done right, but have been burned too many times by software that does it wrong.