Skip to content

Instantly share code, notes, and snippets.

View nicktelford's full-sized avatar

Nick Telford nicktelford

View GitHub Profile
@nicktelford
nicktelford / gist:872445
Created March 16, 2011 12:55
Abuse of 'self' to manipulate type-hinting
<?php
interface Comparable
{
public function compare(self $obj);
}
class A implements Comparable
{
public function compare(self $obj) {
return $this == $obj;
@nicktelford
nicktelford / gist:919227
Created April 14, 2011 10:18
How to determine available system memory
$ free -m
total used free shared buffers cached
Mem: 3017 2953 64 0 381 671
-/+ buffers/cache: 1899 1118
^
Available memory (in MB) -------------|
The Linux kernel will allocate as much free memory as possible to buffers and caches (e.g. the fs cache)
to improve performance. When an application allocates more memory than is "free", the kernel will allocate
memory from it's caches.
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /usr/lib/hadoop-0.20/logs (Is a directory)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:177)
at java.io.FileOutputStream.<init>(FileOutputStream.java:102)
at org.apache.log4j.FileAppender.setFile(FileAppender.java:290)
at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:164)
at org.apache.log4j.DailyRollingFileAppender.activateOptions(DailyRollingFileAppender.java:216)
at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:257)
at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:133)
@nicktelford
nicktelford / gist:988485
Created May 24, 2011 10:25
goto loop in libmemcached - criminal!
test_connect:
if (connect(ptr->fd,
(struct sockaddr *)&servAddr,
sizeof(servAddr)) < 0)
{
switch (errno)
{
case EINPROGRESS:
case EALREADY:
case EINTR:
INFO [NonPeriodicTasks:1] 2011-05-17 20:47:44,725 SSTable.java (line 147) Deleted /var/lib/cassandra/yyy-f-1201
INFO [COMMIT-LOG-WRITER] 2011-05-17 20:56:48,233 CommitLogSegment.java (line 50) Creating new commitlog segment /var/lib/cassandra/commitlog/CommitLog-1305665808233.log
INFO [ScheduledTasks:1] 2011-05-17 21:02:14,513 Gossiper.java (line 226) InetAddress /x.x.x.34 is now dead.
INFO [GossipStage:1] 2011-05-17 21:02:14,786 Gossiper.java (line 607) InetAddress /x.x.x.34 is now UP
INFO [ScheduledTasks:1] 2011-05-17 21:02:21,514 Gossiper.java (line 226) InetAddress /x.x.x.34 is now dead.
INFO [HintedHandoff:1] 2011-05-17 21:03:13,676 HintedHandOffManager.java (line 295) Endpoint /x.x.x.34 died before hint delivery, aborting
INFO [GossipStage:1] 2011-05-17 21:06:09,884 Gossiper.java (line 628) Node /x.x.x.34 has restarted, now UP again
INFO [GossipStage:1] 2011-05-17 21:06:10,552 StorageService.java (line 724) Node /x.x.x.34 state jump to normal
INFO [HintedHandoff:1] 2011-05-17 21:06:15,924 Hinte
@nicktelford
nicktelford / gist:1885542
Created February 22, 2012 15:27
Alternative to PartialFunctions for Extractors - avoids double unapply calls
// pretend this extractor is performance critical
object EvenExtractor {
def unapply(x: Int): Option[Int] = {
println("Testing for EVEN: " + x)
if (x % 2 == 0) Option(x) else None
}
}
// when you use it from a PartialFunction, it gets executed TWICE for all applicable values
Seq(1, 2, 3, 4) collect {
@nicktelford
nicktelford / gist:2204734
Created March 26, 2012 12:19
Implicits for Either? Nasty of helpful?
object Either {
implicit def aToLeft[A, B](value: A): Left[A, B] = Left(value)
implicit def bToRight[A, B](value: B): Right[A, B] = Right(value)
}
import Either._
case class User(id: Either[Long, String])
User(123L)
@nicktelford
nicktelford / gist:2204675
Created March 26, 2012 12:07
Why is this not a part of the Scala standard library?
object Either {
def apply[A, B](value: A): Left[A, B] = Left(value)
def apply[A, B](value: B): Right[A, B] = Right(value)
}
val a: Either[Long, String] = Either(123L)
// a: Either[Long,String] = Left(123)
val b: Either[Long, String] = Either("123")
b: Either[Long,String] = Right(123)
@nicktelford
nicktelford / gist:2205715
Created March 26, 2012 15:04
Pattern matching - why is it implemented at the language level?
object Any {
implicit def matching[A](a: A): Matchable[A] = new Matchable(a)
sealed class Matchable[A](o: A) {
// "match" is a reserved word, so lets call it "matchIt" instead
def matchIt[B](pf: PartialFunction[A, B]): B = {
if(pf.isDefinedAt(o)) {
pf(o)
} else {
throw new MatchError("...")
object App {
case class Post(title: String,
body: String,
tags: List[String])
case class Address(street: String,
city: String,
state: String,
zip: String,
tags: List[String])