<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Carol's 10 Cents</title>
		<description>This blog was re-released before it was ready</description>
		<link>http://carol-nichols.com</link>
		<atom:link href="http://carol-nichols.com/feed.xml" rel="self" type="application/rss+xml" />
		
			<item>
				<title>Rust Profiling with DTrace and FlameGraph on OSX</title>
				<description>&lt;p&gt;This is an alternative to my post &lt;a href=&quot;/2015/12/09/rust-profiling-on-osx-cpu-time/&quot;&gt;Rust Profiling with Instruments and
FlameGraph on OSX: CPU/Time&lt;/a&gt;,
since XCode disabled exporting of the Time/CPU data from Instruments. Many
thanks to &lt;a href=&quot;http://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html&quot;&gt;this post of BrendanGregg’s
&lt;/a&gt; that has
examples of using DTrace to get the data to feed into &lt;a href=&quot;https://github.com/brendangregg/FlameGraph&quot;&gt;his FlameGraph
generation scripts&lt;/a&gt;, and
&lt;a href=&quot;http://stackoverflow.com/a/43491361/51683&quot;&gt;GolDDranks on Stack Overflow&lt;/a&gt; who
figured out how to get OSX’s DTrace to find Rust debugging symbols – I didn’t
have the same problem, I’m not sure why, but they inspired me to try DTrace
instead of Instruments and write an update blog post!&lt;/p&gt;

&lt;p&gt;So here’s the new workflow that worked for me!&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;These are the same.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Turn on debug symbols in your release profile in Cargo.toml
    &lt;ul&gt;
      &lt;li&gt;This lets the profiling tools match up to your source code.&lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;As documented on &lt;a href=&quot;http://doc.crates.io/manifest.html#the-[profile.*]-sections&quot;&gt;crates.io&lt;/a&gt;, this is done by adding this to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cargo.toml&lt;/code&gt;:&lt;/p&gt;

        &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[profile.release]
debug = true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Build with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo build --release&lt;/code&gt; then run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./target/release/[binary]&lt;/code&gt;; or use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo run --release&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;Forgetting to build in release mode (which turns on optimizations) is the #1 cause of unexpected Rust slowness.&lt;/li&gt;
      &lt;li&gt;By default, cargo builds in debug mode, which is faster to compile but runs more slowly.&lt;/li&gt;
      &lt;li&gt;We want to profile the optimized code.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;dtrace&quot;&gt;DTrace&lt;/h2&gt;

&lt;p&gt;OSX (yes yes I know it’s macOS now but it’ll always be OSX to me) comes with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace&lt;/code&gt;! I’m on Sierra, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace -V&lt;/code&gt; says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace: Sun D 1.13&lt;/code&gt; for me.&lt;/p&gt;

&lt;p&gt;TL;DR: the command I ran to profile my program named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zopfli&lt;/code&gt; was:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo dtrace -c './zopfli large-file' -o out.stacks -n 'profile-997 /execname == &quot;zopfli&quot;/ { @[ustack(100)] = count(); }'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here’s the explanation of all the pieces:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;sudo&lt;/strong&gt;: Because of Reasons™, you have to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace&lt;/code&gt; as root on OSX.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-c&lt;/code&gt;&lt;/strong&gt;: A lot of the tutorials and blog posts I’m looking at use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace&lt;/code&gt; by running the program, then finding the PID of the running process, then passing the PID as an argument to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace&lt;/code&gt;– I don’t want to do all that, I want to have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace&lt;/code&gt; run my program and watch just what it’s running. Luckily, that’s what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-c&lt;/code&gt; does! I have a bunch of symlinks set up because Other Reasons™, so the command I use to run my Rust program is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./zopfli large-file&lt;/code&gt;. So that’s what I’m doing with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-c './zopfli large-file'&lt;/code&gt; part, replace my command with the one you want to run under dtrace.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-o&lt;/code&gt;&lt;/strong&gt;: This is the output filename that we’ll be passing to the flamegraphs scripts in the next section.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-n&lt;/code&gt;&lt;/strong&gt;: This argument specifies the probe names that you want to enable. Breaking this down:&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;profile-997&lt;/code&gt;: interrupt the program 997 times per second. See &lt;a href=&quot;http://dtrace.org/guide/chp-profile.html&quot;&gt;the profile provider in the DTrace book&lt;/a&gt; for more info.&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/execname == &quot;zopfli&quot;/&lt;/code&gt; is a predicate that will fire if the name of the current process’s executable file is equal to the string we’ve specified; you’ll want to change this to be your program. I’m not sure why this is needed exactly since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace&lt;/code&gt; should know the executable from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-c&lt;/code&gt; argument, but I couldn’t figure out the right syntax to get the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-n&lt;/code&gt; argument to work without this.&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{ @[ustack(100)] = count(); }&lt;/code&gt; is the code to run if the predicate is true. This code stores an aggregate (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@[]&lt;/code&gt;) count (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count()&lt;/code&gt;) of the number of times that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dtrace&lt;/code&gt; sees a particular user stack trace (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ustack&lt;/code&gt;) 100 frames deep.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;flamegraph&quot;&gt;FlameGraph&lt;/h2&gt;

&lt;p&gt;Now that you have the output from dtrace in a file, the flamegraph steps are
similar. Once you have, the &lt;a href=&quot;https://github.com/brendangregg/FlameGraph&quot;&gt;FlameGraph
code&lt;/a&gt;, and assuming you gave &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-o&lt;/code&gt;
the argument &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;out.stacks&lt;/code&gt;, run:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./stackcollapse.pl out.stacks | ./flamegraph.pl &amp;gt; pretty-graph.svg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Tada!!! You have pretty flame graph SVGs again!!!&lt;/p&gt;
</description>
				<pubDate>Thu, 20 Apr 2017 00:00:00 +0000</pubDate>
				<link>http://carol-nichols.com/2017/04/20/rust-profiling-with-dtrace-on-osx/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2017/04/20/rust-profiling-with-dtrace-on-osx/</guid>
			</item>
		
			<item>
				<title>Rust Profiling with Instruments and FlameGraph on OSX: CPU/Time</title>
				<description>&lt;h2 id=&quot;update-april-2017&quot;&gt;UPDATE APRIL 2017&lt;/h2&gt;

