Add to PATH on a mac
Next time you want to add something to PATH on a mac, do this:
sudo vi /etc/paths
The file format is actually really nice, once you know to get in there.
The useful top shortcuts
top‘s shortcut mnemonics are, to be charitable, poor. Here’s a short list of the ones I actually use.
| command | description |
|---|---|
| x | highlights the sort field |
| b | turns on highlighting, so you can see x |
| <,> | move sort column |
| R | reverse sort order |
| h | help, displaying basically this |
| q | quit |
When I start top, I immediately hit xb.
Vim as basic spreadsheet
I was about to pull out a spreadsheet to solve a problem when I thought, "Hey, I bet vim could do that."
Most of the time all I need from a spreadsheet are columns and an ability to sum them. Creating and navigating tab-separated columns aren’t a problem in vim, but what about the summing?
My solution is below. It’s a short function in .vimrc — scripted in python — that sums a list of numbers from the "yanked" register. The result is displayed, and also stuffed into the unnamed register so you can paste it where you want it.
I also included a convenience mapping that makes alt-s sum the currently selected column of numbers.
" call Sum() on current selection vmap <M-s> y:call Sum()<CR>
" sum column of numbers in yanked register, save " result to " register function! Sum() python << EOF import vim # find total from input items = vim.eval("@0").split("\n") floatsList = map(float, filter(lambda n: n != '', items)) total = sum(floatsList) # set register and echo result vim.command("call setreg('\"', '%s\n')" % total) vim.command("echo '%s saved to clipboard'" % total) EOF endfunction
My current Rubik’s Cube method
For the last six months or so I’ve been looking for a sub-30 second rubik’s cube algorithm that doesn’t involve a prohibitive amount of memorization. Specifically, my memorization metric is that I should still be able to solve the cube after, say, a two year break. To do this, the thought is to keep the number of algorithms down, and/or limit myself to algorithms that are intuitive or easy to derive.
Petrus
Take one was the Petrus method, which I have a lot of respect for. I used it for several months, but found myself getting frustrated because it seemed I spent more time turning the cube and looking for the next piece than actually solving it. Also, some of the moves were physically awkward to execute.
ZZ
Later I found the ZZ method. It focuses on ergonomic motions (which are more comfortable and lead to greater speeds), and because you don’t rotate the whole cube it’s easier to keep track of pieces. However, EOLine is something that, I’ve accepted, I’ll never be able to do at a reasonable speed.
Roux
Which brings me to the heart of my current thinking: the Roux method. It’s a kind of variation off the Petrus method, and many of the (ergonomic) techniques I learned through ZZ seem to carry over to it. It has a relatively small number of algorithms, it has a small number of quickly recognizable situations towards the end of the solve with fast solutions, and — like ZZ — you can solve it without cube rotations.
The one change I’ve made to Roux is Step 3, in which you orient and permutate four corners at once. There are 24 algorithms, and that’s too much for me.
Instead, I substitute Petrus’ Steps 5 and 6. It does the same thing, but with a 2-look philosophy. This drastically reduces the number of algorithms to learn. I’m willing to take a time hit for that. I’m not trying to set records; I’m aiming for sub-30.
Foo-bar notation
This started life as a silly idea, and it only got worse from there.
The situation
We kinda sorta had a situation at work where it would be nice to encode ASCII as only alphabetic characters (as it turned out, not really). In response, I invented an inefficient (but funny) solution to a barely existing problem. And I called it Foo-bar Notation.
The specification
It works like this. In most sensible languages, the escape character is a \. Thus you might write a string like this in javascript:
var bear = "went \"over\" the mountain";
Or, using Foo-bar Notation:
var bear = "went foodqbaroverfoodqbar the mountain";
foo starts the escape sequence, and bar ends it. A similar concept in a language like groovy might look like this:
def bear = "went ${dq}over${dq} the mountain"
Foo-bar Notation supports all the common non-alphabetic characters. Here’s a small sample:
| ASCII | Foo-bar Notation |
|---|---|
| = | eq |
| < | lt |
| > | gt |
| ( | op |
| ) | cp |
| . | dt |
| , | cm |
| $ | ds |
| ! | ex |
| & | am |
… and many more!
Special characters
Two special cases demand extra attention. The first is that foo itself needs to be escaped. Thus,
the power of foo
would need to be written
the power of foofoobar
The sequence foofoobar is pronounced "foo-foo bar".
The second is that Foo-bar Notation can optionally be written without uppercase letters (or even spaces). Letters preceeded with up will be converted to uppercase. Thus,
New York
could be rewritten as
fooupbarnewfoospbarfooupbaryork
which is clearly better.
Closing point
Though not yet tested, it is thought that Foo-bar Notation would work best with LOLCODE.
Foo It!
I should preface this by saying that this is silly.
How to Foo It!
Out of nowhere, the following vim command occurred to me today.
" foo it! command! -range=% FooIt <line1>,<line2>s;\<\w*\>;foo;g
When dropped in .vimrc and executed on a file by :FooIt, it turns this:
import re for test_string in ['555-1212', 'ILL-EGAL']: if re.match(r'^\d{3}-\d{4}$', test_string): print test_string, 'is a valid US local phone number' else: print test_string, 'rejected'
into this:
foo foo foo foo foo ['foo-foo', 'foo-foo']: foo foo.foo(foo'^\foo{foo}-\foo{foo}$', foo): foo foo, 'foo foo foo foo foo foo foo' foo: foo foo, 'foo'
thereby Fooing It!
You might think this has no practical value. You’d be right.
Some explanation
Beyond the base of this vim trick — using a command to execute a regular expression on a file — there is something tricky going on here: this command can operate on a range. The -range=% bit means that the command will operate on a range (<line1>,<line2>) if specified, or the entire file (%) otherwise.
For those who aren’t regular expression geeks, \< and \> specify the start and end of a word, \w indicates word characters, and * indicates that there can be any number of them.
The perfect breakfast recipe
Oatmeal is good for you, but it takes too long to cook and it’s hard to get the consistency right. Hence my new recipe; toss this into a blender:
- half cup dry oatmeal - 1 apple - 1 banana - half cup almonds or walnuts - 8 oz water
The result is good, and good for you. Makes an excellent breakfast: the foods have a staggered energy release, it’s quick to make, and I can always choke it down regardless of how not-hungry I feel.
Creating maps in groovy, functionally
I have a lot of respect for functional programming, and try to pull its best idioms into more imperative languages. While it is possible to build a map in groovy in a functional way, it’s not exactly obvious how.
This post looks at some of groovy’s the map-generation solutions for a trivial example, and documents the functional trick.
Method 1: bad imperative
The most simple possible solution. Does not scale. Useful only for defining hard-coded constants.
def map1 = [:] map1."a" = "A" map1."b" = "B" map1."c" = "C" map1."d" = "D" map1."e" = "E" map1."f" = "F" map1."g" = "G" println map1 // -> [a:A, b:B, c:C, d:D, e:E, f:F, g:G]
Method 2: good imperative
The imperative approach. Creates an empty map, then fills it. The work is broken up into two pieces. If the lines are separated the code will fail.
To me, this example seems ad-hoc and messy. The first line makes no sense without the context of the second.
def map2 = [:]
("a".."g").each { key ->
map2[key] = key.toUpperCase()
}
println map2 // -> [a:A, b:B, c:C, d:D, e:E, f:F, g:G]
Method 3: functional
The more functional approach. The completed map is pushed directly into the holding variable, which need never be modified. There is only one line, and it cannot easily be accidentally disturbed.
To me, this example seems self-contained and clean.
def map3 = ("a".."g").collect { key ->
[ (key): key.toUpperCase() ]
}.sum()
println map3 // -> [a:A, b:B, c:C, d:D, e:E, f:F, g:G]
Explanation
A bit of description about what’s going on here. ("a".."g") defines the list of lower-case letters. collect does what map would do in a language with sensible naming conventions (haskell), it applies the given function to each list element.
The given function, in this case, takes a key and returns a map entry. Yes, the () around key are necessary — without them groovy thinks key means "key".
After the "lower-case letters" list is transformed into the "map entries" list, sum() compresses the result into a single map.
Ubuntu’s trash location
If you’ve ever deleted something through Ubuntu’ GUI and wanted it back, here’s where it lives:
/home/<username>/.local/share/Trash/files
I was looking for this because I needed to apply some logic to what I restored.
bash’s $()
This post may only serve to showcase my poor bash skills, but I’m going to write it up anyway. I just discovered $(). Session walk-through below.
I wanted to delete all files that matched a certain pattern. First I checked that my pattern worked:
$ ls todo*
todo.html todo.log todo.pdf todo.tex todo.txt
Seeing that the return was correct, I deleted them:
$ rm $(!!)
Where $() executes the internal code, and !! expands to the previous command.
There’s some handy constructs in there. And I think this use-case is valuable — check what you’re going to delete before you delete it.