• An Intro to Lens With No Theory

    Control.Lens is an awesome Haskell library by Edward Kmett that makes working with complex data-types a joy. That might sound boring, but consider updating a deeply nested value. Which of the following do you prefer?

    Motivation

    -- With pattern matching
    updateZ :: Alpha -> (Int -> Int) -> Alpha
    updateZ a@Alpha{beta=b@Beta{gamma=g@Gamma{z=old}}} f = a{beta=b{gamma=g{z=f old}}}
    
    -- With field accessors
    updateZ' :: Alpha -> (Int -> Int) -> Alpha
    updateZ' a f = a{beta=b{gamma=g{z=new}}}
                    where b = beta a
                          g = gamma b
                          new = f $ z $ gamma $ beta a
    
    -- With Control.Lens
    -- There's some cheating going on, this is just here to show the difference.
    -- See below for complete examples.
    updateZ'' :: Alpha -> (Int -> Int) -> Alpha
    updateZ'' a f = a&beta.gamma.z %~ f
    

    If you picked number three, read on. First, here are two programs for actually running the above examples, so you can poke at them. Then we'll examine parts of the tutorial at lens.github.io (which is totally great), look at some generalizations and finish with some extra coolness. You can also find some useful links at the bottom.

  • Recurring Madness: Functional Bash

    There are some ideas that have a will of their own. All the mighty strength of these wills is focused on a single goal, with a narrow-mindedness comparable only to a surgical laser. That goal is to become reality. You know this is so because these crazy stupid ideas keep happening again and again to different people.

    One of these ideas is functional programming in bash.

  • PlayStation 3 Jack Audio Output

    Waiting for the PS4 I realized that (1) it's still a while until it arrives, (2) it'll probably be a bit expensive at first and (3) there won't be a huge number of games for it. Conclusion: had to buy a PS3, and play some things I missed.

    Having been a PC-only gamer, I had a screen and great headphones. Note: not a TV; a computer screen with no speakers built in. I asked about the video, and got a DVI-HDMI adapter. Simple. Surprise: I noticed after all else was et up that there's no jack output on the thing. There are plenty of other outputs though. If you want 5.1, you can use the HDMI or optical digital output. If you want 7.1, you need to use HDMI. I just wanted my headphones to work, so I used the simple RCA output (which supports only stereo).

  • Django REST Framework: Add id in HyperlinkedModelSerializer

    from rest_framework import serializers
    from models import MyModel
    
    
    class WithPkMixin(object):
        def get_pk_field(self, model_field):
            return self.get_field(model_field)
    
    
    class MyModelSerializer(WithPk, serializers.HyperlinkedModelSerializer):
        class Meta:
            model = MyModel
    

    If that's enough for you, glad to be of service. Some context follows, and also a way to wrap JSON top-level list responses in a JSON object.

  • Relocating a hsenv

    If you have a hsenv environment (kind of like virtualenv, except it’s for Haskell), you can easily move it to another location and use it there; all you need to do is change a few strings in a few files. Here’s how, assuming you’re in the directory containing the .hsenv folder:

        old_location=FILL_THIS
        new_location=FILL_THIS
        for file in ghc_package_path_var path_var_prependix bin/activate; do
            sed -e "s,$old_location,$new_location,g" -i .hsenv/$file
        done