&lt;p&gt;I just tried to follow my own directions and found out that Instruments no longer lets you export CSV data that the Time Profiler collects (I’m currently using Instruments Version 8.3 (8E162)). I’m leaving the rest of the post as-is in case it’s useful to anyone still on an older version of Instruments, but any point after the export won’t work, so no pretty flame graphs anymore :( There’s still analysis of performance you can do within Instruments.&lt;/p&gt;

&lt;h2 id=&quot;original-post&quot;&gt;Original Post&lt;/h2&gt;

&lt;p&gt;I recently decided I wanted to figure out a way to measure the performance of the Rust code I’ve been writing and see pretty graphs of what parts of the code were slower. There are a bunch of different ways to do this, and I was going to cover more than one of them, but &lt;a href=&quot;https://youtu.be/84Ud3V9NPw8?t=42s&quot;&gt;I am le tired&lt;/a&gt; so I decided to post this after writing only about Instruments + FlameGraph.&lt;/p&gt;

&lt;p&gt;My somewhat-arbitrary constraints used in picking this method of profiling and graphing:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Profile Rust programs’ time (I’d like to profile memory usage too, but that’ll be in another post)&lt;/li&gt;
  &lt;li&gt;In a way that produces graphs&lt;/li&gt;
  &lt;li&gt;On OSX (Linux has other, some may say better, options, but for getting a first look at the performance of my code at all, I don’t feel like booting a VM)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If these constraints are the same as yours, great! Read on! However, there are many more tools that didn’t meet these constraints (or that I didn’t find), so I encourage you to do research specific to your situation as well.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;

&lt;p&gt;For any profiling of Rust code on OSX, it’s recommended to do the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Build with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo build --release&lt;/code&gt; then run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./target/release/[binary]&lt;/code&gt;; or use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo run --release&lt;/code&gt;
    &lt;ul&gt;
      &lt;li&gt;Forgetting to build in release mode (which turns on optimizations) is the #1 cause of unexpected Rust slowness.&lt;/li&gt;
      &lt;li&gt;By default, cargo builds in debug mode, which is faster to compile but runs more slowly.&lt;/li&gt;
      &lt;li&gt;We want to profile the optimized code.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Turn on debug symbols in your release profile in Cargo.toml
    &lt;ul&gt;
      &lt;li&gt;This lets the profiling tools match up to your source code.&lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;As documented on &lt;a href=&quot;http://doc.crates.io/manifest.html#the-[profile.*]-sections&quot;&gt;crates.io&lt;/a&gt;, this is done by adding this to your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cargo.toml&lt;/code&gt;:&lt;/p&gt;

        &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[profile.release]
debug = true
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Use OSX’s system allocator instead of jemalloc
    &lt;ul&gt;
      &lt;li&gt;This requires using nightly Rust.&lt;/li&gt;
      &lt;li&gt;I’m not going to pretend to understand this, but it seems like &lt;a href=&quot;https://github.com/rust-lang/rust/issues/28224&quot;&gt;valgrind might not report allocations correctly with jemalloc&lt;/a&gt;? It didn’t seem to hurt anything, but I’m not sure how important this is.&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;https://www.reddit.com/r/rust/comments/3p0ljg/forcing_rustc_to_use_the_system_allocator/&quot;&gt;More discussion on /r/rust about the impact of this on benchmarks&lt;/a&gt;.&lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;As documented in &lt;a href=&quot;http://doc.rust-lang.org/nightly/book/custom-allocators.html&quot;&gt;the book&lt;/a&gt;, this is done by adding this code to your crate:&lt;/p&gt;

        &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#![feature(alloc_system)]
extern crate alloc_system;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;        &lt;/div&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;protip-on-inlining&quot;&gt;Protip on inlining&lt;/h2&gt;

&lt;p&gt;I had to consult with &lt;a href=&quot;http://jakegoulding.com/&quot;&gt;Jake Goulding&lt;/a&gt; on this one– I was profiling some code that had a function calling another function, and only the inner function was showing up. I couldn’t figure out why. Jake quickly looked at what I was doing and guessed correctly that the compiler was inlining the contents of the outer function as an optimization, so it effectively didn’t exist anymore.&lt;/p&gt;

&lt;p&gt;Jake taught me about adding the annotation &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#[inline(never)]&lt;/code&gt; before the outer function, which will tell the compiler to not inline it so that I could see the profiling for it.&lt;/p&gt;

&lt;h2 id=&quot;instruments&quot;&gt;Instruments&lt;/h2&gt;

&lt;p&gt;Instruments is a part of XCode. You’ve already downloaded 2 gigs of XCode, right?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Open XCode. Don’t pick anything on the splash screen.&lt;/li&gt;
  &lt;li&gt;From the XCode menu, choose “Open Developer Tool” -&amp;gt; “Instruments”.&lt;/li&gt;
  &lt;li&gt;Choose a blank profiling template. (Or try others! I haven’t explored all the options yet)&lt;/li&gt;
  &lt;li&gt;Click the “+” button in the upper right. Choose “Time Profiler” and drag it to the section in the upper left.&lt;/li&gt;
  &lt;li&gt;In the upper left, after your computer name (yes, my computer’s name is the tableflip emoji), click on where it says “All processes” and click on “Choose Target” in order to choose just your executable instead of all processes. For some reason, populating the list of executables to choose from takes a long time, so don’t be alarmed.
&lt;a href=&quot;/assets/img/instruments-screenshot.png&quot;&gt;&lt;img src=&quot;/assets/img/instruments-screenshot.png&quot; width=&quot;560&quot; alt=&quot;Screenshot of Instruments showing the location of the Time Profiler, where you drop it, and where you choose your executable&quot; /&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;If needed, you can type environment variables and arguments into the “Choose Target” window. Here’s an example where I added some arguments:
&lt;a href=&quot;/assets/img/instruments-arguments.png&quot;&gt;&lt;img src=&quot;/assets/img/instruments-arguments.png&quot; width=&quot;560&quot; alt=&quot;Screenshot of Choose Target window with some arguments specified&quot; /&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;By default, the Time Profiler takes a sample every 1 ms. Because I was using a toy example, I found that I wasn’t getting interesting results because my code wasn’t being sampled very often, so I changed the sample interval from 1 ms to 40 microseconds in the lower right.
&lt;a href=&quot;/assets/img/instruments-sampling-interval.png&quot;&gt;&lt;img src=&quot;/assets/img/instruments-sampling-interval.png&quot; alt=&quot;Zoomed in screenshot of the sampling interval set to 40 microseconds&quot; style=&quot;width: 340px&quot; /&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Click the record button in the upper left and wait for it to run your code. You may need to give administrator privileges to Instruments. You’ll know it’s done when the button changes back to a record button after being a stop button.&lt;/li&gt;
  &lt;li&gt;You do get a little bit of a graph across the top– From the View menu, choosing “Snap Track to Fit” makes it a little easier to see. It still doesn’t show very specific information though.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;flamegraph&quot;&gt;Flamegraph&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://www.brendangregg.com/flamegraphs.html&quot;&gt;Flamegraph&lt;/a&gt; is an awesome collection of perl scripts that can take the output of a few different profiling tools and produce a lovely SVG.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;To get the data out of Instruments for Flamegraph to use, I followed &lt;a href=&quot;https://schani.wordpress.com/2012/11/16/flame-graphs-for-instruments/&quot;&gt;these instructions&lt;/a&gt; which say to:
    &lt;ul&gt;
      &lt;li&gt;Expand all the nodes in the call tree: Option-click on all the roots to toggle them open&lt;/li&gt;
      &lt;li&gt;From the Instrument menu, choose “Export track” and save the CSV.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Download the &lt;a href=&quot;https://github.com/brendangregg/FlameGraph&quot;&gt;FlameGraph code&lt;/a&gt; and run:&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ./stackcollapse-instruments.pl export.csv | ./flamegraph.pl &amp;gt; pretty-graph.svg
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
  &lt;li&gt;Open the resulting svg in your browser. You now have a pretty graph!!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href=&quot;/assets/img/instruments-flamegraph.svg&quot;&gt;Here’s an SVG I made&lt;/a&gt; from &lt;a href=&quot;https://github.com/carols10cents/rust-profiling-example&quot;&gt;the toy code in this repo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy profiling!!!&lt;/p&gt;
</description>
				<pubDate>Wed, 09 Dec 2015 20:47:18 +0000</pubDate>
				<link>http://carol-nichols.com/2015/12/09/rust-profiling-on-osx-cpu-time/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2015/12/09/rust-profiling-on-osx-cpu-time/</guid>
			</item>
		
			<item>
				<title>Ruby Occurrence Counting</title>
				<description>&lt;p&gt;Last night, I was coding, as one does, and I wanted something that I thought FOR SURE there was a method in Ruby’s Enumerable module for already, because Enumerable is awesome and The Best Module Ever. But alas, there is not. My problem was:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Given an array like:
arr = [&quot;a&quot;, &quot;b&quot;, &quot;a&quot;, &quot;c&quot;, &quot;c&quot;]

You want a count of the occurrences of each item in the
array, as a hash with the unique items in the array as
the keys, and the number of occurrences as the values.

The desired result in this example is:
{&quot;a&quot; =&amp;gt; 2, &quot;b&quot; =&amp;gt; 1, &quot;c&quot; =&amp;gt; 2}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I read through all of enumerable, then tried my hand at a solution, and it just wasn’t pretty or satisfying. This is unusual for Ruby! So what did I do? I asked my coworkers and I asked twitter.&lt;/p&gt;

&lt;p&gt;Between all of us, we came up with 12 significantly different solutions:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;arr.uniq.map { |x| [x, arr.count(x)] }.to_h
h = Hash.new(0); arr.each { |l| h[l] += 1 }; h
arr.reduce({}) { |m, a| m[a] = m[a].to_i + 1; m }
arr.inject(Hash.new(0)) { |h, i| h[i] += 1; h }
arr.sort.chunk { |ex| ex }.map { |k, v| [k, v.length] }.to_h
arr.reduce({}) { |ret, val| ret[val] = (ret[val] || 0) + 1; ret }
arr.each_with_object(Hash.new(0)) { |word, counts| counts[word] += 1 }
arr.each_with_object({}) { |item, memo| memo[item] ||= 0; memo[item] += 1 }
arr.map { |x| { x =&amp;gt; 1 } }.inject { |a, b| a.merge(b) { |k, x, y| x + y } }
arr.group_by { |x| x }.map { |element, matches| [ element, matches.length ] }.to_h
Hash[arr.group_by(&amp;amp;:itself).map {|k,v| [k, v.size] }] # Must also upgrade to Ruby 2.2 or Rails 4
arr.sort.chunk(&amp;amp;:itself).map {|v, vs| [v, vs.count]}.to_h # Must also upgrade to Ruby 2.2 or Rails 4
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I love the variety! I love that there’s not a clear right answer! I love that the solution is evolving over newer versions of Ruby (that I’m not on yet)!&lt;/p&gt;

&lt;p&gt;And as if &lt;a href=&quot;https://twitter.com/BillLaboon/status/629462532734996480&quot;&gt;this wasn’t Tom Sawyerish enough&lt;/a&gt;, I decided we then needed to have a vote. As of this writing, with the polls open about 24 hours, I’ve had 41 responses to these 3 questions:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/like.png&quot; alt=&quot;Results of which do you like best, winners are arr.sort.chunk(&amp;amp;:itself).map {|v, vs| [v, vs.count]}.to_h with 10 votes (24.4%) then arr.uniq.map { |x| [x, arr.count(x)] }.to_h with 9 votes (22%)&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/fastest.png&quot; alt=&quot;Results of which do you think is fastest, winners are h = Hash.new(0); arr.each { |l| h[l] += 1 }; h and arr.sort.chunk(&amp;amp;:itself).map {|v, vs| [v, vs.count]}.to_h, both with 11 votes (26.8%)&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/snark.png&quot; alt=&quot;Results of which snarky answer did you really want to give, winner was What do you mean, you're not on Ruby 2.2 or Rails 4?	with 9 votes (23.1%)&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So then, of course, I had to benchmark the results. And I don’t benchmark things very often, so I don’t know how to do it off the top of my head, but luckily I had &lt;a href=&quot;https://github.com/rails/rails/pull/21057&quot;&gt;Schneems’ Rails pull request open in another tab&lt;/a&gt; where &lt;a href=&quot;https://github.com/rails/rails/pull/21057/files#r35902468&quot;&gt;someone benchmarked parts of the code&lt;/a&gt; using &lt;a href=&quot;https://github.com/evanphx/benchmark-ips&quot;&gt;evanphx’s benchmark-ips gem&lt;/a&gt;. And a number of things appealed to me in the gem’s readme, namely “No more guessing at random iteration counts!” If there’s anything I know, it’s that I don’t know how to pick iteration counts. &lt;a href=&quot;https://github.com/carols10cents/occurrence-counting/blob/master/bench.rb&quot;&gt;The code I used is on GitHub&lt;/a&gt;, and I’m sure I messed something up– please send me pull requests if there are improvements to be made!&lt;/p&gt;

&lt;p&gt;So I put all the solutions into a Benchmark.ips block, using powers of 10 for the length of the array we’d be counting (this is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; in the Big-O analysis). I used a sampling of lowercase letters in the English alphabet for the values. Benchmarking arrays that are all the same value or all different values instead of a random distribution of values is left as an exercise to the reader.&lt;/p&gt;

&lt;h2 id=&quot;tldr-youre-not-gonna-like-the-results&quot;&gt;TL;DR You’re Not Gonna Like The Results&lt;/h2&gt;

&lt;p&gt;The absolute winner is:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Hash[arr.group_by(&amp;amp;:itself).map {|k,v| [k, v.size] }]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are two things that I don’t think yinz are going to like about these results:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;In my opinion, the differences between the fastest 10 solutions are insignificant&lt;/strong&gt;. So you’d be fine going with any of those; it’s purely your preference and benchmarking does not give us one unequivocally “best” answer.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The shortest example that 22% of people liked best is significantly slower&lt;/strong&gt;. Yup, that one looked most elegant to me too! But 7.5x slower isn’t anything to sneeze at.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/carols10cents/occurrence-counting/blob/master/results.txt&quot;&gt;Here’s the text output of the script I ran&lt;/a&gt;. It cuts off because I let it run overnight and it still wasn’t done in the morning, but we did get full runs up to n = 1,000,000.&lt;/p&gt;

&lt;p&gt;The last section in each run shows the comparison of the candidates to each other; here’s that section for 1,000,000 items:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Comparison:
     group-by-itself:        6.7 i/s
            group-by:        6.5 i/s - 1.03x slower
           hash-each:        5.1 i/s - 1.29x slower
              inject:        4.6 i/s - 1.44x slower
         reduce-or-0:        4.4 i/s - 1.52x slower
         reduce-to-i:        4.2 i/s - 1.60x slower
  each-with-hash-new:        4.1 i/s - 1.63x slower
               chunk:        4.0 i/s - 1.67x slower
     chunk-by-itself:        3.9 i/s - 1.71x slower
each-with-empty-hash:        3.4 i/s - 1.96x slower
          uniq-count:        0.9 i/s - 7.49x slower
          map-inject:        0.1 i/s - 90.93x slower
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You’ll have to &lt;a href=&quot;https://github.com/carols10cents/occurrence-counting/blob/master/bench.rb#L7-L18&quot;&gt;look at my code&lt;/a&gt; to see which names correspond to each solution.&lt;/p&gt;

&lt;p&gt;In order to get the nice, Big-O like graphs that I remember learning all about in college, I took the iterations per second from each N’s comparison section of the results, took the inverse so that I had seconds per iteration, &lt;a href=&quot;https://docs.google.com/spreadsheets/d/10n6E5N0mD_DauGer9yFwIskzgdP4NhomdtDSj6EpsCI/edit?usp=sharing&quot;&gt;and compiled them in a spreadsheet&lt;/a&gt; then made a graph of the data on a logarithmic scale:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/big-o-graph.png&quot; alt=&quot;Big-O graph showing that most of the solutions are the same, the uniq-count solution is significantly slower, and the map-inject solution is exponential&quot; /&gt;&lt;/p&gt;

&lt;p&gt;UPDATE 2015-08-08: &lt;a href=&quot;https://twitter.com/Gankro&quot;&gt;Gankro&lt;/a&gt; kindly mentioned that I didn’t put the y-axis in logarithmic scale originally, here’s the fixed version. This lets you see more differences between the 10 similarly-performing solutions:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/big-o-graph-both-log.png&quot; alt=&quot;The same data as the previous graph but with the y-axis using a logarithmic scale. There is now some visual difference between some of the 10 fastest solutions.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So there you go! Go forth and benchmark your own code!&lt;/p&gt;
</description>
				<pubDate>Fri, 07 Aug 2015 15:09:18 +0000</pubDate>
				<link>http://carol-nichols.com/2015/08/07/ruby-occurrence-couting/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2015/08/07/ruby-occurrence-couting/</guid>
			</item>
		
			<item>
				<title>Rustcamp Talk Notes: Navigating the Open Seas</title>
				<description>&lt;p&gt;I gave a talk called “Navigating the Open Seas” at &lt;a href=&quot;http://rustcamp.com&quot;&gt;Rustcamp&lt;/a&gt; today– loosely
based on &lt;a href=&quot;/2015/05/10/rustc-discovery/&quot;&gt;my last blog post&lt;/a&gt; :) Here are links to everything I mentioned in the talk in case you’d
like to learn more about these parts of Rust’s history!&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Introduction&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://github.com/rust-lang/rfcs/pull/134&quot;&gt;RFC 134&lt;/a&gt; that proposed removing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;'&lt;/code&gt; sigil from the lifetime syntax. I called out &lt;a href=&quot;https://github.com/rust-lang/rfcs/pull/134#issuecomment-47054728&quot;&gt;this comment of Patrick’s in particular&lt;/a&gt; to illustrate the value of Rust’s open development to future language designers who will be in this hypothetical state he proposed for every decision they’ll be making.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Exploration #1 - into the rust-dev mailing list&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://mail.mozilla.org/pipermail/rust-dev/&quot;&gt;The rust-dev mailing list&lt;/a&gt;, active from July 2010-Jan 2015. Replaced by the &lt;a href=&quot;https://users.rust-lang.org/&quot;&gt;users&lt;/a&gt; and &lt;a href=&quot;https://internals.rust-lang.org/&quot;&gt;internals&lt;/a&gt; discourse installations.&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;http://rust-dev.1092773.n5.nabble.com/&quot;&gt;The rust-dev mailing list on nabble&lt;/a&gt;, which I prefer because of the search available there.&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;http://rust-dev.1092773.n5.nabble.com/Renaming-quot-tag-quot-and-quot-log-err-quot-td991.html&quot;&gt;Oct 28, 2011 Renaming “tag” and “log_err” mailing list thread&lt;/a&gt; - the 21 message long mailing list thread that announces that the std library now has print/println, wonders if Rust should have a prelude, and discusses the renaming of “tag”, which is what ends up as “enum” today.&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Exploration #2 - where did the lifetime elision rules come from?&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://doc.rust-lang.org/stable/book/lifetimes.html#lifetime-elision&quot;&gt;The lifetime elision rules in TRPL&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/blob/master/src/doc/trpl/lifetimes.md&quot;&gt;The markdown source of the lifetime page in the book&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/commits/master/src/doc/trpl/lifetimes.md&quot;&gt;Github’s rendering of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log src/doc/trpl/lifetimes.md&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/blame/master/src/doc/trpl/lifetimes.md&quot;&gt;Github’s rendering of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git blame src/doc/trpl/lifetimes.md&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/blame/master/src/doc/trpl/lifetimes.md#L269&quot;&gt;Github’s rendering of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git blame -L 269 src/doc/trpl/lifetimes.md&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/commit/ab3cb8c5ae3b9fe86faa1dfa9402145788a005f5&quot;&gt;Github’s rendering of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git show ab3cb8c5&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/commit/ab3cb8c5ae3b9fe86faa1dfa9402145788a005f5&quot;&gt;Github’s rendering of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git blame ab3cb8c5~ src/doc/trpl/ownership.md&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/commit/a56e7aee81733485d6edd415ab383347232e3c36&quot;&gt;Github’s rendering of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git show a56e7aee&lt;/code&gt;&lt;/a&gt;, where the lifetime elision section was added to the docs&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/issues/19662&quot;&gt;Issue #19662 for adding documentation about all forms of lifetime elision&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rfcs/blob/master/text/0141-lifetime-elision.md#the-rules&quot;&gt;The rules as proposed in RFC 141&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rfcs/pull/141&quot;&gt;RFC 141 PR discussion&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Exploration #3 - What happened to iface?&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;http://rust-dev.1092773.n5.nabble.com/RFC-Removing-as-a-WIP-tp1910.html&quot;&gt;A sample message on the rust-dev mailing list that mentions &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iface&lt;/code&gt;, that doesn’t exist today&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust/commit/eb834fdb&quot;&gt;Commit eb834fdb&lt;/a&gt; that stopped parsing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iface&lt;/code&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://air.mozilla.org/rust-typeclasses/&quot;&gt;Lindsey Kuper’s intern presentation video&lt;/a&gt; that talked about her work unifying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;trait&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;iface&lt;/code&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust-wiki-backup&quot;&gt;The rust-wiki-backup repository&lt;/a&gt;&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
        &lt;p&gt;&lt;a href=&quot;https://github.com/rust-lang/rust-wiki-backup/blob/95602c07a543404117685439c58586af756c918f/Proposal-for-unifying-traits-and-interfaces.md&quot;&gt;The proposal for unifying traits and interfaces&lt;/a&gt; in the wiki history&lt;/p&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Conclusion&lt;/p&gt;

    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;https://web.archive.org/web/20130530235831/http://irclog.gr/#browse/irc.mozilla.org/rust&quot;&gt;Where the IRC logs before April 2013 would be if irclog.gr hadn’t loaded the data with ajax&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

