Skip to content

Instantly share code, notes, and snippets.

@leifwickland
leifwickland / gist:6168a4c2a93c58eb803f
Created November 21, 2014 03:05
Smash multiple PDFs into one.
gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=/tmp/output.pdf input1.pdf input2.pdf`
// Suggested Wartremover errors to improve inference rules and avoid partial methods which throw
wartremoverErrors ++= Seq(
Wart.Any,
Wart.Any2StringAdd,
Wart.EitherProjectionPartial,
Wart.OptionPartial,
Wart.Product,
Wart.Serializable,
Wart.ListOps,
Wart.Nothing
// Adapted from Rob Norris' post at https://tpolecat.github.io/2014/04/11/scalac-flags.html
scalacOptions ++= Seq(
"-deprecation",
"-encoding", "UTF-8", // yes, this is 2 args
"-feature",
"-unchecked",
"-Xfatal-warnings",
"-Xlint",
"-Yno-adapted-args",
String.format() call with mismatched args
Unused function argument
aList.length == 0 <- use .isEmpty
Using string interpolation unnecessarily
Calling .toString on Array
Close scala.io.Source
Inferring Nothing
Casting instead of .toByte
Calling .toSeq on a Seq
Calling .toString on a String
@leifwickland
leifwickland / Output
Created June 18, 2013 17:18
In Scala, require a type parameter has multiple typeclasses.
Q: some string
R: some string
--langdef=scala
--langmap=scala:.scala
--regex-Scala=/^[ \t]*(abstract[ \t]*)*(sealed[ \t]*)*(case[ \t]*)*class[ \t]*([a-zA-Z0-9_]+)/\4/c,classes/
--regex-Scala=/^(case[ \t]*)*[ \t]*object[ \t]*([a-zA-Z0-9_]+)/\2/o,objects/
--regex-Scala=/^[ \t]*trait[ \t]*([a-zA-Z0-9_]+)/\1/t,traits/
--regex-Scala=/[ \t]*def[ \t]*([a-zA-Z0-9_=]+)[ \t]*.*[:=]/\1/m,methods/
--regex-Scala=/[ \t]*val[ \t]*([a-zA-Z0-9_]+)[ \t]*[:=]/\1/V,values/
--regex-Scala=/[ \t]*var[ \t]*([a-zA-Z0-9_]+)[ \t]*[:=]/\1/v,variables/
--regex-Scala=/^[ \t]*type[ \t]*([a-zA-Z0-9_]+)[ \t]*[\[<>=]/\1/T,types/
--regex-Scala=/^[ \t]*import[ \t]*([a-zA-Z0-9_{}., \t=>]+$)/\1/i,includes/
@leifwickland
leifwickland / sortBy.php
Created May 11, 2012 17:33
sortBy() function for PHP (inspired by Scala)
<?php
/**
* Sorts an array based on keys provided by a closure which extracts a value from the array's elements.
*
* Keys are not preserved. The original is not modified.
*
* Inspired by Scala's Seq.sortBy()
* @see http://www.scala-lang.org/api/current/index.html#scala.collection.Seq
*
* @param $array array-like To sort. (Really it just needs to be something iterable.)
@leifwickland
leifwickland / npr.scala
Created January 29, 2012 19:12
NPR Sunday Puzzle for Jan 29, 2012
object npr {
def main(args: Array[String]) {
val digits = "123456789"
val numbers = for (
i <- 1 to (digits.length - 3);
j <- (i + 1) to (digits.length - 2);
k <- (j + 1) to (digits.length - 1)
) yield Seq(
digits.substring(0,i),
digits.substring(i,j),
@leifwickland
leifwickland / HFileInputFormat.scala
Created August 2, 2011 14:34
Allows an HFile to be used as the input to MapReduce.
import org.apache.hadoop.fs.Path
import org.apache.hadoop.hbase.io.hfile.{HFile,HFileScanner}
import org.apache.hadoop.hbase.io.hfile.HFile.Reader
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.KeyValue
import org.apache.hadoop.mapreduce.{JobContext,InputSplit,TaskAttemptContext,RecordReader}
import org.apache.hadoop.mapreduce.lib.input.{FileInputFormat,FileSplit}
class HFileInputFormat extends FileInputFormat[ImmutableBytesWritable, KeyValue] {
override def isSplitable(context: JobContext, file: Path): Boolean = false
@leifwickland
leifwickland / gist:1099861
Created July 22, 2011 17:01
Here's one solution I found for fighting with type erasure warnings.
object typeErasure {
def main(args: Array[String]) {
val evenOdd = 1.to(10)
.map{x => if (x%2==0) { Option[Int](x) } else { None } }
.partition{ case Some(_) => true case _ => false }
println(evenOdd)
}
}