<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title>ctrl-c</title>
	<subtitle>a friendly interruption</subtitle>
	<link href="https://ctrl-c.us/atom.xml" rel="self" />
	<link href="https://ctrl-c.us" />
	<id>https://ctrl-c.us/</id>
	<updated>2024-09-04T00:00:00Z</updated>
  <author><name>Caleb Spare</name></author>

  
	<entry>
		<title>The tool cache manifesto</title>
		<link href="https://ctrl-c.us/posts/tool-cache" />
		<updated>2024-09-04T00:00:00Z</updated>
		<id>tag:ctrl-c.us,2024-09-04:/posts/tool-cache.html</id>
		<content type="html">
      &lt;p&gt;In my career I&amp;rsquo;ve used many tools which keep local file caches of one kind or&#xA;another: pip, Maven, npm, &lt;code&gt;clj&lt;/code&gt;, &lt;code&gt;go&lt;/code&gt;, staticcheck, and dozens of others. I&amp;rsquo;ve&#xA;also written a few myself.&lt;/p&gt;&#xA;&lt;p&gt;Besides running them on my local computers, I&amp;rsquo;ve also maintained automated test&#xA;suites and CI systems that use these tools.&lt;/p&gt;&#xA;&lt;p&gt;These experiences have led me to a set of ideas about what makes a tool cache&#xA;good or bad.&lt;/p&gt;&#xA;&lt;h1&gt;Tool caches must be correct&lt;/h1&gt;&#xA;&lt;p&gt;The most important job of a tool cache is to be correct. Correctness means that&#xA;the user &lt;em&gt;cannot tell&lt;/em&gt; that the tool is making use of a cache&amp;mdash;other than that&#xA;the tool is faster.&lt;/p&gt;&#xA;&lt;p&gt;Here are some specific behaviors of correct tool caches:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;The tool invisibly recovers from being interrupted or killed.&lt;/strong&gt;&#xA;Typically, this means that it writes files atomically or else handles&#xA;partially-written files.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;The tool invisibly recovers from having its cache directory deleted.&lt;/strong&gt;&#xA;Excellent cache implementations also recover if you selectively delete or even&#xA;mutate cache files, but we can call this a stretch goal.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;The tool just works when you update to a new version.&lt;/strong&gt; Usually this&#xA;means that the tool understands the version of the cached data and partially&#xA;or completely invalidates it when the tool changes.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Concurrent executions of the tool just work.&lt;/strong&gt; They should never cause&#xA;errors or break the cache.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Here are some smells that may indicate a poor tool cache:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;The tool fails with an error message that mentions cache files.&lt;/li&gt;&#xA;&lt;li&gt;The tool authors encourage the deletion of the cache as a troubleshooting step.&lt;/li&gt;&#xA;&lt;li&gt;The tool has provisions for initializing, manipulating, or interacting with&#xA;the cache.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h1&gt;Caching is a privilege, not a right&lt;/h1&gt;&#xA;&lt;p&gt;If a tool&amp;rsquo;s cache behavior is not correct, it&amp;rsquo;s usually best to disable the&#xA;cache. Better to be slow and correct than fast and broken.&lt;/p&gt;&#xA;&lt;p&gt;Disabling the cache can mean running the tool in a no-cache mode or wiping the&#xA;cache before each invocation.&lt;/p&gt;&#xA;&lt;p&gt;Occasionally when you do this you&amp;rsquo;ll discover that a tool cache is not just&#xA;buggy, but also unnecessary, because the cache hardly makes the tool faster at&#xA;all.&lt;/p&gt;&#xA;&lt;h1&gt;Cache location&lt;/h1&gt;&#xA;&lt;p&gt;By default, tools should locate their caches according to the system&amp;rsquo;s&#xA;conventions. For example:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;macOS: &lt;code&gt;~/Library/Caches&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;Linux: &lt;a href=&#34;https://specifications.freedesktop.org/basedir-spec/latest/&#34;&gt;&lt;code&gt;$XDG_CACHE_HOME&lt;/code&gt;&lt;/a&gt;,&#xA;falling back to &lt;code&gt;~/.cache&lt;/code&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Dumping cache files in &lt;code&gt;/tmp&lt;/code&gt; or &lt;code&gt;~/.bespoke&lt;/code&gt; may have been fine in 1995, but&#xA;it&amp;rsquo;s not acceptable anymore.&lt;/p&gt;&#xA;&lt;p&gt;It should be easy for the user to override a cache location, typically via an&#xA;environment variable. This should be clearly and prominently documented.&lt;/p&gt;&#xA;&lt;h1&gt;Dependencies&lt;/h1&gt;&#xA;&lt;p&gt;If your tool has a code dependency that writes a file cache, &lt;em&gt;you own that&#xA;cache&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;If a dependency&amp;rsquo;s caching behavior is broken, your tool needs to shield the user&#xA;from it. For example, the tool might disable the cache, it might selectively&#xA;delete the cache when it&amp;rsquo;s in a bad state that confuses the dependency, or it&#xA;might add concurrency control if the dependency doesn&amp;rsquo;t properly handle&#xA;concurrent cache use.&lt;/p&gt;&#xA;&lt;p&gt;If a dependency places its cache in a weird location, your tool should override&#xA;it.&lt;/p&gt;&#xA;&lt;p&gt;If your tool has multiple caches, they should be consolidated in a single&#xA;directory rather than scattered across the file system.&lt;/p&gt;&#xA;&lt;h1&gt;The best cache is no cache&lt;/h1&gt;&#xA;&lt;p&gt;Modern hardware can read files and process data &lt;em&gt;very&lt;/em&gt; quickly. A tool can do a&#xA;huge amount of work in 5 seconds (or even 50 ms).&lt;/p&gt;&#xA;&lt;p&gt;Good tools are fast. When a good tool uses a file cache for performance reasons,&#xA;it&amp;rsquo;s because the problem at hand is fundamentally expensive enough that a cache&#xA;makes a real difference after the work has been well-optimized.&lt;/p&gt;&#xA;&lt;p&gt;Adding a cache to a slow, inefficient tool can result in a tool which is still&#xA;slow some of the time but also has new correctness issues. If a tool&amp;rsquo;s authors&#xA;haven&amp;rsquo;t delivered respectable performance, I&amp;rsquo;m skeptical that they can implement&#xA;a solid file cache.&lt;/p&gt;&#xA;&lt;h1&gt;Doing tool caches right&lt;/h1&gt;&#xA;&lt;p&gt;I&amp;rsquo;d love to point to an article which explains how best to implement file caches&#xA;for tools. Unfortunately, I don&amp;rsquo;t think it exists.&lt;/p&gt;&#xA;&lt;p&gt;Each use case has its own particular needs; doing this task well requires&#xA;thinking through those needs and coming up with a design that plays nicely with&#xA;the underlying capabilities of the OSes you are targeting.&lt;/p&gt;&#xA;&lt;p&gt;I can recommend studying &lt;a href=&#34;https://github.com/golang/go/tree/master/src/cmd/go/internal/cache&#34;&gt;the Go tool&amp;rsquo;s&#xA;cache&lt;/a&gt;; it&#xA;works extremely well.&lt;/p&gt;&#xA;&lt;p&gt;Apenwarr&amp;rsquo;s blog post &lt;a href=&#34;https://apenwarr.ca/log/20181113&#34;&gt;mtime comparison considered harmful&lt;/a&gt;&#xA;is well worth a read. It&amp;rsquo;s focused on &lt;code&gt;make&lt;/code&gt;-like tools, but many of the&#xA;pitfalls he points out also await implementers of file caches.&lt;/p&gt;&#xA;
		</content>
	</entry>
  
	<entry>
		<title>Leaving California</title>
		<link href="https://ctrl-c.us/posts/california" />
		<updated>2021-09-12T00:00:00Z</updated>
		<id>tag:ctrl-c.us,2021-09-12:/posts/california.html</id>
		<content type="html">
      &lt;p&gt;I&amp;rsquo;ve lived in California for more than 11 years&amp;mdash;longer than anywhere&#xA;else&amp;mdash;so it&amp;rsquo;s as much my home as any place. But in a few weeks I am moving&#xA;away.&lt;/p&gt;&#xA;&lt;p&gt;California is a truly special place. As my time here draws to an end, I&amp;rsquo;ve been&#xA;thinking about the many reasons I love this state.&lt;/p&gt;&#xA;&lt;img src=&#34;/img/california/angel_island_small.04wXWIou0n.jpg&#34; data-lightbox=&#34;/img/california/angel_island.MAq8HFwaIp.jpg&#34;&gt;&#xA;&lt;p&gt;I came to Silicon Valley to work in software, and there is no better place for&#xA;it. The deep history of the computing industry here and the density of &lt;em&gt;my&#xA;people&lt;/em&gt; on the ground still surprises me sometimes. It&amp;rsquo;s typical to encounter&#xA;programmers online only to later discover that they live a few miles away.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/black_mountain_small.wj015SRslQ.jpg&#39; data-lightbox=&#39;/img/california/black_mountain.KY9iIaXTqr.jpg&#39;&gt;&#xA;&lt;p&gt;I love the variety of excellent food here. During my time in California I found&#xA;a real love of Korean cuisine, and of Oren&amp;rsquo;s pita, and of the amazing burritos&#xA;from the place down the road.&lt;/p&gt;&#xA;&lt;p&gt;I love Dutch Crunch. There is no better sandwich bread.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/mt_whitney_small.2GPA89bw5f.jpg&#39; data-lightbox=&#39;/img/california/mt_whitney.8tjb0RSgQu.jpg&#39;&gt;&#xA;&lt;p&gt;I love the Sierra Nevada: the sharp peaks, bare white granite, and sheer&#xA;vertical relief. I have many happy memories of hikes, runs, and scrambles up and&#xA;down these mountains.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/granite_dome_small.AEssoT47Oe.jpg&#39; data-lightbox=&#39;/img/california/granite_dome.LRnDntMXob.jpg&#39;&gt;&#xA;&lt;img src=&#39;/img/california/pyramid_peak_small.Fn1YysQghH.jpg&#39; data-lightbox=&#39;/img/california/pyramid_peak.TpkWqn8iYi.jpg&#39;&gt;&#xA;&lt;img src=&#39;/img/california/rubicon_peak_small.bbjsgvjy87.jpg&#39; data-lightbox=&#39;/img/california/rubicon_peak.ezOp9e07L0.jpg&#39;&gt;&#xA;&lt;p&gt;I love US 395, which can give any road a run for its money as the finest highway&#xA;in America.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/mono_lake_small.UsIX7bdj2Y.jpg&#39; data-lightbox=&#39;/img/california/mono_lake.ibrkDSvSq3.jpg&#39;&gt;&#xA;&lt;img src=&#39;/img/california/jigsaw_pass_small.HESpg9izKR.jpg&#39; data-lightbox=&#39;/img/california/jigsaw_pass.KdN1m26xbi.jpg&#39;&gt;&#xA;&lt;p&gt;California is where I&amp;rsquo;ve done most of my skiing, and where I started&#xA;backcountry skiing. I will remember so many fun ski trips with friends, and solo&#xA;missions, around Tahoe, Kirkwood, and Mammoth.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/ellis_peak_small.blSkgJCVZc.jpg&#39; data-lightbox=&#39;/img/california/ellis_peak.Q7kdAkKqM2.jpg&#39;&gt;&#xA;&lt;p&gt;Skiing in California becomes magical during the long spring season. When the&#xA;crowds thin out, the days become sunny and warm, and you can cruise down&#xA;wide expanses of corn snow, then you have been blessed.&lt;/p&gt;&#xA;&lt;p&gt;I was lucky enough to live through a few big snow years during my time here,&#xA;when we had great skiing through May, June, and into July.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/rose_knob_peak_small.m4PjzC53mY.jpg&#39; data-lightbox=&#39;/img/california/rose_knob_peak.Vb9kSxjxyx.jpg&#39;&gt;&#xA;&lt;p&gt;I also love the hills closer to where I live, where I began trail running. I&#xA;love the freedom of running along grassy ridgelines with views for 30, 40, 50&#xA;miles in every direction.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/mission_peak1_small.RLeHlB3kj5.jpg&#39; data-lightbox=&#39;/img/california/mission_peak1.pLTccHm0ki.jpg&#39;&gt;&#xA;&lt;p&gt;I will fondly remember Mt. Diablo, Rancho San Antonio, Montara Mountain, and the&#xA;many other peaks, hills, and trails I enjoyed around the bay.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/mission_peak2_small.NPGFQ61Cqv.jpg&#39; data-lightbox=&#39;/img/california/mission_peak2.bZEHUU2DgW.jpg&#39;&gt;&#xA;&lt;p&gt;And I will remember Mission Peak, my longtime favorite. I love the vivid green&#xA;it takes on after winter rain and the bright gold during the dry summers. I love&#xA;the feeling of pushing myself hard up the hill, knowing that I can surely manage&#xA;it because I&amp;rsquo;ve done it dozens of times before, while watching the hawks and&#xA;paragliders spiral lazily on thermals overhead. And I love running down the&#xA;trail in the evening, always a little later than I intended, seeing the fog&#xA;slowly roll over the coastal hills while a vast web of lights starts to shine&#xA;across the valley below.&lt;/p&gt;&#xA;&lt;img src=&#39;/img/california/mission_peak3_small.29xsq9T6uX.jpg&#39; data-lightbox=&#39;/img/california/mission_peak3.j7rxirnyar.jpg&#39;&gt;&#xA;
		</content>
	</entry>
  
	<entry>
		<title>Cross-arch Go tests with QEMU</title>
		<link href="https://ctrl-c.us/posts/test-goarch" />
		<updated>2021-08-28T00:00:00Z</updated>
		<id>tag:ctrl-c.us,2021-08-28:/posts/test-goarch.html</id>
		<content type="html">
      &lt;p&gt;This is handy:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;$ GOARCH=arm64 go test&#xA;PASS&#xA;ok      github.com/cespare/xxhash/v2    0.201s&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;My machine has an amd64 CPU. How does this work?&lt;/p&gt;&#xA;&lt;p&gt;For a long time (since Go 1.5), compiling for a different architecture has been&#xA;as easy as setting the appropriate &lt;code&gt;GOARCH&lt;/code&gt;. &lt;code&gt;go test&lt;/code&gt; works in two steps: it&#xA;synthesizes and compiles a main package with all the tests and then runs it. So&#xA;&lt;code&gt;GOARCH=arm64 go test&lt;/code&gt; tells the build step to compile for arm64, not amd64.&lt;/p&gt;&#xA;&lt;p&gt;Normally that means that this command fails at the run step:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;$ GOARCH=arm64 go test&#xA;fork/exec /tmp/go-build2883591561/b001/xxhash.test: exec format error&#xA;FAIL    github.com/cespare/xxhash/v2    0.001s&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;However, we can use QEMU to emulate the target architecture. Specifically, we&#xA;can use QEMU&amp;rsquo;s &lt;a href=&#34;https://qemu-project.gitlab.io/qemu/user/index.html&#34;&gt;user mode emulation&lt;/a&gt; to run the test binary directly. In this&#xA;mode QEMU translates the syscalls and takes care of other details so that the&#xA;program can run using the host kernel rather than emulating an entire machine.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;$ GOARCH=arm64 go test -c -o test.arm64&#xA;$ qemu-aarch64 test.arm64&#xA;PASS&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Then the Linux kernel has a feature called &lt;a href=&#34;https://en.wikipedia.org/wiki/Binfmt_misc&#34;&gt;binfmt_misc&lt;/a&gt; which allows the user&#xA;to associate arbitrary executable formats with userspace programs. On Ubuntu, if&#xA;you install the &lt;code&gt;qemu-user-binfmt&lt;/code&gt; package (which comes along as a recommend&#xA;package if you &lt;code&gt;apt install qemu&lt;/code&gt;), it will register all the executable formats&#xA;QEMU supports using binfmt_misc. Thus:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;$ GOARCH=arm64 go test -c -o test.arm64&#xA;$ ./test.arm64&#xA;PASS&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;or simply&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;$ GOARCH=arm64 go test&#xA;PASS&#xA;ok      github.com/cespare/xxhash/v2    0.191s&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;I think that&amp;rsquo;s pretty neat!&lt;/p&gt;&#xA;&lt;h1&gt;Cross-arch Go tests with GitHub Actions&lt;/h1&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://docs.github.com/en/actions&#34;&gt;GitHub Actions&lt;/a&gt; is a convenient way to run automated tests for projects on&#xA;GitHub, but the free VMs they provide are currently amd64-only. Hopefully in the&#xA;future they will add more architectures, but in the meantime, we can use the&#xA;method described here to run cross-arch tests using QEMU.&lt;/p&gt;&#xA;&lt;p&gt;In a workflow file, use the &lt;a href=&#34;https://github.com/marketplace/actions/docker-setup-qemu&#34;&gt;docker/setup-qemu-action&lt;/a&gt; action to install QEMU&#xA;static binaries and configure binfmt_misc. Then you can run &lt;code&gt;go test&lt;/code&gt; with the&#xA;appropriate value of &lt;code&gt;GOARCH&lt;/code&gt;. The &lt;a href=&#34;https://github.com/cespare/xxhash/blob/08b98dc4990404e47f9bb36d46cae56196a0a0b6/.github/workflows/test.yml#L31-L56&#34;&gt;workflow file&lt;/a&gt; for&#xA;&lt;a href=&#34;https://github.com/cespare/xxhash&#34;&gt;github.com/cespare/xxhash&lt;/a&gt; is a working&#xA;example.&lt;/p&gt;&#xA;&lt;h1&gt;Caveats&lt;/h1&gt;&#xA;&lt;p&gt;One limitation here is that it assumes a statically linked binary; if your code&#xA;requires cgo, you&amp;rsquo;ll need add &lt;code&gt;-extldflags=-static&lt;/code&gt; to the linker flags and use&#xA;a C compiler for the target architecture. (Also, note that the Go tool sets&#xA;&lt;code&gt;CGO_ENABLED=0&lt;/code&gt; for cross-compiles by default.)&lt;/p&gt;&#xA;&lt;p&gt;It should also be possible to make this work with dynamically linked binary.&#xA;&lt;a href=&#34;https://wiki.debian.org/QemuUserEmulation&#34;&gt;This Debian Wiki page&lt;/a&gt; suggests that&#xA;you&amp;rsquo;d need to install the libc6 package for the target architecture so that QEMU&#xA;will have access to the appropriate ELF interpreter (I haven&amp;rsquo;t tried it).&lt;/p&gt;&#xA;
		</content>
	</entry>
  
	<entry>
		<title>Why I use minimal syntax highlighting</title>
		<link href="https://ctrl-c.us/posts/highlight" />
		<updated>2019-02-03T00:00:00Z</updated>
		<id>tag:ctrl-c.us,2019-02-03:/posts/highlight.html</id>
		<content type="html">
      &lt;p&gt;People who see code in my text editor often comment about the lack of syntax&#xA;highlighting.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;/img/highlight/cespare-go.ig9QsdadbX.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;Over the dozen-odd years I&amp;rsquo;ve been writing code, I have used a few different&#xA;colorschemes. I favor dark, medium- to low-contrast themes because I find this&#xA;more comfortable on my eyes for extended reading.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;/img/highlight/oceandeep.CuCzasLynf.png&#34; alt=&#34;&#34;&gt;&#xA;&lt;img src=&#34;/img/highlight/zenburn.LnJ07ks5f1.png&#34; alt=&#34;&#34;&gt;&#xA;&lt;img src=&#34;/img/highlight/lucius.CucEMAXqak.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;I always considered my colorscheme to be an aesthetic preference. I hadn&amp;rsquo;t&#xA;questioned the tacit understanding that rendering each bit of syntax in a&#xA;different color was a necessary aid to programming practice until a few&#xA;years ago, when I became aware that some influential folks from the &lt;a href=&#34;https://www.youtube.com/watch?v=dP1xVpMPn8M&#34;&gt;Go and Plan&#xA;9 world&lt;/a&gt; do not use syntax&#xA;highlighting in their editors. (Rob Pike &lt;a href=&#34;https://groups.google.com/d/msg/golang-nuts/hJHCAaiL0so/kG3BHV6QFfIJ&#34;&gt;went so far as to deride syntax&#xA;highlighting as&#xA;&amp;ldquo;juvenile&amp;rdquo;&lt;/a&gt;.)&lt;/p&gt;&#xA;&lt;p&gt;I credit a lot of my growth as a programmer in recent years to the influence of&#xA;the Go authors and community, so the notion of disabling syntax highlighting&#xA;stuck with me. Maybe it was an entirely unnecessary practice that our industry&#xA;had adopted en masse? It wouldn&amp;rsquo;t have been the first &amp;ldquo;modern&amp;rdquo; programming&#xA;tradition to be productively challenged by my participation in the Go community.&lt;/p&gt;&#xA;&lt;p&gt;So in 2016, I turned off syntax highlighting in Vim to judge this idea for&#xA;myself.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;/img/highlight/nohighlight.TlWacdY482.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;At first, it felt strange, like the text was pseudocode in an academic paper.&#xA;After a couple of days it stopped seeming foreign, but it still took more work&#xA;to understand code as I read it. The biggest problem was comments: the tiny&#xA;amount of effort to classify bits of text as comment or code added up. I&#xA;concluded that for myself, at least, distinguishing comments is a valuable&#xA;function of syntax highlighting.&lt;/p&gt;&#xA;&lt;p&gt;I created my own minimal syntax highlighting theme which colored all text the&#xA;same way except for comments. I eventually made a few more concessions: coloring&#xA;string literals differently helps in the same way that coloring comments helps,&#xA;and in Markdown, highlighting section headers makes it easy for me to pick out&#xA;document structure as I scroll around.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;/img/highlight/cespare-python.hAyfb5vyT3.png&#34; alt=&#34;&#34;&gt;&#xA;&lt;img src=&#34;/img/highlight/cespare-markdown.cs8W76LVrR.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;The rest&amp;mdash;a different color for each keyword, parenthesis, number, and so&#xA;on&amp;mdash;I don&amp;rsquo;t miss. Three years later, I&amp;rsquo;ve barely thought about my syntax&#xA;highlighting at all. My minimal colorscheme isn&amp;rsquo;t &lt;em&gt;better&lt;/em&gt; than the colorschemes&#xA;I used to use, but it turned out that most of the colors were unnecessary.&lt;/p&gt;&#xA;
		</content>
	</entry>
  
	<entry>
		<title>Generating permutations in Go</title>
		<link href="https://ctrl-c.us/posts/permute" />
		<updated>2018-05-15T00:00:00Z</updated>
		<id>tag:ctrl-c.us,2018-05-15:/posts/permute.html</id>
		<content type="html">
      &lt;p&gt;A few years ago, while aboard a ferry crossing the Cook Strait, my wife and I&#xA;were working on some math puzzles and I found myself needing to generate all&#xA;permutations of a sequence.&lt;/p&gt;&#xA;&lt;p&gt;Generating permutations is a common task in Project Euler-style puzzles or&#xA;interview questions; it&amp;rsquo;s a finger exercise for writing a recursive function.&#xA;This time, though, I decided to write a package for generating permutations in a&#xA;more general-purpose way. A little Googling led me to &lt;a href=&#34;https://en.wikipedia.org/wiki/Heap%27s_algorithm&#34;&gt;Heap&amp;rsquo;s&#xA;algorithm&lt;/a&gt; which gives an&#xA;iterative approach for generating permutations one swap at a time.&lt;/p&gt;&#xA;&lt;p&gt;I took inspiration from two standard library APIs:&#xA;&lt;a href=&#34;https://golang.org/pkg/sort/&#34;&gt;sort&lt;/a&gt; and&#xA;&lt;a href=&#34;https://golang.org/pkg/bufio/#Scanner&#34;&gt;bufio.Scanner&lt;/a&gt;. The result is&#xA;&lt;a href=&#34;https://github.com/cespare/permute&#34;&gt;github.com/cespare/permute&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The permute package is centered around &lt;code&gt;permute.Interface&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;type Interface interface {&#xA;&#x9;Len() int&#xA;&#x9;Swap(i, j int)&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;code&gt;permute.Interface&lt;/code&gt; is a subset of&#xA;&lt;a href=&#34;https://golang.org/pkg/sort/#Interface&#34;&gt;&lt;code&gt;sort.Interface&lt;/code&gt;&lt;/a&gt;: it doesn&amp;rsquo;t have the&#xA;&lt;code&gt;Less&lt;/code&gt; comparison method.&lt;/p&gt;&#xA;&lt;p&gt;Once you&amp;rsquo;ve got a type that satisfies this two-method interface, you can&#xA;generate permutations in place:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;package main&#xA;&#xA;import (&#xA;        &amp;quot;fmt&amp;quot;&#xA;&#xA;        &amp;quot;github.com/cespare/permute&amp;quot;&#xA;)&#xA;&#xA;type Bytes []byte&#xA;&#xA;func (b Bytes) Len() int      { return len(b) }&#xA;func (b Bytes) Swap(i, j int) { b[i], b[j] = b[j], b[i] }&#xA;&#xA;func main() {&#xA;        b := Bytes{&#39;A&#39;, &#39;B&#39;, &#39;C&#39;}&#xA;        p := permute.New(b)&#xA;        for p.Permute() {&#xA;                fmt.Println(string(b))&#xA;        }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Here&amp;rsquo;s the output:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;ABC&#xA;BAC&#xA;CAB&#xA;ACB&#xA;BCA&#xA;CBA&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Like the sort package, permute provides a few convenience functions for&#xA;commonly-permuted types:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;func Ints(s []int) *Permuter&#xA;func Strings(s []string) *Permuter&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;And that&amp;rsquo;s it! (You can also &lt;a href=&#34;https://godoc.org/github.com/cespare/permute&#34;&gt;consult the&#xA;documentation&lt;/a&gt;.) I&amp;rsquo;ve only ever&#xA;used permute once or twice, but it&amp;rsquo;s a cute little package. Try permuting&#xA;something with it today! :)&lt;/p&gt;&#xA;
		</content>
	</entry>
  
	<entry>
		<title>Blog v2</title>
		<link href="https://ctrl-c.us/posts/blogv2" />
		<updated>2018-04-15T00:00:00Z</updated>
		<id>tag:ctrl-c.us,2018-04-15:/posts/blogv2.html</id>
		<content type="html">
      &lt;p&gt;After something like a 5-year hiatus, ctrl-c.us is back! In that time, I:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Spent hours staring at Google Fonts&lt;/li&gt;&#xA;&lt;li&gt;Tried out a bunch of different color schemes&lt;/li&gt;&#xA;&lt;li&gt;Used each new popular static site generator&lt;/li&gt;&#xA;&lt;li&gt;Implemented my own static site generator in Ruby&lt;/li&gt;&#xA;&lt;li&gt;Threw out my Ruby static site generator and wrote one in Go&lt;/li&gt;&#xA;&lt;li&gt;Rewrote my Go site generator two or three times&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;At some point, though, you have to set down vim, stop tweaking CSS, and actually&#xA;serve some HTTP requests.&lt;/p&gt;&#xA;&lt;p&gt;I haven&amp;rsquo;t done much web dev stuff since I left my previous job in 2013, and in&#xA;the course of rebuilding my site I came across a few neat things.&lt;/p&gt;&#xA;&lt;h1&gt;Vanilla CSS&lt;/h1&gt;&#xA;&lt;p&gt;When I was writing a lot of web apps in the early 2010s I used CoffeeScript and&#xA;Sass.&lt;/p&gt;&#xA;&lt;p&gt;These days it seems like a reasonable choice to use modern JavaScript (ES6), and&#xA;if you have the luxury of only targeting recent browsers you might not even need&#xA;to compile it to ES5. With CSS, though, it seems like most folks are still using&#xA;preprocessors to paper over shortcomings in the language.&lt;/p&gt;&#xA;&lt;p&gt;Every few years I check in and see how viable it is to write vanilla CSS.&#xA;Happily, for this project, the time has arrived. This is thanks to a few CSS&#xA;features.&lt;/p&gt;&#xA;&lt;h2&gt;Variables&lt;/h2&gt;&#xA;&lt;p&gt;CSS variables&amp;mdash;technically, &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/CSS/--*&#34;&gt;&lt;em&gt;custom properties&lt;/em&gt;&lt;/a&gt;&amp;mdash;are finally available for factoring out repeated properties and avoiding repetition.&lt;/p&gt;&#xA;&lt;p&gt;To me, variables always seemed like the biggest missing feature of CSS and they&#xA;were the number one reason I used preprocessors. It&amp;rsquo;s wonderful to have them&#xA;directly in the language.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;header {&#xA;  --dark-purple: #664e87;&#xA;  color: var(--dark-purple);&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This syntax is cumbersome compared with preprocessor variables. On the other&#xA;hand, custom properties are part of CSS in a deep way that preprocessor&#xA;constructs cannot be. &lt;a href=&#34;https://css-tricks.com/difference-between-types-of-css-variables/&#34;&gt;This&#xA;article&lt;/a&gt; over&#xA;on css-tricks.com is a good comparison.&lt;/p&gt;&#xA;&lt;h2&gt;Calculations&lt;/h2&gt;&#xA;&lt;p&gt;The &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/CSS/calc&#34;&gt;calc&lt;/a&gt; function lets&#xA;you perform a variety of computations in your CSS. It&amp;rsquo;s another feature that&#xA;I&amp;rsquo;ve wanted ever since I started using CSS fifteen years ago.&lt;/p&gt;&#xA;&lt;p&gt;One way that I&amp;rsquo;ve put &lt;code&gt;calc&lt;/code&gt; to use is in conjunction with CSS variables. Once&#xA;you have a &lt;code&gt;--baseline&lt;/code&gt; property, it&amp;rsquo;s great to be able to write&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;margin-top: calc(2 * var(--baseline));&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Of course, that feature is available in preprocessors. Where &lt;code&gt;calc&lt;/code&gt; really&#xA;shines is dynamic calculations like &lt;code&gt;calc(100% - 30px)&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h2&gt;Flexbox&lt;/h2&gt;&#xA;&lt;p&gt;It took &lt;a href=&#34;https://stackoverflow.com/questions/16280040/css3-flexbox-display-box-vs-flexbox-vs-flex/16280118#16280118&#34;&gt;a few&#xA;revisions&lt;/a&gt;&#xA;to get there, but the latest flexbox standard is now quite stable and widely&#xA;implemented.&lt;/p&gt;&#xA;&lt;p&gt;Before flexbox, some seemingly-simple layouts required arcane hacks to&#xA;implement. Now, with flexbox, it&amp;rsquo;s usually quite easy, at least for layouts that&#xA;fit the flexbox model (which is a lot of them).&lt;/p&gt;&#xA;&lt;h2&gt;What&amp;rsquo;s missing&lt;/h2&gt;&#xA;&lt;p&gt;The preprocessor feature that I miss the most in vanilla CSS today is nesting.&#xA;This is helpful for keeping code organized. Without nesting, I find myself&#xA;repeating the same outer selector over and over.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://twitter.com/tabatkins?lang=en&#34;&gt;Tab Atkins&lt;/a&gt; of Google has &lt;a href=&#34;https://tabatkins.github.io/specs/css-nesting/&#34;&gt;a spec&#xA;proposal&lt;/a&gt; for nesting. I don&amp;rsquo;t&#xA;know enough about the CSS Working Group to gauge this proposal&amp;rsquo;s chances of&#xA;graduating to an official proposal or being accepted after that.&lt;/p&gt;&#xA;&lt;h1&gt;Chrome goodies&lt;/h1&gt;&#xA;&lt;p&gt;I use Chrome as my development browser. It&amp;rsquo;s always fun to see the new features&#xA;whenever I go back to the developer console after having not used it for a&#xA;while.&lt;/p&gt;&#xA;&lt;p&gt;Here are a few goodies that I noticed this time around (though I don&amp;rsquo;t know how&#xA;new they are).&lt;/p&gt;&#xA;&lt;p&gt;When a style sheet includes a commented-out line, then for that element, the&#xA;inspector shows the property as crossed-out, allowing you to easily toggle it on&#xA;and off:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;nav {&#xA;  /* color: green; */&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;&lt;img src=&#34;/img/green.dgxm4acq7P.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;When I&amp;rsquo;m customizing styles for various viewport sizes, it&amp;rsquo;s a nice touch that&#xA;resizing the page with the developer console open displays the size in the&#xA;top-right corner.&lt;/p&gt;&#xA;&lt;p&gt;The device toolbar takes this to the next level. My favorite detail is the ruler&#xA;at the top which shows the bounds of media queries so you can see which ones are&#xA;active as you resize the viewport, or click to select a boundary size.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;/img/devicetoolbar.eYYvsKn1q5.png&#34; alt=&#34;&#34;&gt;&lt;/p&gt;&#xA;&lt;h1&gt;Let&amp;rsquo;s Encrypt&lt;/h1&gt;&#xA;&lt;p&gt;I used &lt;a href=&#34;https://letsencrypt.org&#34;&gt;Let&amp;rsquo;s Encrypt&lt;/a&gt; for the first time in this&#xA;project, and my experience was excellent.&lt;/p&gt;&#xA;&lt;p&gt;The key idea of Let&amp;rsquo;s Encrypt seems to be this: instead of buying a new&#xA;certificate every few years using some manual process, provision a free&#xA;certificate automatically every couple of months. You do this using a clever&#xA;protocol,&#xA;&lt;a href=&#34;https://en.wikipedia.org/wiki/Automated_Certificate_Management_Environment&#34;&gt;ACME&lt;/a&gt;,&#xA;which has a few different methods for proving ownership of the domain.&lt;/p&gt;&#xA;&lt;p&gt;Not only is this process a huge improvement on the status quo, but it has a&#xA;wonderful side effect: the more certificates that Let&amp;rsquo;s Encrypt issues, the more&#xA;it cuts into the revenue of the existing certificate authorities and resellers,&#xA;firms which&amp;mdash;despite ostensibly being in infosec business&amp;mdash;regularly&#xA;reveal themselves to be shockingly (or criminally) incompetent&#xA;&lt;a href=&#34;https://en.wikipedia.org/wiki/DigiNotar&#34;&gt;again&lt;/a&gt; and&#xA;&lt;a href=&#34;https://security.googleblog.com/2017/09/chromes-plan-to-distrust-symantec.html&#34;&gt;again&lt;/a&gt;&#xA;and&#xA;&lt;a href=&#34;https://arstechnica.com/information-technology/2018/03/23000-https-certificates-axed-after-ceo-e-mails-private-keys/&#34;&gt;again&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;p&gt;In conclusion: Let&amp;rsquo;s Encrypt is great, and you should use it too!&lt;/p&gt;&#xA;
		</content>
	</entry>
  
	<entry>
		<title>‘yield’ in Python and Ruby</title>
		<link href="https://ctrl-c.us/posts/yield" />
		<updated>2010-10-08T00:00:00Z</updated>
		<id>tag:ctrl-c.us,2010-10-08:/posts/yield.html</id>
		<content type="html">
      &lt;p&gt;&lt;em&gt;This post was updated in April 2018.&lt;/em&gt;&lt;/p&gt;&#xA;&lt;p&gt;A couple of days ago I was working on some Ruby code when I found myself wishing&#xA;I had Python&amp;rsquo;s &lt;code&gt;yield&lt;/code&gt; keyword. (If you&amp;rsquo;re a user of both languages, you may be&#xA;aware that &lt;code&gt;yield&lt;/code&gt; is quite different in Ruby and Python, though not unrelated.)&#xA;This led me to try to characterize the relationship between the two &lt;code&gt;yield&lt;/code&gt;s.&lt;/p&gt;&#xA;&lt;p&gt;In a nutshell, Ruby&amp;rsquo;s &lt;code&gt;yield&lt;/code&gt; is used when a function operates over a block, and&#xA;is used to pass values into the block. Python&amp;rsquo;s &lt;code&gt;yield&lt;/code&gt; actually yields control&#xA;(and a value) to the caller while saving the state to be resumed the next time&#xA;we enter the function. This is why iterators written with &lt;code&gt;yield&lt;/code&gt; in Ruby are&#xA;sometimes called &lt;em&gt;internal iterators&lt;/em&gt;, while Python&amp;rsquo;s iterators are &lt;em&gt;external&#xA;iterators&lt;/em&gt;.&lt;/p&gt;&#xA;&lt;p&gt;It is interesting to consider the following question: how can we approximate&#xA;Ruby-style internal iterators in Python, and vice versa? Furthermore, can we&#xA;write functions in each language to take a &amp;ldquo;natural&amp;rdquo; iterator and convert it to&#xA;its equivalent approximated version?&lt;/p&gt;&#xA;&lt;h1&gt;Ruby Iterators&lt;/h1&gt;&#xA;&lt;p&gt;In Ruby, &lt;code&gt;yield&lt;/code&gt; is used to pass function arguments into a block. It is normally&#xA;used in concert with the block syntax for anonymous functions.&lt;/p&gt;&#xA;&lt;p&gt;For example, &lt;code&gt;Array&lt;/code&gt;s have an &lt;code&gt;each&lt;/code&gt; method which performs computations&#xA;sequentially on each element:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;[1,2,3].each { |x| puts x } # prints 1, 2, and 3 on separate lines&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;This could be implemented with &lt;code&gt;yield&lt;/code&gt; as follows:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;class Array&#xA;  def each2&#xA;    i = 0&#xA;    while i &amp;lt; self.size&#xA;      yield self[i]&#xA;      i += 1&#xA;    end&#xA;  end&#xA;end&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;In this example, &lt;code&gt;yield&lt;/code&gt; passes in the elements of the array as arguments to the&#xA;block one at a time. If we wanted to implement something like this &lt;code&gt;yield&lt;/code&gt;, we&#xA;would need an implicit anonymous function as some kind of argument to the&#xA;iterator. Then &lt;code&gt;yield&lt;/code&gt; would translate to a call to that function. Consider the&#xA;following low-sugar translation:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;class Array&#xA;  def each2(&amp;amp;block)&#xA;    i = 0&#xA;    while i &amp;lt; self.size&#xA;      block.call(self[i])&#xA;      i += 1&#xA;    end&#xA;  end&#xA;end&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h1&gt;Python Iterators&lt;/h1&gt;&#xA;&lt;p&gt;Python&amp;rsquo;s &lt;code&gt;yield&lt;/code&gt; is also a core part of idiomatic Python iteration, because it&#xA;defines a generator. A generator exhibits behavior such as the following:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;def my_generator():&#xA;  yield 1&#xA;  yield 2&#xA;  yield 3&#xA;&#xA;g = my_generator()&#xA;print(next(g)) # prints &amp;quot;1&amp;quot;&#xA;print(next(g)) # prints &amp;quot;2&amp;quot;&#xA;print(next(g)) # prints &amp;quot;3&amp;quot;&#xA;print(next(g)) # throws StopIteration&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;As this example shows, &lt;code&gt;yield&lt;/code&gt; suspends the function and returns to the caller&#xA;with the value it was passed. The next time we enter the generator, execution&#xA;resumes where it left off. This behavior is more complex than the internal&#xA;iteration in Ruby. Suspending the execution state will make this more difficult&#xA;to emulate.&lt;/p&gt;&#xA;&lt;p&gt;(I&amp;rsquo;m ignoring the fact that &lt;code&gt;yield&lt;/code&gt; is an expression in Python and, as such,&#xA;allows the implementation of arbitrary coroutines. See David Beazley&amp;rsquo;s &lt;a href=&#34;http://www.dabeaz.com/coroutines/&#34;&gt;A&#xA;Curious Course on Coroutines and Concurrency&lt;/a&gt;&#xA;for a look at the sorts of things you can do with coroutines in Python.)&lt;/p&gt;&#xA;&lt;p&gt;Keep in mind that generators are normally used with Python&amp;rsquo;s &lt;code&gt;for&lt;/code&gt; statement and&#xA;other looping constructs:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;g = my_generator()&#xA;for value in g:&#xA;  print(value) # prints &amp;quot;1&amp;quot;, &amp;quot;2&amp;quot;, and &amp;quot;3&amp;quot; on separate lines&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;h1&gt;Bridging the Gap&lt;/h1&gt;&#xA;&lt;p&gt;Now, how would we do the translation between these types of iterators? As a&#xA;disclaimer, I should warn you that the rest of the code in the post is Bad&#xA;Form™: Ruby idioms are best used in Ruby and Python-isms are best used in&#xA;Python.&lt;/p&gt;&#xA;&lt;p&gt;Let&amp;rsquo;s start with the easier case: translating an (outer) Python iterator (a&#xA;generator) into a Ruby-style iterator. To formalize our goal for this&#xA;approximation, let&amp;rsquo;s say that we want a function &lt;code&gt;internalize&lt;/code&gt; which takes a&#xA;generator &lt;code&gt;g&lt;/code&gt; and returns a function&amp;mdash;the internal iterator&amp;mdash;which accepts&#xA;another function &lt;code&gt;f&lt;/code&gt; as an argument and calls &lt;code&gt;f&lt;/code&gt; with each value generated by&#xA;&lt;code&gt;g&lt;/code&gt;. Here&amp;rsquo;s an example usage:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;def simple_generator():&#xA;  arr = [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&#xA;  for letter in arr:&#xA;    yield letter&#xA;&#xA;external = simple_generator()&#xA;internal = internalize(external)&#xA;internal(print) # Prints &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, and &amp;quot;c&amp;quot; on separate lines&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;(I&amp;rsquo;m using Python 3 here.)&lt;/p&gt;&#xA;&lt;p&gt;To write &lt;code&gt;internalize&lt;/code&gt;, we need a function which calls the input function with&#xA;all of its generated values:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;def internalize(g):&#xA;  def internal(f):&#xA;    for v in g:&#xA;      f(v)&#xA;  return internal&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Unfortunately, Python&amp;rsquo;s &lt;code&gt;lambda&lt;/code&gt; expressions are a weak form of anonymous&#xA;functions and aren&amp;rsquo;t as general as Ruby blocks, so this result won&amp;rsquo;t be very&#xA;usable.&lt;/p&gt;&#xA;&lt;p&gt;How do we go the other way? This time, let&amp;rsquo;s try to write a Ruby function&#xA;&lt;code&gt;externalize&lt;/code&gt; which takes a regular iterator method and creates a function&#xA;which sequentially produces the iterator&amp;rsquo;s values as we call it. We could use&#xA;this function as follows:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;internal = [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;].method(:each)&#xA;external = externalize(internal)&#xA;&#xA;p external.call # &amp;quot;a&amp;quot;&#xA;p external.call # &amp;quot;b&amp;quot;&#xA;p external.call # &amp;quot;c&amp;quot;&#xA;p external.call # throws StopIteration&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;The trick to implementing this is how we save the state in the middle of&#xA;iterating such that the next call to the outer function will jump back to that&#xA;state. We can save the state by storing a&#xA;&lt;a href=&#34;https://en.wikipedia.org/wiki/Continuation&#34;&gt;continuation&lt;/a&gt; before we return, so&#xA;this will be a job for &lt;code&gt;callcc&lt;/code&gt;. In fact, my solution is somewhat convoluted and&#xA;involves a pair of continuations:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;def externalize(iter)&#xA;  callcc do |@continuation1|&#xA;    return Proc.new do&#xA;      callcc { |@continuation2| @continuation1.call }&#xA;    end&#xA;  end&#xA;&#xA;  iter.call do |v|&#xA;    callcc { |@continuation1| @continuation2.call(v) }&#xA;  end&#xA;&#xA;  throw &amp;quot;StopIteration&amp;quot;&#xA;end&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;If you&amp;rsquo;ve never used it before, &lt;code&gt;callcc&lt;/code&gt; creates a continuation, passes it to&#xA;its body, and returns the value with which the continuation is called. Hence,&#xA;each entry into the &lt;code&gt;Proc&lt;/code&gt; is bounced into the current level of iteration of&#xA;&lt;code&gt;iter&lt;/code&gt;. Each iteration causes the value to be returned through both&#xA;continuations and out of the &lt;code&gt;Proc&lt;/code&gt;, and also updates both continuations such&#xA;that the next call to the &lt;code&gt;Proc&lt;/code&gt; resumes at the same place. Try tracing the&#xA;execution&amp;mdash;it&amp;rsquo;s fairly tricky. I&amp;rsquo;d be interested to know if there&amp;rsquo;s an easier&#xA;way of accomplishing this.&lt;/p&gt;&#xA;&lt;h1&gt;Improvements&lt;/h1&gt;&#xA;&lt;p&gt;Both of these examples are just rough demonstrations of the concepts, but we can&#xA;make them a bit nicer. The iterators should be able to support processing&#xA;multiple values at once, so our translations should preserve that feature. The&#xA;Python case is simple: add a star for array expansion. Also, our conversion&#xA;function is begging to be turned into a decorator:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;def internalize(generator_fn):&#xA;  g = generator_fn()&#xA;  def internal(f):&#xA;    for v in g:&#xA;      f(*v)&#xA;  return internal&#xA;&#xA;@internalize&#xA;def internal():&#xA;  arr = [(&amp;quot;a&amp;quot;, &amp;quot;z&amp;quot;), (&amp;quot;b&amp;quot;, &amp;quot;y&amp;quot;), (&amp;quot;c&amp;quot;, &amp;quot;x&amp;quot;)]&#xA;  for letter in arr:&#xA;    yield letter&#xA;&#xA;# Prints &amp;quot;a z&amp;quot;, &amp;quot;b y&amp;quot;, and &amp;quot;c x&amp;quot; on separate lines&#xA;internal(lambda v1, v2: print(&amp;quot;%s %s&amp;quot; % (v1, v2)))&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;In Ruby, it&amp;rsquo;s not quite as pretty: we can do similar array expansion when we&#xA;call the iterator so that we can at least transform iterators with multiple&#xA;parameters, but then the result will yield single-element arrays if there is&#xA;only one block parameter. Ruby folks seem to enjoy monkey-patching, so let&amp;rsquo;s add&#xA;the transformation directly to the &lt;code&gt;Method&lt;/code&gt; class.&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;class Method&#xA;  def externalize&#xA;    callcc do |@continuation1|&#xA;      return Proc.new do&#xA;        callcc { |@continuation2| @continuation1.call }&#xA;      end&#xA;    end&#xA;&#xA;    self.call do |v|&#xA;      callcc { |@continuation1| @continuation2.call(*v) }&#xA;    end&#xA;&#xA;    throw &amp;quot;StopIteration&amp;quot;&#xA;  end&#xA;end&#xA;&#xA;h = { &amp;quot;a&amp;quot; =&amp;gt; &amp;quot;z&amp;quot;, &amp;quot;b&amp;quot; =&amp;gt; &amp;quot;y&amp;quot;, &amp;quot;c&amp;quot; =&amp;gt; &amp;quot;x&amp;quot; }&#xA;external = h.method(:each).externalize&#xA;&#xA;p external.call # [&amp;quot;a&amp;quot;, &amp;quot;z&amp;quot;]&#xA;p external.call # [&amp;quot;b&amp;quot;, &amp;quot;y&amp;quot;]&#xA;p external.call # [&amp;quot;c&amp;quot;, &amp;quot;x&amp;quot;]&#xA;p external.call # throws StopIteration&#xA;&lt;/code&gt;&lt;/pre&gt;&#xA;&lt;p&gt;Although internal iterators aren&amp;rsquo;t all that useful in Python, there are&#xA;legitimate reasons to use generators in Ruby and there is an implementation for&#xA;creating generators in the Ruby&#xA;&lt;a href=&#34;https://ruby-doc.org/stdlib-1.8.7/libdoc/generator/rdoc/Generator.html&#34;&gt;Generator&lt;/a&gt;&#xA;class.&lt;/p&gt;&#xA;&lt;p&gt;&lt;em&gt;Since I wrote this post in 2010, external iteration has gained first-class&#xA;support in Ruby with the addition of&#xA;&lt;a href=&#34;https://ruby-doc.org/core-2.5.0/Enumerator.html&#34;&gt;Enumerator&lt;/a&gt;s. These form the&#xA;basis of modern Ruby&amp;rsquo;s lazy iteration features.&lt;/em&gt;&lt;/p&gt;&#xA;
		</content>
	</entry>
  
</feed>