</description>
				<pubDate>Sat, 01 Aug 2015 15:09:18 +0000</pubDate>
				<link>http://carol-nichols.com/2015/08/01/rustcamp-talk-notes/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2015/08/01/rustcamp-talk-notes/</guid>
			</item>
		
			<item>
				<title>Rust Discovery, or: How I Figure Things Out</title>
				<description>&lt;p&gt;I’m by no means a prolific contributor to &lt;a href=&quot;https://github.com/rust-lang/rust&quot;&gt;the Rust language&lt;/a&gt; (yet!), but I have submitted a few patches. One thing that I’m appreciating as I work on these is that it’s possible to figure out why something did (or did not) change since the development process of Rust is happening in the open. Using git, GitHub, &lt;a href=&quot;https://github.com/rust-lang/rfcs&quot;&gt;the RFCs repo&lt;/a&gt;, and documentation, I have felt like I could figure anything out with a little digging. It’s so much more satisfying than languages that are closed source, haven’t documented the decisions made, or have the information more distributed across multiple forums, bug trackers, or mailing lists so that it’s harder to get the full picture.&lt;/p&gt;

&lt;p&gt;In programming, I strongly believe that it’s not important to know all the answers off the top of your head, but it is VERY important to know how to figure out the answers you need, and you can make meaningful contributions if you have the latter but most people don’t even try because they feel like they’re supposed to have the former.&lt;/p&gt;

