Skip to content

Instantly share code, notes, and snippets.

View nicktelford's full-sized avatar

Nick Telford nicktelford

View GitHub Profile
@nicktelford
nicktelford / Option.scala
Created June 26, 2017 11:41
Cheap Options in dotty?
type Option[A] = A | Null
object Option {
inline def apply[A](x: A): Option[A] = x
}
implicit class OptionMethods[A](val x: Option[A]) extends AnyVal {
inline def map[B](f: A => B): Option[A] = x match {
case null => null
@nicktelford
nicktelford / scala.net.scala
Last active June 1, 2017 10:56
Rough sketch of ideas for a "scala.net" like package
class UnknownHostException(hostname: String) extends RuntimeException(s"Unknown host: $hostname")
object NetworkAddress {
def apply(address: String): Option[NetworkAddress] = address match {
case addr @ IPv4NetworkAddress.pattern(_, _, _, _) => Some(new IPv4NetworkAddress(addr) {})
case addr @ IPv6NetworkAddress.pattern(_, _, _, _, _, _, _, _) => Some(new IPv6NetworkAddress(addr) {})
case addr @ UnresolvedNetworkAddress.pattern() => Some(new UnresolvedNetworkAddress(addr) {})
case _ => None
}
}
package com.yammer.dropwizard.validation;
import com.google.common.base.Joiner;
import com.google.common.collect.*;
import org.hibernate.validator.internal.engine.ConstraintValidatorFactoryImpl;
import javax.validation.*;
import javax.validation.groups.Default;
import java.util.Set;
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])
@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("...")
@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: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 {
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: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: