Groovy Map Minus

06 June 2017

Groovy is relatively intuitive for me: I can usually guess a method or operator and find it works as I had guessed.

Removing a key/value by the key from a Map conveniently eludes me though. I always guess that I should be able to minus a key from a Map to produce a new Map with that entry /value removed. That doesn’t work, so I needed to invent my own. I still not sure how to practically apply it everywhere though, or if it’s even worthwhile. In practice, I always end up writing the findAll inline.

@Category(Map)
class MapMinus {
    Map minus(Collection keys) {
        this.findAll { k, v -> ! (k in keys) }
    }
    Map minus(Object key) {
        this - [key]
    }
}

use (MapMinus) {
    assert [b: 2, c: 3,] == [a: 1, b: 2, c: 3,] - 'a'
    assert [b: 2, c: 3,] == [a: 1, b: 2, c: 3,] - ['a']

}


Too Many MacVims Installed

24 April 2017

I have MacVim installed by Homebrew, but MacVim still pops up sometimes to suggest an update, and brew updates it as well. After a year or 2 of this, my "Open with…​" menu was littered with about 7 different versions of MacVim dating back to version 7.4.

I figured I’d try to uninstall it, brew uninstall macvim, and see how many version I’m left. The simple uninstall confirmed that quite a few versions of MacVim were installed, and suggested the --force option to remove them all, so I did. That left mvim no longer working in the shell, and fortunately, all the MacVims had disappeared from the "Open with…​" menu as well.

Reinstalling it with brew install macvim leaves me nicely with only one MacVim now. I’ll have to see if they multiply again, but now I’ll have this note to myself on how to fix it.


Quick and Easy Groovy for the Web

22 June 2011

Groovy can be used pretty easily to spin up some simple web pages in almost the same way one would hack out some PHP or JSP without going to the trouble to do an all-out Grails project.

The Groovy Servlet allows you to pack up the groovy-all-*.jar, a simple web.xml, and whatever *.groovy scripts you want and deploy it right into Tomcat as a plain WAR file. The Groovy Servlet page

Here's a bit of a script I put together to jump start a simple Groovlet project by packaging a WAR file from a directory of scripts. This isn't Groovy Servlet code itself, but just a command-line tool. (The Groovy Servlet page linked previously has examples for writing your own servlets.) This script will copy in the Groovy JAR and generate the basic web.xml to wire up the GroovyServlet to dynamically execute your scripts. I also have a downloadable copy of package_groovlet.groovy.

#!/usr/bin/env groovy

if (args.size() < 1) {
    print """\
        |Usage: package_groovlet.groovy <war-name>
        |Package the current directory into a Groovy Servlet war.
        |""".stripMargin()
    return
}

def war = args[0]
def embed = "${System.getenv()['GROOVY_HOME']}/embeddable"

def ant = new AntBuilder()

ant.sequential {
    delete(dir: 'build')
    mkdir(dir: 'build/WEB-INF/lib')
    copy(toDir: 'build/WEB-INF/lib') {
        fileset(dir: embed) {
            include(name: 'groovy-all-*.jar')
        }
    }
    copy(toDir: 'build') {
        fileset(dir: '.') {
            exclude(name: 'build/**')
        }
    }
}

new FileOutputStream('build/WEB-INF/web.xml').withWriter { webxml ->
    webxml.print """\
        <!DOCTYPE web-app PUBLIC
          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd" >
        <web-app>
            <servlet>
                <servlet-name>Groovy</servlet-name>
                <servlet-class>groovy.servlet.GroovyServlet</servlet-class>
            </servlet>
            <servlet-mapping>
                <servlet-name>Groovy</servlet-name>
                <url-pattern>*.groovy</url-pattern>
            </servlet-mapping>
         </web-app>
     """.stripIndent()
}

ant.jar(destfile: "build/${war}", basedir: 'build')
println "Created build/${war}"


Top Posting in Mutt

27 May 2010

Outlook, Entourage, and Evolution have trained me in work environments to top-post my email replies against my curmudgeonly better judgment.

Now that I'm mostly job searching and sending lots of email from my home account, I've finally buckled to the peer pressure, and configured mutt to top-post my signature in the .muttrc:

set sig_on_top = yes

I don't even frequent those old mailing lists where someone would yell at me for top-posting, anyway. Next I'll likely just dump Mutt and go to a desktop mail client, like Thunderbird.


All the Posts

June 2017

April 2017

June 2011

May 2010

January 2010

June 2008

March 2008

December 2007

October 2007

August 2007

July 2007

June 2007

May 2007

March 2007

February 2007

January 2007

November 2006

September 2006

August 2006

July 2006

March 2006

January 2006

December 2005

October 2005

August 2005

May 2005

April 2005

March 2005

January 2005

December 2004

November 2004

October 2004

September 2004

May 2004

April 2004

March 2004

January 2004

September 2003

August 2003

July 2003