&lt;p&gt;I’ve decided to write down my thought processes and the techniques I’ve used while working on a change in order to give some ideas to people who might be feeling overwhelmed or stuck, and to demonstrate that, even though I know very little about how the Rust compiler works, I can help. I’d love to hear about more places/ways I could be looking or about interesting information you’ve found out using these sorts of techniques!&lt;/p&gt;

&lt;p&gt;So! Lately, I’ve been working on &lt;a href=&quot;https://github.com/rust-lang/rust/blob/master/src/doc/grammar.md&quot;&gt;the grammar documentation&lt;/a&gt;, trying to make sure the patterns that explain what is valid in the language are up to date with the code that parses the language since the language is settling down with the 1.0 release impending. The grammar documentation is useful to &lt;a href=&quot;https://github.com/codemirror/CodeMirror/issues/2937&quot;&gt;people writing syntax highlighters&lt;/a&gt;, among other things. In this post, I am assuming a general familiarity with &lt;a href=&quot;https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form&quot;&gt;EBNF&lt;/a&gt; and &lt;a href=&quot;http://doc.rust-lang.org/nightly/grammar.html#notation&quot;&gt;the variant this Rust document is choosing to use&lt;/a&gt;, but I promise they’re essentially a set of rules of substitutions that define what is valid in a language, nothing more. You can totally learn the concepts if you haven’t heard of EBNF before, especially if you’re familiar with regular expressions.&lt;/p&gt;

&lt;p&gt;Today, I was working on the Items section and got to the Modules &amp;gt; View items &amp;gt; Use declarations subsection, which &lt;a href=&quot;https://github.com/rust-lang/rust/blob/750f2c63f2737305d33288303cdecb7a470a7922/src/doc/grammar.md#use-declarations&quot;&gt;at this point in time reads&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;use_decl : &quot;pub&quot; ? &quot;use&quot; [ path &quot;as&quot; ident
                          | path_glob ] ;

path_glob : ident [ &quot;::&quot; [ path_glob
                          | '*' ] ] ?
          | '{' path_item [ ',' path_item ] * '}' ;

path_item : ident | &quot;mod&quot; ;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After reading this closely, I noticed what seemed to be like a few issues:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt; isn’t defined anywhere (not even in another section in this document)&lt;/li&gt;
  &lt;li&gt;
    &lt;s&gt;`use foo{bar}` (without the `::` between `foo` and `{`) would be valid and I don't think it is&lt;/s&gt;
    &lt;p&gt;I was totally wrong that the definition allowed this, see the “IN WHICH I REALIZE I’M TOTALLY WRONG” section. I’m leaving this in rather than correcting it because I think it’s important to demonstrate how research can change your understanding of something.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use foo::{mod}&lt;/code&gt; (using the keyword &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mod&lt;/code&gt;) would be valid and I don’t think it is; I don’t remember seeing this in code I’ve read and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git grep -h use | grep mod&lt;/code&gt; in the rust repo doesn’t turn up any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use&lt;/code&gt; declarations that have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mod&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;s&gt;The second problem seem to be because of ambiguous grouping. These are valid if the grouping is:&lt;/s&gt;
&lt;p&gt;THIS IS TOTALLY WRONG SEE BELOW&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ident [ &quot;::&quot; [ path_glob | '*' ] ] ?
|
'{' path_item [ ',' path_item ] * '}'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But not if the grouping is:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ident
  [ &quot;::&quot; [ path_glob | '*' ] ] ?
  |
  '{' path_item [ ',' path_item ] * '}'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;(whitespace altered to make the groups a bit clearer)&lt;/p&gt;

&lt;p&gt;So now I want to figure out if I’m correct that these are issues (SPOILER I’M NOT SEE BELOW), and then figure out what these definitions should be.&lt;/p&gt;

&lt;h2 id=&quot;are-these-issues&quot;&gt;Are these issues?&lt;/h2&gt;

&lt;p&gt;In trying to figure out if things I find in code are, in fact, problems, I’ve found two questions to be useful to ask:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;How did this get to be the way it is today? (history)&lt;/li&gt;
  &lt;li&gt;Does it match the current state of the rest of the code? (present)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s start with the history! Git is really good at keeping track of the history of changes. There are a few tools git provides that could be useful in this case:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;I could use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log src/doc/grammar.md&lt;/code&gt; to read the whole history of this file&lt;/li&gt;
  &lt;li&gt;I could use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git blame src/doc/grammar.md&lt;/code&gt; to see which commit last changed each line in the file&lt;/li&gt;
  &lt;li&gt;I could use the &lt;a href=&quot;https://gitfu.wordpress.com/2008/06/03/the-pickaxe-finding-changes-was-never-easier/&quot;&gt;git “pickaxe” search&lt;/a&gt; to look for commits that added or removed occurrences of particular text, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -Gpath_glob&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Taking a quick look at the log of the file, and especially with the stats that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log --stat src/doc/grammar.md&lt;/code&gt; shows, it looks like a lot of the grammar was added to this file in the first commit to it, &lt;a href=&quot;https://github.com/icorderi/rust/commit/ffc5f1ccd8b0c7ca07414c324729ecac97f47e6a&quot;&gt;ffc5f1c&lt;/a&gt;. A GitHub trick I like to use is searching for the commit hash to find the pull request this commit was part of:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/github-commit-search.png&quot; alt=&quot;screenshot showing that if you do a pull request search for a git SHA, you'll get the pull request that commit was part of&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This is awesome, because &lt;a href=&quot;https://github.com/rust-lang/rust/pull/19353&quot;&gt;in the PR description&lt;/a&gt; it says “The current state of the PR contains all the grammars that were available in reference.md and nothing else.” So this content that looks brand new actually has more history as part of &lt;a href=&quot;https://github.com/rust-lang/rust/blob/master/src/doc/reference.md&quot;&gt;reference.md&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;So if I do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log src/doc/reference.md&lt;/code&gt;, I get… a lot of commits. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log --format=oneline src/doc/reference.md | wc -l&lt;/code&gt; =&amp;gt; 356 commits as of today, to be exact. I don’t know about you, but I’m not about to look through all of those individually. I did look at &lt;a href=&quot;https://github.com/steveklabnik/rust/commit/47682f96de1da3bb0986e44e529cc51f24549c86&quot;&gt;the first commit&lt;/a&gt;, though, which shows that THIS file actually used to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/doc/rust.md&lt;/code&gt;, and that the section I’m interested in had the same issues at that point.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/img/we-need-to-go-deeper.jpg&quot; alt=&quot;screenshot of a paint program that has opened a screenshot of the screenshot of the screenshot to infinity, with the caption WE NEED TO GO DEEPER&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If I look at &lt;a href=&quot;https://github.com/alexcrichton/rust/commit/864b434bfa3fd5b3ea9e38958652ed1abdc24f1d&quot;&gt;the first commit to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/doc/rust.md&lt;/code&gt;&lt;/a&gt;, the section I’m interested in is different at this point!&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;use_decl : &quot;pub&quot; ? &quot;use&quot; ident [ '=' path
                          | &quot;::&quot; path_glob ] ;

path_glob : ident [ &quot;::&quot; path_glob ] ?
          | '*'
          | '{' ident [ ',' ident ] * '}'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Taking a search through &lt;a href=&quot;https://raw.githubusercontent.com/alexcrichton/rust/864b434bfa3fd5b3ea9e38958652ed1abdc24f1d/src/doc/rust.md&quot;&gt;the whole document at that version&lt;/a&gt;, it seems like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt; still isn’t defined anywhere, so this version still has issue #1. But &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use foo{bar}&lt;/code&gt; would NOT be valid here because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_decl&lt;/code&gt; has a requirement of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;::&quot;&lt;/code&gt; before the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path_glob&lt;/code&gt;. Also, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path_item&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;mod&quot;&lt;/code&gt; does not appear. So I’ve got a good range to know when some of the changes I’m interested in have been made!&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log --format=oneline src/doc/rust.md | wc -l&lt;/code&gt; shows 179 changes, more manageable but I still don’t want to look through all of them manually. At this point, I’m going to pull out git’s pickaxe search. Usually, you’ll see the concept of a pickaxe search being done using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -Ssomestring&lt;/code&gt;, which will show commits where that string was added or removed from a file, but I more often like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -Gsomestring&lt;/code&gt;, which also shows when lines in a file containing that string appeared in a diff, that is, those lines were changed.&lt;/p&gt;

&lt;p&gt;If I do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -Gpath_glob src/doc/rust.md&lt;/code&gt;, I get the two commits that moved this content around AND &lt;a href=&quot;https://github.com/pczarn/rust/commit/6674913c022f54cef5545e2e4956bc6f6c8528a1&quot;&gt;a commit&lt;/a&gt; between them that says “Correct EBNF grammar in the manual”, “The grammar for use declarations was outdated.” This looks relevant to my interests! If I do a GitHub search for the pull requests, I can see &lt;a href=&quot;https://github.com/rust-lang/rust/pull/14277#discussion_r12782015&quot;&gt;some discussion from a year ago&lt;/a&gt; about WHY these changes are being made. Most interesting to me are the examples that show with these changes, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use {many, idents};&lt;/code&gt; would be correctly valid and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use *;&lt;/code&gt; would be correctly invalid.&lt;/p&gt;

