Skip to content

Instantly share code, notes, and snippets.

@tachesimazzoca
tachesimazzoca / gist:3615945
Last active October 10, 2015 02:08
Set a default value for an empty string #scala
val strings = Array("Foo", "") map { str =>
Some(str).filter(_ != "").getOrElse("Bar")
}
strings.foreach(println)
@tachesimazzoca
tachesimazzoca / gist:3616882
Last active October 10, 2015 02:17
Parse a time string #scala
val timeParser: (String => (String, String)) = { str =>
val ptn = """^(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9])$""".r
ptn findFirstIn str match {
case Some(ptn(h, m)) => ("%02d".format(h.toInt), "%02d".format(m.toInt))
case None => ("--", "--")
}
}
Array("01:23", "1:23", "12:34", "23:60", "25:00") foreach { str =>
val (hour, min) = timeParser(str)
@tachesimazzoca
tachesimazzoca / gist:3617264
Last active October 10, 2015 02:18
Regex examples #scala
val urlMatcher: (String => Option[Map[Symbol, String]]) = { str =>
val ptn = """^(https?)://([^/]+)(.*)$""".r
for { ptn(m1, m2, m3) <- ptn findFirstIn str } yield {
Map(
'scheme -> m1,
'host -> m2,
'uri -> m3
)
}
}
@tachesimazzoca
tachesimazzoca / gist:3617437
Last active October 10, 2015 02:18
Date format #scala
import java.util.Date
// Use StringLike#format
println("%tY-%<tm-%<td %<tH:%<tM:%<tS".format(new Date()))
@tachesimazzoca
tachesimazzoca / gist:3698095
Last active October 10, 2015 13:37
Loan Pattern #scala
import java.io._
object HexDumper {
def main(args: Array[String]) {
def withCloseable(args: Array[Closeable])(f: (Array[Closeable]) => Unit) =
try f(args) finally args map { _.close }
withCloseable(
@tachesimazzoca
tachesimazzoca / .vimrc
Last active October 10, 2015 19:38
.vimrc #vim
set directory=$HOME/.vim/swap
set nobackup
set number
set expandtab
set tabstop=2
set softtabstop=2
set shiftwidth=2
"set foldmethod=marker
@tachesimazzoca
tachesimazzoca / gist:3747707
Created September 19, 2012 04:34
Unix - date
# Unix timestamp
% date "+%s"
1348029114
% date -d @1348029114 "+%Y-%m-%d %H:%M:%S"
2012-09-19 13:31:54
@tachesimazzoca
tachesimazzoca / gist:3847867
Last active October 11, 2015 10:57
Add a directory to the library search path #ruby
$:.unshift File.dirname(__FILE__)
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib]) # "../lib"
@tachesimazzoca
tachesimazzoca / gist:3891036
Last active October 11, 2015 17:07
Makefile for Unicorn #ruby
PID = /path/to/unicorn.pid
all:
@echo Usage: (start|stop|restart|graceful)
start:
@bundle exec unicorn -c config/unicorn.rb -D
stop:
@[[ -s "$(PID)" ]] && kill -QUIT `cat $(PID)`
restart:
@[[ -s "$(PID)" ]] && kill -HUP `cat $(PID)`
@tachesimazzoca
tachesimazzoca / gist:4343390
Last active December 9, 2015 23:19
Adding swap space #linux
% su -
% dd if=/dev/zero of=/swap bs=1M count=2048
% mkswap /swap
% swapon /swap
% vi /etc/fstab
....
/swap swap swap defaults 0 0