quickies
Common Lisp
SBCL come with very few built-in package.
To install additional ones, use asdf, e.g.
(require 'asdf) (require 'asdf-install) (asdf-install:install 'cl-ppcre)
will install Perl-style regular expression support. Then, when you restart SBCL, you'll need to load it with
(asdf:operate 'asdf:load-op :cl-ppcre)
To get a full-featured version of CL Hyperspec in Emacs
Look at dpans2texi.el.
Clojure
To start Clojure REPL
We need to create a small startup script, say clj
. The simplest one is something like:
#! /usr/bin/env bash java -cp /usr/local/share/clojure/clojure.jar clojure.main
The problem is that we won't benefit from command-line history, backward search, etc. For that to be effective, we need to use JLine (rlwrap
doesn't work on OS X, due to the lacking Readline library). Then, we just have to modify the above script with:
java -cp /.../jline-0.9.94.jar:/.../clojure.jar jline.ConsoleRunner clojure.main
where ...
denotes actual path to related files. Alternatively, this could be setup using CLASSPATH
, or by placing the jar file in the system-wide java extensions directory, e.g. /Library/Java/Extensions
or /System/Library/Java/Extensions
. The latter doesn't seem to be working as jline would be loaded by the extension classloader which is unaware of class path variable. Startup tips are described in http://clojure.org/getting_started, and on http://www.stackoverflow.com.
In addition to the core distribution
It is recommended to install clojure.contrib which is a set of additional features (like common mathematical operations) that are not present in the base package. To install it, just grab a binary (jar
) distribution and make it available for Java. For that purpose, we can use a custom CLASSPATH
, e.g. in my .profile
:
CLASSPATH=/usr/local/share/jars
or add an extra argument to -cp
when invoking clojure.jar
from Java.
To check that everything works well, we can use a simple script test.clj
, like this:
(ns test (:use clojure.contrib.math clojure.test)) (deftest foo (is (= 4 (sqrt 16)))) (run-tests)
that can be run under Clojure REPL. If it is ok, we should see something like:
Ran 1 tests containing 1 assertions. 0 failures, 0 errors. {:type :summary, :test 1, :pass 1, :fail 0, :error 0}
Here is another example:
(ns test (:use clojure.contrib.pprint)) (pprint (for [x (range 10)] (range x)))
A better way to get a working clj starting script on OS X
is described at http://bit.ly/eda9IS. It is possible to add as many libraries as we want (e.g., incanter).
To facilitate editing and stay within our familiar Emacs
environment, we can use slime. A specific port for Aquamacs is available for download.
There's a difference between :use and :require
JVisualVM (jvisulavm comes with Java SDK) might help seeing
how much Java memory, cpu and threads Clojure actually uses. h/t http://www.fatvat.co.uk/2009/05/jvisualvm-and-clojure.html
A good set of exercices to translate from Lisp to Clojure:
L-99 – Ninety-Nine Lisp Problems. There is also functional-koans on Github.
Getting help
(doc histogram)
brings a short description of the function with optional parameters (no pager though).
A bunch of documentation and resources is available from Mark
Volkmann's website, http://java.ociweb.com/mark/clojure.
Emacs
To activate folding mode for code blocks
M-1 C-x $
; to revert, C-x $
(See set-selective-display
)
To test whether we are running Aquamacs (or any other Emacs flavor),
try:
(when (featurep 'aquamacs) ... )
On OS X, this just has to be negated to address built-in emacs.
Use M-x describe-bindings (or C-h b) to see
all shortcuts for current buffer.
Something that is lacking in most editors
is the possibility of creating temp buffer on the fly. E.g., C-x b
(aka switch-to-buffer
) allows to switch to a temporary buffer.
Managing multiple windows is a matter of 6 commands:
C-x 1 = delete-other-windows, C-x 2 = split-window-vertically, C-x 3 = split-window-horizontally, C-x + = balance-windows, C-x o = other-window, C-x C-b = list-buffers. Use M-x describe-bindings
to view all the Buffer-menu key bindings.
To remove all distractive UI stuff, just put in your .emacs:
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1)) (if (fboundp 'tool-bar-mode) (tool-bar-mode -1)) (if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
C-c ( allows to navigate between references
e.g. equations, figures, etc. (With reftex-mode
enabled, it is C-=
.)
Spell-checking is done with ispell
Under OS X, an easy replacement of ispell
is done with aspell
. You need to populate /usr/local/lib/aspell-0.60/
with compiled dictionnaries. To check which dictionnaries are available on your system, type aspell dicts
.
A useful replacement of ispell-mode
is the interactive flyspell-mode
. To use it with LaTeX, just add in your .emacs
file:
(add-hook ‘LaTeX-mode-hook ‘flyspell-mode)
To repeat a specific pattern or character
just use C-u nn pp
, where nn
is the number of times character pp
as to be repeated. For example, C-u 78 -
put a pseudo line of length 78; it is useful to choose this length as the default fill column width. To check which one is currently defined, type fill-column
in the scratch buffer and evaluate this expression (C-x C-e
). Common values are less than 80, e.g. 68, 70, or 78.
An 'annoying' (?) feature of ESS
is that R comment (#
) are automagically tabbed to the right (40th column by default). To get ride of this, you can:
- use ## instead, which yields comments aligned to the current level of indentation;
- use ### instead, which results in comments aligned to the beginning of the line.
See http://ess.r-project.org/Manual/ess.html#Indenting. Another option is to put (setq ess-fancy-comments nil)
in your .emacs
file.
The auto-fill-mode also works with comments
Just M-q
(fill-paragraph
) on the region of interest. Another option is to use newcomment.el
which provides comment-auto-fill-only-comments
.
(require 'newcomment) (setq comment-auto-fill-only-comments 1) (setq-default auto-fill-function 'do-auto-fill)
Ido and Smex are two useful add-ons to Emacs
They help mastering buffers and provide enhanced completion.
ELPA stands for Emacs Lisp Package Archive
It provides package.el
which acts as a package manager for Emacs based on a repository of pre-packed Emacs Lisp code. http://tromey.com/elpa/
To get ride of the automatic indentation of comments in ESS mode
upon pressing the Enter key or after C-M-\
, use two or three '#'. E.g.,
# this is a comment that will be right-justified ## This one will not move
To check what a combination of keys stands for
just type C-h k
followed by the combination.
Don't forget the very handy markdown-mode
Useful keybinding for ESS/Roxygen
C-c C-o
(inside function body) generate help template
To ease the navigation across tab
use tabbar or remap F5 (not used on Air OS X), e.g.:
(global-set-key (kbd "<f5>") 'tabbar-forward-tab) (global-set-key (kbd "C-<f5>") 'tabbar-backward-tab)
To view the current buffer in Safari
just use M-x browse-url-of-buffer
To open a file as root
just C-x C-f
and type /sudo::/path/file
Interesting navigation keys
C-@ set mark C-x C-x exchange point and mark C-x n n narrow down to the specified region (between mark and point) C-x n w revert to full buffer editing M-< go to the top of the buffer M-> go to the end of the buffer C-l center buffer C-v scroll one page forward M-v scroll one page backward
Try word wrap with M-x longlines-mode.
Although Emacs highlights closing parenthesis (short blink)
you might prefer M-x show-paren-mode
To customize a given font face
just C-u C-x =
to get its name.
In dired mode, instead of pressing Enter
use a
: it won't open a new buffer for each directory you visit.
To get a nice overview of some pattern matching in a source file
M-x occur
M-I convert word to lowercase (see also M-x downcase-region).
In ESS mode,
C-c C-z
will switch to the end of ess inferior buffer.
Use M-x remember to save quick notes, referencing the current buffer.
See http://stackoverflow.com/a/8920373/420055 to correct weird characters
in Emacs term or ansi-term when using zsh. (See also http://stackoverflow.com/a/7437783/420055 for git prompt and unicode characters).
To add a specific entry to the Info index page, check that your custom
Info dir are up to date, C-h v Info-directory-list
, and use install-info
to update the Info dir (linking or moving an Info file is not enough). E.g., we can put mu4e.info
in /usr/local/share/info
and use
$ sudo install-info /usr/local/share/info/mu4e.info /usr/local/share/info/dir
Mathematica
Somes Notes on Internal Implementation
lot of useful tricks to know about how Mathematica actually handle some of its calculus. Available at http://reference.wolfram.com/mathematica/tutorial/ (SomeNotesOnInternalImplementation.html)
OS X
To boot in 64-bits, just run in a Terminal
$ sudo systemsetup -setkernelbootarchitecture x86_64
To change Dock appareance (switch from 3D to 2D theme),
$ defaults write com.apple.dock no-glass -boolean YES $ killall Dock
To revert, use -boolean NO
.
There are three configuration files that can be used to customize the shell
(default to Bash): .bashrc
, .bash_profile
, and .profile
. The first two are for non-interactive and interactive shell, respectively. The latter one is loaded when no .bash_profile
exists, but its existence will supercedes .profile
. The .bashprofile seems to be the recommended one (at least, according to Apple support). Some authors suggest to fill the .bash_profile
with something like
if [ -f ~/.bashrc ]; then source ~/.bashrc fi
so that everything goes into .bashrc
. There are also two system-wide configuration files that are located under /etc
: /etc/profile
and /etc/bashrc
.
Apple's computers don't come with a true Readline library.
Instead, libedit
is used a replacement, which might cause some troubles with applications relying on console.
If necessary, it can be installed with a few steps:
$ curl -O ftp://ftp.cwru.edu/pub/bash/readline-6.1.tar.gz $ tar xvf readline-6.1.tar.gz && cd readline-6.1 $ ./configure && make $ sudo make install
For Python, it is not really a problem as we can simply do
$ sudo easy_install readline
and use the excellent ipython
or bpython
interactive shells.
Adding colors in the Terminal can be done
in (at least) two ways. First, we can just set an alias to ls
, e.g. alias ls
'ls -G'= (=ls --color=auto=
don't under OS X, but we can use export LS_OPTIONS
'–color=auto'= instead). Another option is to use CLI colors, that is:
export CLICOLOR=1 export LSCOLORS=dxfxcxdxbxegedabagacad
The order matters: DIR, SYMLINK, SOCKET, PIPE, EXE, BLOCKSP, CHARSP, EXESUID, EXEGUID, DIRSTICKY, DIRWOSTICKY. Color codes read as follows: a = black, b = red, c = green, d = brown, e = blue, f = magenta, g = cyan, h = light gray, x = default.
Parameter expansion is useful when we want to remove file extensions
in shell scripts, e.g. ${file%.*}. More generally, ${varname%pattern} allows to remove pattern at the end of a variable. h/t Jake Hofman
${!#} gives the last command line argument
and !!
gives the arguments of the previously-executed command.
Instead of playing with diff
use comm
(or grep -xF
) to check for lines common to two files.
h/t Jake Hofman
To make a given file executable, use chmod +x or chmod 755.
About file permission
we often find root (user) and wheel or admin (group). In fact, wheel is the system administrator group in BSD, while root is the system administrator user. It is common to add sudo permissions to users in the wheel group. The choice of wheel comes from the BSD UNIX group at UC Berkeley; "big wheel" means an influential or important person.
To go back home, ⇧⌘H in the Finder.
Some useful search features in Gmail
has:attachment pdf after:2011/01/01 before:2011/01/20 has:attachment (*.ppt || *.pptx || *.pps) label:Label1 | label:Label2 (case insensitive) label:Label1 filename:pdf from:John OR from:Joe cc: bcc: subject:"match this exactly" from:John (keyword1 OR keyword2) (remove OR to match both keys) keyword1 -keyword2 (has key1 but not key2) in:anywhere (Spam and Trash excluded) is:starred
No solution for filtering large attachment, though. But see http://www.findbigmail.com or http://www.searchgmailbysize.com.
To know if you are running the 'new awk' (nawk)
just try to add a custom function in an awk script. E.g., from the gawk manual
function changeit(array, ind, nvalue) { array[ind] = nvalue } BEGIN { a[1] = 1; a[2] = 2; a[3] = 3 changeit(a, 2, "two") printf "a[1] = %s, a[2] = %s, a[3] = %s\n", a[1], a[2], a[3] }
and test it:
$ awk -f test_awk.awk
To view the latest files added to a directory
use ls
like this:
$ ls -l -t /usr/bin | tail
The -c
switch allows to view file sorted by modification date instead.
Instead of creating an animated GIF with ImageMagick
e.g.:
$ convert -delay 20 -loop 0 fig*.png spin_plot.gif
let's try to generate an SWF file thanks to the swftools
:
$ png2swf -r 2 fig*.png -o afig.swf
We can enable summarize function for text
in Sytem Preferences > Keyboard > Services
. Useful for counting words in an abstract.
mv file{,-old} will rename all file to sth like file-old
Set intersection between two files
$ sort file1 file2 | uniq -d h/t
h/t @mathieuen, 25 Mar
Use pkgutil –pkgs
to list all installed packages (with Leopard installer). Useful command include pkgutil --forget org.r-project.R.Leopard.fw.pkg
when you want to install two concurrent versions of R.
To delete specific pages in Apercu.app
select them and Shift-Command Del
.
The fmt program is a simple text formatter
which allows to insert hard line breaks in a whole file according to some fixed line width (but respecting word breaks).
To count how many rows have at least one empty field
$ grep ,, file.csv | wc -l
To show filesystems in human readable column format
and order by percentage used:
$ df -Ph | column -t | sort -n -k5
h/t climagic
To see recent files but just print the last 30 lines
$ ls -ltra | tail -n 30
h/t climagic
To delete any files over 30 days olds in /dir and below
$ find /dir -type f -a -mtime +30 -delete
h/t climagic
To indent every line by two spaces
$ ls -l | sed 's/^/ /'
h/t climagic
Simply change file extensions from ex1 to ex2
$ for f in *.ex1 ; do mv $f $( basename $f .ex1 ).ex2 ; done
Assuming you just ran an ssh command
then ping !$:s/user@//
will ping the IP itself, removing the user@ part.
h/t climagic
The new OS X Lion added a lot of UI cleaning
but the home Library
folder is not visible anymore. To bring it back in the Finder, just run
$ chflags nohidden ~/Library
To find words that end in 'da'
$ grep "da$" /usr/share/dict/words
h/t climagic
To reformat plain text file
$ fmt -ut -w 80 filename.txt
h/t climagic
To show directories containing files older than 365 days
$ find . -mtime +365 | while read -r file ; do dirname "$file" ; done | uniq
h/t climagic
To be notified by email when a job is finished
$ C-z bg ; wait %1 ; echo "done" | mail -s "done" you@example.com
h/t climagic
How much a file will be compressed using gzip?
$ gzip -v9 -c file > /dev/null
To find all files larger than a Gb and report how large each one is
$ find / -type f -size +1G | xargs ls -lh > listofbigfiles.txt
h/t rmounce
Uninstalling Applications is quite easy
but for Libraries it is often harder to tell what's been installed by a pkg. While events are logged in in /var/log/install.log
, we won't find useful information there.
Solution 1 (easy):
- Run the pkg installer
- Look into "Show Files" just after installer has been launched
Solution 2 (harder):
- In
/Library/Receipts
there is everything we need, including anInstallHistory.plist
where all packages are registered. - Otherwise, you can just look at
$ lsbom /Library/Receipts/package_name/Contents/Archive.bom
to get the list of files (with relative paths) and Contents/Info.plist
to get the root directory (IFPkgFlagDefaultLocation
).
To generate Makefile with Qt on OS X
$ qmake -project $ qmake -spec macx-g++
To know if there are current users logged via ssh
$ lsof -i :ssh
In place of *nix `find` command
we can use
$ mdfind -name "proqol scoring"
Print list of folders in home directory, sorted by size:
$ du -d 1 -h ~ | sort -n -r
If you want to free up inactive RAM
just issue at the terminal $ purge
.
See also: http://apple.stackexchange.com/q/67031.
The tmutil command is useful to control Time Machine backups from the
command-line. E.g., http://apple.stackexchange.com/a/18665.
Hold over the mouse over a word
and type Ctrl-Cmd-d
to get its definition from the built-in dictionnary.
With Numbers
there's no TRANSPOSE()
command, but we can use the following:
=INDEX(Table 1::$A$1:$A$10,COLUMN(),ROW())
The mdls utility
allows to extract any metadata from a given file, including EXIF and geolocalisation information from an image file.
Python
Nice use of itertools.groupby
for Fasta files (https://www.biostars.org/p/225913/):
from itertools import groupby def transcribe(sequence): return sequence.replace('T', 'U') def translate_rna(s): codon2aa = {"AAA":"K", "AAC":"N", "AAG":"K", "AAU":"N", "ACA":"T", "ACC":"T", "ACG":"T", "ACU":"T", "AGA":"R", "AGC":"S", "AGG":"R", "AGU":"S", "AUA":"I", "AUC":"I", "AUG":"M", "AUU":"I", "CAA":"Q", "CAC":"H", "CAG":"Q", "CAU":"H", "CCA":"P", "CCC":"P", "CCG":"P", "CCU":"P", "CGA":"R", "CGC":"R", "CGG":"R", "CGU":"R", "CUA":"L", "CUC":"L", "CUG":"L", "CUU":"L", "GAA":"E", "GAC":"D", "GAG":"E", "GAU":"D", "GCA":"A", "GCC":"A", "GCG":"A", "GCU":"A", "GGA":"G", "GGC":"G", "GGG":"G", "GGU":"G", "GUA":"V", "GUC":"V", "GUG":"V", "GUU":"V", "UAA":"_", "UAC":"Y", "UAG":"_", "UAU":"T", "UCA":"S", "UCC":"S", "UCG":"S", "UCU":"S", "UGA":"_", "UGC":"C", "UGG":"W", "UGU":"C", "UUA":"L", "UUC":"F", "UUG":"L", "UUU":"F"} l = [codon2aa.get(s[n:n+3], 'X') for n in range(0, len(s), 3)] return "".join(l) with open('dna.fasta') as h: faiter = (x[1] for x in groupby(h, lambda l: l[0] == ">")) for header in faiter: header = next(header)[1:].strip() seq = "".join(s.strip() for s in next(faiter)) print(header) print(translate_rna(transcribe(seq)))
It is possible to evaluate double inequalities in Python
>>> x = 5 True >>> 10 < x < 20 False
http://stackoverflow.com/questions/101268/hidden-features-of-python
The Python challenge is a series of puzzles
that can be solved with few lines: http://www.pythonchallenge.com
Easy formatting of date
year,month,day = time.strftime('%Y-%m-%d',time.localtime()).split('-')
h/t neilkod
Enumerate and generators
Nice trick when using enumerate
:
next(i for i,v in enumerate(l) if is_odd(v))
regex.match vs. regex.search
match
only finds a pattern at the beginning of the string; if you need to scan all the string, just use search
or regex.match("(.*?*)pattern(.*?*)")
eventually.
See also this SO post:
import random import re import string import time LENGTH = 10 LIST_SIZE = 1000000 def generate_word(): word = [random.choice(string.ascii_lowercase) for _ in range(LENGTH)] word = ''.join(word) return word wordlist = [generate_word() for _ in range(LIST_SIZE)] start = time.time() [re.search('python', word) for word in wordlist] print('search:', time.time() - start) start = time.time() [re.match('(.*?)python(.*?)', word) for word in wordlist] print('match:', time.time() - start)
R
I always create small dataframe for illustrations
X <- replicate(2, rnorm(100)) y <- X[,1]+X[,2]+rnorm(100) df <- data.frame(y=y, X=X)
Here is a one-line solution:
df <- transform(X <- as.data.frame(replicate(2, rnorm(100))), y = V1+V2+rnorm(100))
This is also a nice way to generate two uncorrelated predictors, while allowing a strong association between the outcome and each of them.
A quick and dirty way to simulate two-way ANOVA data
n <- 100 A <- gl(2, n/2, n, labels=paste("a", 1:2, sep="")) B <- gl(2, n/4, n, labels=paste("b", 1:2, sep="")) df <- data.frame(y=rnorm(n), A, B)
A good replacement to sink() for capturing output of R commands
is capture.output()
.
From the on-line help, it can even be combined to enscript
like so:
ps <- pipe("enscript -o tempout.ps","w") capture.output(example(glm), file=ps) close(ps)
conflicts(detail=TRUE) gives details about masked functions
Export plot with shaded border (like OS X screencapture utility)
R> png("grv.png"); plot(replicate(2, rnorm(100))); dev.off() $ convert grv.png \( +clone -background black -shadow \ 55x15+0+5 \) +swap -background none -layers merge +repage grv2.png
Instead of replicate, we can use rply to do something
when no return values is expected, e.g. for an animation
r_ply(10, plot(runif(50)))
To get the column number of a column given its name
better than which (colnames(d)=
"a")= or grep("^a$", colnames(df))
use match("a", names(d))
With huge data.frame, it's even better to use the fastmatch
package (use fmatch
instead of match
).
h/t Matthew Dowle, http://bit.ly/yFeDTT
To update to a new version of R while keeping older installed programs
we can use pkgutil to look for available versions on a Mac system:
$ pkgutil --packages / | grep org.r-project
Mac versions usually include Leopard.fw.*
in the installaed receipts. Then, we just have to tell the system to forget about a previous version, using e.g.,
$ sudo pkgutil --forget org.r-project.R.Leopard.fw.pkga
Use
page(d, method
"print")= to view a long data frame d
through a pager.
Racket
How to prevent void in output of list
https://stackoverflow.com/q/41135630
On the use of the #:when
clause in for/list
form.
TeX
Using texdoc mathptmx show up a complete description of current font
selection scheme for use with common PostScript fonts. Example of a nice title typesetted in Times New Roman (h/t tex.stackexchange.com, 8768) :
\documentclass{article} \usepackage[T1]{fontenc} \usepackage{mathptmx} \begin{document} \noindent\fontsize{2cm}{2.2cm}\selectfont Geometric\\ Invariant Theory \end{document}
http://www.identifont.com is useful to try to identify
a given typeface.
http://detexify.kirelabs.org is a LaTeX symbol classifier
where any symbol can be drawn by hand.
An eps figure can be included in a pdftex or pdflatex document
\usepackage[pdftex]{graphicx} \usepackage{epstopdf}
A tilde can be printed as ∼\!\! in math mode
h/t Jake Hofman
There are various spaces available in math mode
\, = thin space; \; = medium space; \ = space; \quad = quad space; \! = negative space (useful with multiple integrands).
To get a prefixed superscript on something
use {}; i.e., {}_nC_k
.
h/t Jake Hofman
tex2im is a useful replacement of LaTeXit to convert LaTeX formulas
into high resolution pixmap graphics for inclusion in text processors or presentations. http://www.nought.de/tex2im.html
If you're looking for anything to read before going to sleep
try The LATEX2ε Sources (texdoc source2e
).
To see what documents are available through texdoc
just run
$ ls `kpsewhich --var-value=TEXMFDIST`/doc/latex
h/t tex.stackexchange.com, 4358
There's a useful reference card for plain TeX available at
We can use system font with TeX (yes, TeX, not LaTeX) too
say tex_ttf.tex
is:
\font\1="Inconsolata"\1 hello \bye
Compile with:
$ xetex -no-pdf tex_ttf $ xdvipdfmx -vv tex_ttf
It is possible to use a custom background on every page
of a (La)TeX document by using the atbegshi
package.
A lot of useful explanations on LaTeX
may be found at the following address: http://nitens.org/taraborelli/latex (Dario Taraborelli).
A collection of fonts I like
Hoefler, Gentium, Fontin Sans, Myriad Pro, Minion Pro, Gill Sans, Inconsolota.
A simple rendering of the TeX logo in HTML is
T<span style
"position: relative; top: .2em">E</span>X=. A better solution is to directly embed relevant code into a CSS stylesheet, like proposed by Edward O’Connor's on his blog: http://edward.oconnor.cx/2007/08/tex-poshlet.
There is no way to pass a range of citation as [1-5]
(instead of [1,2,3,4,5]), but here is a nice solution from http://tex.stackexchange.com (Credit: Martin Scharrer):
\usepackage{pgffor} \makeatletter \newcommand*\rcite[1]{% \def\@gtempa{}% \foreach \n in {#1} {% \edef\@tempa{,bibitem\n}% \expandafter\g@addto@macro\expandafter\@gtempa \expandafter{\@tempa}% }% \edef\@gtempa{{\expandafter\@gobble\@gtempa}}% \expandafter\cite\@gtempa } \makeatother
Then, we can use things like
\rcite{3,...,10} % 3-10 \rcite{2,5,6,...,10} % 2, 5-10 \rcite{3,5,...,10} % 3, 5, 7, 9
To place all hyperlinks in dedicated footnote
\let\oldhref\href \renewcommand{\href}[2]{\footnote{\oldhref{#1}{#2}}}