&lt;h2 id=&quot;in-which-i-realize-im-totally-wrong&quot;&gt;IN WHICH I REALIZE I’M TOTALLY WRONG&lt;/h2&gt;

&lt;p&gt;Up until this point, I thought the current grammar allowed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use foo{bar};&lt;/code&gt;. Something about seeing the example of the valid &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use {many, idents};&lt;/code&gt; that I hadn’t thought of as existing, much less being valid, made me look at the current grammar rules again and realize that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use foo{bar};&lt;/code&gt; is actually NOT allowed. If I was going to aim for making &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use foo{bar}&lt;/code&gt;, I’d have follow the path of these substitutions starting from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use_decl&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;use_decl
&quot;pub&quot; ? &quot;use&quot; [ path &quot;as&quot; ident | path_glob ] // initial expansion
&quot;use&quot; [ path &quot;as&quot; ident | path_glob ] // choose to have 0 &quot;pub&quot;s
&quot;use&quot; path_glob // pick the 2nd arm of the use_decl rule since I don't want &quot;as&quot;
&quot;use&quot; ident [ &quot;::&quot; [ path_glob | '*' ] ] ? // pick the 1st arm of the path_glob rule since I want `foo`

// Important part
&quot;use&quot; ident &quot;::&quot; [ path_glob | '*' ] // choose to have the ? be 1 time, not 0, BAM I have to have `::` in there!!!!

// Continuing with the substitutions to show how it plays out
&quot;use&quot; ident &quot;::&quot; path_glob // pick the 1st arm of the choice I have remaining
&quot;use&quot; ident &quot;::&quot; '{' path_item [ ',' path_item ] * '}' // pick the 2nd arm of the path_glob rule
&quot;use&quot; ident &quot;::&quot; '{' path_item '}' // choose to repeat 0 times
&quot;use&quot; ident &quot;::&quot; '{' ident '}' // pick the 1st arm of the path_item rule
&quot;use&quot; &quot;foo&quot; &quot;::&quot; '{' &quot;bar&quot; '}' // substitute in my idents
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I think I was getting confused about where the recursion was happening. Now that I see it this way, it’s hard to see exactly why my brain thought it was a different way (even just 5 minutes ago)! Anyway, I’m not going to spend even a minute feeling bad for my mistake, because it looks like reading and comprehending a grammar definition is hard even for &lt;a href=&quot;https://github.com/rust-lang/rust/pull/14277#discussion_r12778018&quot;&gt;a really smart person&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;SO! I can cross #2 off my list!&lt;/p&gt;

&lt;h2 id=&quot;moving-right-along&quot;&gt;Moving right along&lt;/h2&gt;

&lt;p&gt;Let’s take a look at #3 then. I’m still not so sure about the validity of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mod&lt;/code&gt; keyword in this context. But the commit I just found using the pickaxe search looking for #2 wasn’t the source of the introduction of that. So I’m going to try &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -Gpath_item src/doc/rust.md&lt;/code&gt; instead. This leads me to one of the commits that moved this content around and &lt;a href=&quot;https://github.com/jakub-/rust/commit/7606f580a15da4e093eb51795304c983180a286f&quot;&gt;a new commit&lt;/a&gt; whose message is “Add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use a::b::{c, mod};&lt;/code&gt; to the manual”. Cool! There isn’t really much discussion on &lt;a href=&quot;https://github.com/rust-lang/rust/pull/16537&quot;&gt;the pull request&lt;/a&gt; this commit came in with, but looking at the full diff, I see it changed some of the surrounding documentation that now lives in reference.md, namely “Simultaneously binding a list of paths differing only in their final element and their immediate parent module, using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mod&lt;/code&gt; keyword, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use a::b::{mod, c, d};&lt;/code&gt;” So at the point in time of this commit, this was indeed valid syntax! But what about now? At this point, I’m going to switch from looking at history to looking at the present state. This bullet point does exist in &lt;a href=&quot;https://github.com/rust-lang/rust/blob/9ecc9896dedb426e3f4eb3d23dfc60192fe5275f/src/doc/reference.md#use-declarations&quot;&gt;the current reference.md&lt;/a&gt;, but it now says “Simultaneously binding a list of paths differing only in their final element and their immediate parent module, using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self&lt;/code&gt; keyword, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;use a::b::{self, c, d};&lt;/code&gt;” Neat! So at some point, the way to do this was changed from using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mod&lt;/code&gt; keyword to using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self&lt;/code&gt; keyword and the grammar didn’t keep up with the change! To be absolutely sure, I’m going to look for that change by using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -G &quot;and their immediate parent module, using the \`self\` keyword, such as&quot;&lt;/code&gt; to see when that line appeared in a diff. That gets me &lt;a href=&quot;https://github.com/nodakai/rust/commit/195fd9a2b41ed9b5294f8803aeb84c1ace673e5e&quot;&gt;195fd9a&lt;/a&gt;, which says “This should have been done together with &lt;a href=&quot;https://github.com/nodakai/rust/commit/56dcbd17fdad5d39b7b02e22a7490d2468718d08&quot;&gt;56dcbd1&lt;/a&gt;”. But wait. If I look at the diff of 195fd9a, it looks like the change to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path_item&lt;/code&gt;’s definition that I’m trying to figure out if I need to make has already been made!!! What?!&lt;/p&gt;

&lt;p&gt;At this point, I will admit that I have some knowledge just from having been paying attention to the changes happening in this area for a bit, but I think this is discoverable using the techniques of following changes around that I’ve been demonstrating. When this separate &lt;a href=&quot;https://github.com/icorderi/rust/commit/ffc5f1ccd8b0c7ca07414c324729ecac97f47e6a&quot;&gt;grammar.md file was introduced&lt;/a&gt;, it was created from COPIED content from reference.md, but that content wasn’t REMOVED, and that content evolved separately. Just recently, though, &lt;a href=&quot;https://github.com/rust-lang/rust/pull/24729&quot;&gt;the grammar sections in reference.md were MOVED over to grammar.md&lt;/a&gt; and grammar.md is now linked to from reference.md. I’ve just discovered that at least one section that already existed in grammar.md wasn’t overwritten but should have been since the reference had the more recent content, and now I’m really intrigued because there’s a broader situation to look into and verify that there are or are not other sections that need updates, so I’ve potentially discovered a need for a fix that’s more than one line! Not that there’s anything WRONG with a one line change, mind you, just that there is an overhead to each commit/pull request for both the submitter and maintainers, and it’s always good when you can find CLASSES of problems and fix them rather than individual INSTANCES of problems.&lt;/p&gt;

&lt;p&gt;I’m going to spare you the boring details of checking each section removed from the reference against what existed in the grammar, but my efforts resulted in &lt;a href=&quot;https://github.com/rust-lang/rust/commit/218d38fb94379feca89eb858b309890d513c35da&quot;&gt;this commit&lt;/a&gt;; I found 5 spots.&lt;/p&gt;

&lt;h2 id=&quot;and-back-for-one-more&quot;&gt;And back for one more&lt;/h2&gt;

&lt;p&gt;Now, you’ll notice I’ve mostly been avoiding problem #1. That’s because it’s the hardest one– if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt; has never existed in any form, what is it supposed to be?&lt;/p&gt;

&lt;p&gt;Before considering that question, I’m going to check the history a little bit more to be sure this wasn’t a missed rename, since I noted that when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doc/rust.md&lt;/code&gt; was moved to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/doc/rust.md&lt;/code&gt;, &lt;a href=&quot;https://raw.githubusercontent.com/alexcrichton/rust/864b434bfa3fd5b3ea9e38958652ed1abdc24f1d/src/doc/rust.md&quot;&gt;the document at that version&lt;/a&gt; had this problem already. I started by trying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -Gpath -- doc/rust.md&lt;/code&gt; (I’m not sure why git needs the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--&lt;/code&gt; in this one but not the previous commands, but oh well. Maybe because one slash could be a remote/branch but two slashes is clearly a filename?), but that yielded a lot of false positives of prose containing “path”. I also tried &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -G&quot; path[^s]&quot; -- doc/rust.md&lt;/code&gt;, that got me fewer, but I decided to just start looking at commit messages and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git show&lt;/code&gt;ing ones that looked potentially related, then using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/ path&lt;/code&gt; in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;less&lt;/code&gt; when looking at the diff.&lt;/p&gt;

&lt;p&gt;Then I realized I was being silly– if I’m looking for a potentially-existing-at-some-point definition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;, it would be at the beginning of a line and be followed by a space then a colon (assuming it followed the same conventions as the rest of the file). So that would be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -G&quot;^path :&quot; -- doc/rust.md&lt;/code&gt;. And… nothing. I also tried leaving the filename off; that took a long time to also return nothing. Next I tried &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git log -Guse_decl -- doc/rust.md&lt;/code&gt;, found out a bit about how Rust syntax used to be, but still no clear &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt; definition – everything just referenced the Paths section that only defined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;expr_path&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;type_path&lt;/code&gt;. I eventually got back to &lt;a href=&quot;https://github.com/rust-lang/rust/commit/fefdb63c4c2b078c43836e2ae9d7ffcaeec32890&quot;&gt;the commit introducing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doc/rust.md&lt;/code&gt;&lt;/a&gt;, which says “Begin shift over to using pandoc, markdown and llnextgen for reference manual”, but doesn’t say shift FROM what. Considering there are no deletions, only additions, of the content I’m interested in, I’m assuming it lived somewhere outside of this repository. Dead end!&lt;/p&gt;

