iRihttp://www.jerf.org/iri/Tagline pending.en-usFri, 24 Jan 2014 01:31:40 -0000 The Environment Object Pattern in Go http://www.jerf.org/iri/post/2929 <p>One of the things I've been really enjoying about Go is how easy testing is. The pervasive use of interfaces and composition-instead-of-inheritance synergize nicely for testing. But as I've expressed this online on reddit and Hacker News a couple of times, I've found that this does not seem to be a universally-shared opinion. Some have even commented on how hard it is to test in Go.</p> <p>Since we are all obviously using the same language, the difference must lie in coding behavior. I've internalized a lot of testing methodology over the years, and I find some of the things work even better in Go that most other imperative languages. Let me share one of my core tricks today, which I will call the Environment Object pattern, and why Go makes it incrementally easier to use than other similar (imperative) environments.</p> <p><a href="http://www.jerf.org/iri/post/2929">Read the rest...</a></p> jerf@jerf.org (jerf)Fri, 24 Jan 2014 01:31:40 -0000http://www.jerf.org/iri/post/2929ProgrammingGolang So you want to write a Monad tutorial in Not-Haskell... http://www.jerf.org/iri/post/2928 <p>There are a number of errors made in putative Monad tutorials in languages other than Haskell. Any implementation of monadic computations should be able to implement the equivalent of the following in Haskell:</p> <pre>minimal :: Bool -&gt; [(Int, String)] minimal b = do x &lt;- if b then [1, 2] else [3, 4] if x `mod` 2 == 0 then do y &lt;- ["a", "b"] return (x, y) else do y &lt;- ["y", "z"] return (x, y)</pre> <p>This should yield the local equivalent of:</p> <pre>Prelude&gt; minimal True [(1,"y"),(1,"z"),(2,"a"),(2,"b")] Prelude&gt; minimal False [(3,"y"),(3,"z"),(4,"a"),(4,"b")]</pre> <p>At the risk of being offensive, you, ahhh... really ought to understand <i>why</i> that's the result too, without too much effort... or you really shouldn't be writing a Monad tutorial. <i>Ahem.</i></p> <p>In particular:</p> <ul><li>Many putative monadic computation solutions only work with a "container" that contains zero or one elements, and therefore do not work on lists. <tt>&gt;&gt;=</tt> is allowed to call its second argument (<tt>a -&gt; m b</tt>) an <i>arbitrary</i> number of times. It may be once, it may be dozens, it may be none. If you can't do that, you don't have a monadic computation.</li> <li>A monadic computation has the ability to examine the intermediate results of the computation, and make decisions, as shown by the if statement. If you can't do that, you don't have a monadic computation.</li> <li>In statically-typed languages, the type of the inner value is not fixed at the beginning. It's <tt>a -&gt; m b</tt>, not <tt>a -&gt; m a</tt>, which is quite different. Note how <tt>x</tt> and <tt>y</tt> are of different types.</li> <li>The monadic computation builds up a namespace as it goes along; note we determine <tt>x</tt>, then somewhat later use it in the <tt>return</tt>, regardless of which branch we go down, and in both cases, we do not use it right away. Many putative implementations end up with a pipeline, where each stage can use the previous stage's values, but can not refer back to values before that.</li></ul> <p>A common misconception is that you can implement this in Javascript or similar languages using "method chaining". I do not believe this is possible; for monadic computations to work in Javascript at all, you <i>must</i> be nesting functions within calls to bind within functions within calls to bind... basically, it's impossibly inconvenient to use monadic computations in Javascript, and a number of other languages. A mere implementation of method chaining is not "monadic", and libraries that use method chaining are not "monadic" (unless they really do implement the rest of what it takes to be a monad, but I've so far never seen one).</p> <p>If you <i>can</i> translate the above code correctly, and obtain the correct result, I don't guarantee that you have a proper monadic computation, but if you've got a bind or a join function with the right type signatures, and you can do the above, you're probably at least on the right track. This is the approximately minimal example that a putative implementation of a monadic computation ought to be able to do.</p> jerf@jerf.org (jerf)Fri, 17 Jan 2014 19:47:56 -0000http://www.jerf.org/iri/post/2928ProgrammingHaskell Scientists (and, in my experience, especially bioinformaticians) tend to make horrible, awful messes no matter ... http://www.jerf.org/iri/post/2927 <blockquote style='clear: both'>Scientists (and, in my experience, especially bioinformaticians) tend to make horrible, awful messes no matter how maintainable you think a language is. (You can hand them Inform 7 and it'll still end up looking like Fortran ate the csh manual and vomited all over an APL keyboard.)<div style='text-align: right'>-- <a href='https://news.ycombinator.com/item?id=7059804'>chromatic on HN</a></div></blockquote> jerf@jerf.org (jerf)Wed, 15 Jan 2014 15:33:43 -0000http://www.jerf.org/iri/post/2927Programming I don&#39;t listen to the radio hardly at all anymore. Recently, I was with my ... http://www.jerf.org/iri/post/2926 <p>I don't listen to the radio hardly at all anymore. Recently, I was with my wife while she was just idly flipping through, and I was astounded.</p> <p>Rappers? Autotuned.</p> <p>Rockers? Autotuned.</p> <p>Country? Autotuned.</p> <p>The electro/techno stuff was autotuned, but that's less of a surprise.</p> <p>Autotune, <i>autotune</i>, <b><i>AUTOTUNE!</i></b></p> <p>Not even subtly, either, but cranked up as far as it will go before the high end simply explodes with noise.</p> <p>Is there anyone left in the music industry that can <i>carry a tune</i>?</p> jerf@jerf.org (jerf)Tue, 31 Dec 2013 17:52:46 -0000http://www.jerf.org/iri/post/2926Bloviation I&#39;ve pushed two repos to GitHub with Go code: gomempool (godoc): A []byte pool manager ... http://www.jerf.org/iri/post/2925 <p>I've pushed two repos to GitHub with Go code:</p> <ul><li><a href='https://github.com/thejerf/gomempool'>gomempool</a> (<a href='http://godoc.org/github.com/thejerf/gomempool'>godoc</a>): A <tt>[]byte</tt> pool manager for Go. It's less generic than the Pool implementation that is working its way into Go tip, but also therefore understands more about <tt>[]byte</tt>s, and is much simpler than the I-don't-even-know-what magic is in that implementation. It also tracks stats, which I've hooked up to my monitoring so I can see the usefulness of the pool in my real running code.</li> <li><a href='https://github.com/thejerf/abtime'>abtime</a> (<a href='http://godoc.org/github.com/thejerf/abtime'>godoc</a>): An abstract time library that removes your dependency on the OS time from the time module. I've now run into this problem at work in three forms; unfortunately one of them is in a module I plan on releasing someday and don't want a dependency on this module, but the other two can benefit from a standardized way of dealing with this. I had a semi-complete version of this in my local code base already, but I was inspired to bring it up to public spec by <a href='http://blog.plover.com/prog/Moonpig.html#testing-sucks'>Moonpig</a>.</li></ul> <p>Both libraries have 100% test coverage, and are <a href='https://github.com/golang/lint'>golint</a> and <a href='http://godoc.org/code.google.com/p/go.tools/cmd/vet'>go vet</a> clean.</p> <p>There's more coming, but they're bigger than these. My supervisor tree library is nearly ready to go, but the requisite blog post explaining the design is still in progress. (It's big.) The library that implements Erlang-style mailboxes, including the ability to cluster together machines and send messages across the cluster the way Erlang does, is still in progress. (I have just barely started the clustering.) My current plan is to lock down and stabilize the system this is being written for in the single-server case, then implement this clustering.</p> <p>I wouldn't mention it, except that Google does not seem inclined to index these without some form of external link. GitHub permits external indexing of the master branch of projects but I think perhaps their robots.txt forbids the scraping necessary to find unlinked projects. Hopefully this fixes that.</p> jerf@jerf.org (jerf)Fri, 20 Dec 2013 03:21:52 -0000http://www.jerf.org/iri/post/2925ProgrammingGolang