&lt;p&gt;Since this post is getting rather long, you’re going to have to tune in next time to see if I just end up adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path : expr_path | type_path ;&lt;/code&gt; or if it’s more complicated than that!! Best of luck on your own journeys exploring code :)&lt;/p&gt;
</description>
				<pubDate>Sat, 01 Aug 2015 15:09:18 +0000</pubDate>
				<link>http://carol-nichols.com/2015/08/01/rustc-discovery/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2015/08/01/rustc-discovery/</guid>
			</item>
		
			<item>
				<title>TDD Example in Rust</title>
				<description>&lt;p&gt;This is an example of how I worked through the &lt;a href=&quot;http://www.butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata&quot;&gt;Prime Factors Kata&lt;/a&gt; using Test Driven Development in Rust (specifically rustc 1.0.0-dev 928e2e239 2015-03-25). I’m assuming you’ve read &lt;a href=&quot;http://doc.rust-lang.org/book/&quot;&gt;The Rust Programming Language Book&lt;/a&gt; through the Testing chapter, so I won’t be explaining much of the Rust syntax.&lt;/p&gt;

&lt;p&gt;The kata, as I decided to interpret it, is:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Write a function that takes an integer greater than 1 and returns a vector of its prime factors, which are the prime numbers that divide that integer exactly, in ascending order. For example, the prime factors of 12 are 2, 2, 3.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you’d like to try it yourself without spoilers, here is where you should stop reading and pick up again when you’re ready!&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;If you’d like to see the full code at each step that I describe below, &lt;a href=&quot;https://github.com/carols10cents/prime_factors/commits/master&quot;&gt;take a look at the commits in this repo&lt;/a&gt;. Additionally, the output from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo test&lt;/code&gt; at that commit is in each commit message.&lt;/p&gt;

&lt;p&gt;So! I generated a new library using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo new&lt;/code&gt;, then started with a failing test:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#[test]
fn prime_factors_of_two() {
    assert_eq!(prime_factors(2), [2]);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And, if I run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cargo test&lt;/code&gt;, it fails to compile:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ cargo test
   Compiling prime_factors v0.0.1
(file:///Users/carolnichols/Rust/prime_factors)
src/lib.rs:3:16: 3:29 error: unresolved name `prime_factors`
src/lib.rs:3     assert_eq!(prime_factors(2), [2]);
                            ^~~~~~~~~~~~~
&amp;lt;std macros&amp;gt;:1:1: 9:39 note: in expansion of assert_eq!
src/lib.rs:3:5: 3:39 note: expansion site
error: aborting due to previous error
Build failed, waiting for other jobs to finish...
Could not compile `prime_factors`.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In my mind, TDD in Rust has two “red” states– failing to compile and failing tests. I’m in the first kind right now. I don’t necessarily hit both states every time, nor do they always happen in the same order. I feel like I’ve even stopped distinguishing the two in my mind– it doesn’t really matter whether it’s the compiler or the tests that are helping me make my code better. The next step is the same: read the first problem and do the simplest thing you can do to respond to that problem.&lt;/p&gt;

&lt;p&gt;The first message here is “error: unresolved name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prime_factors&lt;/code&gt;”, and the simplest thing I can do is define that function without any arguments and with the unit type return value:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#[allow(dead_code)]
fn prime_factors() {
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I also decided to add the attribute to override the dead code lint check since I’m not going to have any non-test code calling this function. Silencing that warning will make the test output clearer. So now I get:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;src/lib.rs:7:16: 7:32 error: this function takes 0 parameters but 1
parameter was supplied [E0061]
src/lib.rs:7     assert_eq!(prime_factors(2), [2]);
                            ^~~~~~~~~~~~~~~~
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Easy enough, I do indeed want the function to take 1 integer parameter:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn prime_factors(num: i64) {
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And now I get a warning and a different error:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;src/lib.rs:2:18: 2:21 warning: unused variable: `num`,
src/lib.rs:2 fn prime_factors(num: i64) {
                              ^~~
&amp;lt;std macros&amp;gt;:5:24: 5:35 error: mismatched types:
 expected `()`,
    found `[_; 1]`
(expected (),
    found array of 1 elements) [E0308]
&amp;lt;std macros&amp;gt;:5 if ! ( ( * left_val == * right_val ) &amp;amp;&amp;amp; ( * right_val ==
* left_val ) ) {
                                      ^~~~~~~~~~~
&amp;lt;std macros&amp;gt;:1:1: 9:39 note: in expansion of assert_eq!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I’m going to let this warning continue to happen for now because I &lt;em&gt;should&lt;/em&gt; be using this variable eventually, but I’m not going to address it this step. I’m more concerned with the first error that says in the test, the things I’m trying to assert are equal aren’t the same type. One, the left side, is the unit type, and the right side is an array of one element.&lt;/p&gt;

&lt;p&gt;So I’m going to fix this error first by just specifying the return type:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn prime_factors(num: i64) -&amp;gt; Vec&amp;lt;i64&amp;gt; {
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;which gets me:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;src/lib.rs:1:1: 2:2 error: not all control paths return a value [E0269]
src/lib.rs:1 fn prime_factors(num: i64) -&amp;gt; Vec&amp;lt;i64&amp;gt; {
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;“Not all”? Try “none”, rustc ;) Anyway, let’s get this compiling and the test passing with the simplest implementation:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn prime_factors(num: i64) -&amp;gt; Vec&amp;lt;i64&amp;gt; {
    vec![2]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And I’m green! Woo! There isn’t anything to refactor here, so onto another failing test:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#[test]
fn prime_factors_of_three() {
    assert_eq!(prime_factors(3), [3]);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;which compiles, but the test fails:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---- prime_factors_of_three stdout ----
	thread 'prime_factors_of_three' panicked at 'assertion failed:
`(left == right) &amp;amp;&amp;amp; (right == left)` (left: `[2]`, right: `[3]`)',
src/lib.rs:13
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Yep, as I expected. So… the next simplest thing to get both these tests to pass is returning the argument as a vector, which should work for any prime number:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn prime_factors(num: i64) -&amp;gt; Vec&amp;lt;i64&amp;gt; {
    vec![num]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This compiles, passes the tests, &lt;em&gt;and&lt;/em&gt; gets rid of those warnings about not using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;num&lt;/code&gt;. Onward!&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#[test]
fn prime_factors_of_four() {
    assert_eq!(prime_factors(4), [2, 2]);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Aha, something a bit more interesting since 4 isn’t prime. This fails as expected:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---- prime_factors_of_four stdout ----
	thread 'prime_factors_of_four' panicked at 'assertion failed:
`(left == right) &amp;amp;&amp;amp; (right == left)` (left: `[4]`, right: `[2, 2]`)',
src/lib.rs:18
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I decided to go with a recursive solution here, but I didn’t really like it at this point. It also took me a while to get the syntax for “return the combination of two vectors as a new vector without any variables” to work right, but it turns out &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; with a reference to another vec works the way I want:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn prime_factors(num: i64) -&amp;gt; Vec&amp;lt;i64&amp;gt; {
    if num % 2 == 0 &amp;amp;&amp;amp; num &amp;gt; 2 {
        vec![2] + &amp;amp;prime_factors(num / 2)
    } else {
        vec![num]
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There’s a lot of conditionals and a lot of “2”s duplicated around. I could have refactored some of those at this point, but I didn’t. This is where experience and judgement calls come in… I knew this wasn’t the general solution, and I knew I didn’t really like my implementation anyway. Thus, I knew there was going to be a lot of change coming up, and refactoring at this point would potentially do more harm than good and just have to be undone later. But don’t let this become an excuse to not refactor!&lt;/p&gt;

&lt;p&gt;If you look at the repo, you’ll see I went ahead and added tests for 5, 6, 7, and 8 that all passed without changing the code. In &lt;a href=&quot;http://www.butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata&quot;&gt;Uncle Bob’s solution&lt;/a&gt;, he skips 5 and 7, ostensibly because he is confident enough in his coverage of prime numbers with tests for 2 and 3 that tests for more primes are no longer interesting. Again, judgement call. It’s also totally ok to write a whole bunch of tests while you’re test driving, but once you understand the domain better, you delete some tests that are covering the same cases before finishing so that you have fewer to wait for to run and fewer that will all break at once if something changes.&lt;/p&gt;

&lt;p&gt;And actually, before I ran my test for 6 for the first time, I expected it to fail since 6 isn’t made up of only factors of 2, but my solution was more general than I thought it was!&lt;/p&gt;

&lt;p&gt;But 9 is the next interesting test case that fails because it isn’t prime and 2 is not any of its prime factors:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;---- prime_factors_of_nine stdout ----
	thread 'prime_factors_of_nine' panicked at 'assertion failed:
`(left == right) &amp;amp;&amp;amp; (right == left)` (left: `[9]`, right: `[3, 3]`)',
src/lib.rs:47
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So. Because I was feeling a little meh about the recursive solution, I decided to switch to an iterative approach. This means I made 3 things mutable: the argument, a vector to hold the result, and a temporary variable that iterates over potential factors. Now, Rust tries to discourage you from making things mutable by making all variables immutable by default, but that doesn’t mean it’s &lt;em&gt;bad&lt;/em&gt; or &lt;em&gt;wrong&lt;/em&gt; necessarily. It’s just a bit wordier.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn prime_factors(mut num: i64) -&amp;gt; Vec&amp;lt;i64&amp;gt; {
    let mut result = vec![];
    while num != 1 {
        let mut i = 2;
        while i &amp;lt;= num {
            if num % i == 0 {
                result.push(i);
                num = num / i;
                break;
            } else {
                i += 1;
            }
        }
    }
    result
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now I’m starting to feel like I have a general solution. 9 passes; in &lt;a href=&quot;https://github.com/carols10cents/prime_factors/commits/master&quot;&gt;the repo&lt;/a&gt; you’ll see I added two more tests– one for a larger prime (101 =&amp;gt; [101]) and one for a larger non-prime (48 =&amp;gt; [2, 2, 2, 2, 3]). Again, these aren’t really necessary since they aren’t covering any cases that aren’t already covered (and they do pass right away). But in this case, they don’t add much time to the test run and they could have caught problems that might not show up with smaller numbers. It all depends.&lt;/p&gt;

&lt;p&gt;Refactoring time! I wanted to see if I could get rid of the mutable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt; variable by switching the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while&lt;/code&gt; loop to be a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop over a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;range&lt;/code&gt;, since that’s all it’s doing anyway.&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn prime_factors(mut num: i64) -&amp;gt; Vec&amp;lt;i64&amp;gt; {
    let mut result = vec![];
    while num != 1 {
        for i in 2..num + 1 {
            if num % i == 0 {
                result.push(i);
                num = num / i;
                break;
            }
        }
    }
    result
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Still passes tests, looks a little better. I still wasn’t happy about it though, since I read in &lt;a href=&quot;http://www.butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata&quot;&gt;Uncle Bob’s description&lt;/a&gt; that he was able to solve the kata in 3 lines of code. I couldn’t spot anything I could change, so at this point I gave up and looked at his solutions in his slides. Now, he did it in Java and his solution was:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for (int candidate = 2; n &amp;gt; 1; candidate++)
    for (; n%candidate == 0; n/=candidate)
        primes.add(candidate);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And now it makes sense. This feels a bit too clever, though, with the for loop conditions not matching the iterator index. It’s neat code golf though. I couldn’t get it down quite this small since Rust doesn’t have this for loop syntax, but this is probably the closest translation and is better than what I ended up with in that it’s not going back out to the outer loop if we can continue to divide by the candidate factor over and over:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let mut result = vec![];
let mut i = 2;
while num &amp;gt; 1 {
    while num % i == 0 {
        result.push(i);
        num /= i;
    }
    i += 1;
}
result
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But is this the most idiomatic Rust solution? It didn’t really feel like it, with all those &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mut&lt;/code&gt;s still in there. So at this point I decided to give the recursive solution a try again:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;fn prime_factors(num: i64) -&amp;gt; Vec&amp;lt;i64&amp;gt; {
    for i in 2..num {
        if num % i == 0 {
            return vec![i] + &amp;amp;prime_factors(num / i)
        }
        i += 1;
    }
    vec![num]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This feels better to me, after having explored the iterative solutions. What do you think? Again, there’s no one right answer :)&lt;/p&gt;

&lt;p&gt;I’ve been meaning to learn how to, and write a post on, how to benchmark Rust and analyze the runtime and memory usage, perhaps I’ll use these different implementations as material for that :)&lt;/p&gt;
</description>
				<pubDate>Sat, 28 Mar 2015 19:31:00 +0000</pubDate>
				<link>http://carol-nichols.com/2015/03/28/tdd-example-in-rust/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2015/03/28/tdd-example-in-rust/</guid>
			</item>
		
			<item>
				<title>Dev URLs without Pow</title>
				<description>&lt;p&gt;&lt;em&gt;TL;DR: I had to do a bunch of research in order to set up pretty URLs to apps running on my localhost at a particular port, so I decided to write up the steps I followed in one place in case anyone else wants to do this. Scroll down past the reasons I wanted to do this for the instructions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I recently decided to use a non-admin user with OSX Yosemite on a daily basis, mostly on the recommendation of &lt;a href=&quot;http://www.macworld.com/article/2841965/swedish-hacker-finds-serious-vulnerability-in-os-x-yosemite.html&quot;&gt;a security researcher who discovered a vulnerability in OSX&lt;/a&gt;, and because it sounds like a good idea in general.&lt;/p&gt;

&lt;p&gt;The only problem I’ve encountered so far is in installing &lt;a href=&quot;http://pow.cx/&quot;&gt;Pow&lt;/a&gt;– the script to install it wants sudo, but it also wants to be run as the current user. There are instructions on the wiki for &lt;a href=&quot;https://github.com/basecamp/pow/wiki/Installation-under-Standard-user&quot;&gt;installation as a standard user&lt;/a&gt;, but they appear to be out of date; I got errors when trying to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;npm run-script&lt;/code&gt;. I also got errors when trying to install it from source. So ¯\_(ツ)_/¯.&lt;/p&gt;

&lt;p&gt;The thing is, I was only using Pow for its pretty URL creation anyway; I use foreman or grunt to run my apps instead of Pow’s rack server. So I set out to get URLs like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://my-project.dev&lt;/code&gt; to serve up a rails app that I would usually access at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://localhost:3000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My first thought was that I could just use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt;, but &lt;a href=&quot;http://stackoverflow.com/questions/10729034/can-i-map-a-hostname-and-a-port-with-etc-hosts&quot;&gt;that only works for host names&lt;/a&gt;, not host names &lt;em&gt;and&lt;/em&gt; ports. Luckily, Apache can do this and, conveniently enough, is included with OSX!&lt;/p&gt;

&lt;p&gt;BUT WAIT! I &lt;em&gt;do&lt;/em&gt; still need to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt;, so that the host goes to localhost where Apache will handle it.&lt;/p&gt;

&lt;h2 id=&quot;instructions&quot;&gt;Instructions&lt;/h2&gt;

&lt;p&gt;So, to spell it all out, I logged in as my admin user (since I’m running from a non-admin day-to-day now) and edited &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ login admin
password:
admin$ sudo vim /etc/hosts
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to add a new entry for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;my-project.dev&lt;/code&gt;. So now, my &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/hosts&lt;/code&gt; file looks like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost

127.0.0.1       my-project.dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At this point, still as the admin user, I started up apache by doing:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;admin$ sudo apachectl start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And then I could go to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://my-project.dev&lt;/code&gt; and see the Apache “It works!” page. &lt;a href=&quot;https://www.youtube.com/watch?v=lDK9QqIzhwk&quot;&gt;We’re halfway there!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, edit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/apache2/httpd.conf&lt;/code&gt;, find this part about Virtual Hosts and uncomment the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Include&lt;/code&gt; line so it looks like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then, edit THAT file, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/private/etc/apache2/extra/httpd-vhosts.conf&lt;/code&gt;, remove the two sample &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;VirtualHost&amp;gt;&lt;/code&gt;s, and insert a new one that looks like this, as recommended by &lt;a href=&quot;http://serverfault.com/a/195831/174036&quot;&gt;this ServerFault answer&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;NameVirtualHost *:80

&amp;lt;VirtualHost *&amp;gt;
    ServerName my-project.dev
    ProxyPreserveHost On

    # setup the proxy
    &amp;lt;Proxy *&amp;gt;
        Order allow,deny
        Allow from all
    &amp;lt;/Proxy&amp;gt;
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then restart apache:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;admin$ sudo apachectl restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And, if you’ve got your app running at port 3000, you should be able to visit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://my-project.dev&lt;/code&gt; in your browser and get to your app!&lt;/p&gt;
</description>
				<pubDate>Sat, 27 Dec 2014 21:48:00 +0000</pubDate>
				<link>http://carol-nichols.com/2014/12/27/dev-urls-without-pow/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2014/12/27/dev-urls-without-pow/</guid>
			</item>
		
			<item>
				<title>Ruby, Rust, and Concurrency</title>
				<description>&lt;p&gt;I mostly do Ruby these days, but I’ve just recently started getting into Rust. I’m finding it super
interesting! It’s definitely expanding the way I think about programming, and I’m excited about the
future it’s foreshadowing.&lt;/p&gt;

&lt;p&gt;I’m a visual and experiental learner though, so when I see statements like “&lt;s&gt;Race conditions&lt;/s&gt; Data races are
compile-time errors” (&lt;a href=&quot;http://www.reddit.com/r/rust/comments/2aq2zy/ruby_rust_and_concurrency/cixp069&quot;&gt;thank you for the clarification, dbaupp :)&lt;/a&gt;) I wonder what that looks like in the code, what kind of error message you get,
and how that differs from what I know in Ruby.&lt;/p&gt;

&lt;p&gt;So I went out hunting for a trivial race condition in Ruby that I could port to Rust to see what would happen.
&lt;a href=&quot;https://stackoverflow.com/questions/18574254/generating-a-race-condition-with-mri/18576777#18576777&quot;&gt;Stack overflow to the rescue!&lt;/a&gt; Here’s a slightly modified version of the Ruby code in that post that suited my purposes
better. Note that this is totally contrived and you would never actually want to have a bunch of
threads help you increment a number from 1 to 500:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;THREADS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;COUNT&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;

&lt;span class=&quot;vg&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;THREADS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;times&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;COUNT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;times&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;sleep&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.000001&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Thread &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; wrote &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
      &lt;span class=&quot;vg&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;THREADS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;COUNT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Got $x = &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;vg&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.&quot;&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Expected to get &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;THREADS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;COUNT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;.&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Did not reproduce the issue.&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This causes a race condition most of the time that I run it in Ruby 2.0– I’ve got a global
variable that the threads read, then they sleep, then they increment, and in the time that one
thread sleeps, another thread reads and/or writes that global. Stuff happens all out of order.
Here’s parts of the output from one of the times I ran it:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;78&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;29&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;59&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;29&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;78&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;60&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrote&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;51&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Got&lt;/span&gt; &lt;span class=&quot;vg&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;51&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Expected&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;501&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So what would it look like if I tried to do this in Rust, as faithfully as possible to the
contrived example? Here’s my attempt, critiques welcome:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;std&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;nn&quot;&gt;timer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;threads&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;mut&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;threads&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;spawn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0u&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nf&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;// error: cannot assign to immutable&lt;/span&gt;
               &lt;span class=&quot;c&quot;&gt;// captured outer variable in a proc `x`&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;println!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;The result is {}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Sure enough, if I try to compile this with rust 0.11.0, I get a compile error! Cool! I’ve marked
its location and text with a comment. This error says that the compiler captures &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and makes it
immutable while in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;proc&lt;/code&gt;. I can write to it all I want in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;main&lt;/code&gt; where it’s declared as
mutable, but because I could create race conditions if I was allowed to write to it in a
spawned thread, Rust doesn’t let me do that.&lt;/p&gt;

&lt;p&gt;I tried to make a version of this program in Rust done The Right Way that would compile and run,
but a lot of what I’m seeing is basically saying “don’t do that” to me (and this is a super
contrived example). I tried a little bit with mutexes, a little with futures, but they ended up
changing the logic happening so that one thread was locking the value for its whole existence, then
the next thread would lock it, etc. I never got it to compile, run, and illustrate everything I
wanted it to, so I’m leaving that as an exercise for the reader :) &lt;a href=&quot;http://www.reddit.com/r/rust/comments/29teem/an_experiment_in_forkjoin_parallelism_in_rust/&quot;&gt;This experiment in fork/join parallelism in Rust&lt;/a&gt;
looks relevant to my interests but I haven’t digested it all yet.&lt;/p&gt;

&lt;p&gt;Hope this helps someone else to understand the different choices that Ruby and Rust make in this
area! I’d love to hear what you think, how I could make this example better, or how you’d implement
this example to compile and run in Rust :) &amp;lt;3&lt;/p&gt;
</description>
				<pubDate>Mon, 14 Jul 2014 21:48:00 +0000</pubDate>
				<link>http://carol-nichols.com/2014/07/14/ruby-rust-concurrency/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2014/07/14/ruby-rust-concurrency/</guid>
			</item>
		
			<item>
				<title>On Stack Overflow</title>
				<description>&lt;p&gt;On Dec 24, 2013, Michael Richter wrote a post about &lt;a href=&quot;http://michael.richter.name/blogs/why-i-no-longer-contribute-to-stackoverflow/document_view&quot;&gt;why he no longer
contributes to Stack
Overflow&lt;/a&gt;. I don’t feel the same way as he does, but it made me do
a lot of thinking and, eventually, a lot of writing. I left these thoughts as a
comment on his post but I decided to make my own post as well. Here they are,
slightly modified from their comment form to make a bit more sense standing on
their own.&lt;/p&gt;

&lt;p&gt;Background: &lt;a href=&quot;http://stackoverflow.com/users/51683/carols10cents&quot;&gt;I’ve got a little over 3k in Stack Overflow
points&lt;/a&gt; currently and I
mostly read, ask, and answer questions in the [ruby] and [ruby-on-rails] topic
tags.&lt;/p&gt;

&lt;p&gt;The reason I think Michael’s post hit me so hard is that in the community
organizing stuff I do, I’ve been trying to be very deliberate about making
welcoming environments for everyone. So if there are people who don’t feel that
Stack Overflow is a welcoming environment, I’m interested in seeing what we can
do to change that, and possibly extrapolating the results to my other
communities too.&lt;/p&gt;

&lt;p&gt;Michael mentions that answers written hastily after a quick google search often
get more points than answers that took a long time to write. He also notes that
he’s earned thousands of points while being inactive. I agree that SO doesn’t
directly incentivize thoughtful answers to complex questions. To me, though,
it’s much more about whether I helped someone or not (as Jay Hanlon mentioned
in the comments). I enjoy knowing that some content I put on the internet a few
years ago is still helping someone today. I would hate to lose that feedback,
but I agree it doesn’t necessarily make sense to tie so much information to the
one reputation number. Perhaps privileges should be separated more from votes,
but I don’t know what would be a better fuzzy metric for “this person knows
about these topics and cares about the quality of the content”.&lt;/p&gt;

&lt;p&gt;The other useful thing about votes on answers is to indicate which answers are
better than others, both from the point of view of the original asker and from
the point of view of future searchers with the same question. I wonder if this
could be fixed with, instead of showing the raw count of upvotes per answer,
showing a percentage of votes a particular answer got out of all the votes cast
on the answers for that question. This would be a pretty radical change for SO
though…&lt;/p&gt;

&lt;p&gt;Michael also noticed that, after he posted a controversial answer, people were
downvoting all of his answers. I’m not sure when this was implemented, but
&lt;a href=&quot;http://meta.stackoverflow.com/questions/126829/what-is-serial-voting-and-how-does-it-affect-me&quot;&gt;Stack Exchange actually has detection for serial
voting&lt;/a&gt; (both up and down) in place and I’ve seen the system
automatically reverse some serial downvoting of my answers within 24 hours.&lt;/p&gt;

&lt;p&gt;Michael has been feeling a creeping authoritarianism in the culture of Stack
Overflow lately. I agree that the culture of SO has changed, but I don’t
remember exactly when, why, or how it happened (my SO activity has waxed and
waned a few times). Today, I would probably vote to close some of the questions
I asked a few years ago, but as far as I know they were within the site
guidelines for good questions at the time. I’m a little confused when putting
this section together with the poor pedagogy section though– many of the
situations you cite there of people not attempting to solve their own problems
first are handled by moderating them as duplicates, needing more information
about what the asker has tried and why that didn’t work, etc. Which would be
&lt;em&gt;more&lt;/em&gt; authoritarianism, but this section seems like you’d prefer &lt;em&gt;less&lt;/em&gt;. I’m
just not sure if it’s possible to go back to the way SO used to be now that
it’s at this scale.&lt;/p&gt;

&lt;p&gt;I definitely think SO could use improvement in the experience of a new user. As
pointed out by the commenter Mike and others, not being able to comment unless
you’ve gotten 50 points is incredibly frustrating and off-putting, and I’ve
seen it cause poor content to be created (answers that start with “I can’t
comment on X but…”). Yet another case where reputation is being used for
multiple purposes– in this case I think mostly to prevent spam and sock
puppets.&lt;/p&gt;

&lt;p&gt;It’s also really hard to explain what makes a question high quality for this
format and guide a new user towards providing the information that will help
them get the information they’re looking for fastest. The current process of
closing questions, even with the close reasons given to the asker as feedback,
does feel very authoritarian and unwelcoming. This would require much more
effort, but perhaps instead of closing, they could be put in a “mentoring”
queue that has more affordances for back-and-forth discussion than the main Q&amp;amp;A
format. Maybe that’s what chat is for and more questions need to be moved
there? I don’t really use chat much.&lt;/p&gt;

&lt;p&gt;One final comment I’d like to make is that it’s really tough to paint “the SO
community” with one brush, given the number of users and breadth of subtopics.
As prolific as he is, I actually rarely see any answers from Jon Skeet because
I don’t do any C# and he doesn’t do any Ruby. There are definitely subcultures
in SO (although I’d be hard pressed to define them). I suppose my point here is
that I’m not convinced that the structure of SO is &lt;em&gt;entirely&lt;/em&gt; responsible for
the “community” aspect since the same structure has enabled different cultures
to exist (although it’s &lt;em&gt;definitely&lt;/em&gt; a large factor).&lt;/p&gt;

&lt;p&gt;Michael, thank you again for sharing your thoughts– I appreciate by your “poor
community counter” that this isn’t easy.&lt;/p&gt;

</description>
				<pubDate>Sat, 04 Jan 2014 21:55:00 +0000</pubDate>
				<link>http://carol-nichols.com/2014/01/04/on-stack-overflow/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2014/01/04/on-stack-overflow/</guid>
			</item>
		
			<item>
				<title>And we're back</title>
				<description>&lt;p&gt;Sigh. My wordpress blog apparently got hacked, my host shut it down, so I’ve
spent the last few days converting this blog to a Jekyll site hosted on github
pages. I really can’t recommend wordpress to anyone anymore, given the massive
botnets that are attacking every wordpress site out there right now. For
non-technical folks or lazy technical people who don’t want to have to spend
time keeping up with updates or fighting off attacks, it’s just not a viable
solution.&lt;/p&gt;

&lt;p&gt;So here we are. I still have a lot of work to do… the links between posts are
all broken, I had some google juice on some of the posts at least, so it’d be
nice if links to my old posts still redirected. I haven’t decided whether to
add commenting via disqus or similar– I like hearing that my posts have helped
people, but do I really need them? They’re really just another avenue for spam.
I’m also not wild about this theme, it’s nice enough, but it’s just not really
&lt;em&gt;me&lt;/em&gt; (but I also don’t have the time/talent to make something I like better).&lt;/p&gt;

&lt;p&gt;Oh, and it’s a good thing I’m not very prolific because I ended up converting
my posts by hand. I had a sql dump file, not a wordpress xml export, and
apparently the jekyll-import gem only supports connecting to a database or
importing from the wp xml file. &lt;a href=&quot;https://github.com/mojombo/jekyll/pull/1662&quot;&gt;And I had enough problems just getting the
jekyll import process to tell me
that&lt;/a&gt; that I didn’t exactly have
much confidence that the import process was going to go well anyway.&lt;/p&gt;

&lt;p&gt;Technology sucks. Everything’s broken.&lt;/p&gt;
</description>
				<pubDate>Sat, 26 Oct 2013 18:46:00 +0000</pubDate>
				<link>http://carol-nichols.com/2013/10/26/and-were-back/</link>
				<guid isPermaLink="true">http://carol-nichols.com/2013/10/26/and-were-back/</guid>
			</item>
		
	</channel>
</rss>
