Skip to content

Instantly share code, notes, and snippets.

Andrew Conner andrewconner

Block or report user

Report or block andrewconner

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View OverrideVal.scala
class Parent {
val a = 1
val b = a
}
class Kid extends Parent {
override val a = 100
}
new Kid().b // == 0
View Lazily.scala
final class Lazily[A](f: () => A) {
private[this] var thunk: () => A = f
@volatile private var inst = false
private lazy val value: A = {
val r = thunk()
thunk = null // scalastyle:off
inst = true
r
}
def get: A = value
View ForwardPipe.scala
implicit class AnyExtensionOps[A](val x: A) extends AnyVal {
def |>[B](f: A => B): B = f(x)
}
View keybase.md

Keybase proof

I hereby claim:

  • I am andrewconner on github.
  • I am adc (https://keybase.io/adc) on keybase.
  • I have a public key whose fingerprint is 5ED1 9137 2BCA C7BC 5E5D 3806 7C3F 80BE DD1B CDBD

To claim this, I am signing this object:

View topdomains.txt
| 2546 | youtube.com |
| 865 | facebook.com |
| 632 | docs.google.com |
| 550 | mail.google.com |
| 514 | amazon.com |
| 507 | github.com |
| 499 | google.com |
| 455 | linkedin.com |
| 386 | en.wikipedia.org |
| 378 | techcrunch.com |
@andrewconner
andrewconner / FutureGoodies.scala
Last active May 4, 2016
SafeFuture, TimeoutFuture, CancelableFuture implementations. See https://eng.42go.com/future-safefuture-timeout-cancelable for further explanation.Thanks to @bretthoerner for spotting an error!
View FutureGoodies.scala
/* We've run into a few common pitfalls when dealing with Futures in Scala, so I wrote these three helpful
* classes to give some baked-in functionality.
*
* I'd love to hear about other helpers you're using like these, or if you have improvement suggestions.
* github@andrewconner.org / @connerdelights
*/
import scala.concurrent.{ExecutionContext, CanAwait, Awaitable, Future, Promise}
import scala.concurrent.duration.Duration
import scala.util.Try
@andrewconner
andrewconner / TrimString.scala
Created Aug 16, 2013
Trimming a string at a certain number of bytes
View TrimString.scala
import java.nio.{ByteBuffer, CharBuffer}
import java.nio.charset.Charset
def trimAtBytes(str: String, len: Int, charset: Charset) = {
val outBuf = ByteBuffer.wrap(new Array[Byte](len))
val inBuf = CharBuffer.wrap(str.toCharArray())
charset.newEncoder().encode(inBuf, outBuf, true)
new String(outBuf.array, 0, outBuf.position(), charset)
}
View ReverseInt.scala
// Reverses an integer without using Strings
def reverse(n: Int): Int = {
val len = math.ceil(math.log10(n+1)).toInt
Stream.continually(n).take(len).zipWithIndex
.map{ case (m,p) =>
(m % math.pow(10, len-p) / math.pow(10, len-p-1)).toInt * math.pow(10,p)
}
.sum.toInt
}
@andrewconner
andrewconner / Employee.scala
Created Jun 21, 2013
Validating objects during a db save using `Try`
View Employee.scala
case class Employee(id: Option[Long] = None, name: String, age: Int, salary: Int)
object EmployeeRepo {
private def persist(employee: Employee): Employee = {
// persisting logic
employee.copy(id = Some(10L)) // database returns persisted id, return new object with id set
}
def save(employees: Seq[Employee])(implicit validator: Employee => Try[Employee]): Seq[Try[Employee]] = {
employees.map(validator(_).map(persist))
@andrewconner
andrewconner / KillingPlay.sh
Created Jun 20, 2013
Killing a running Play! framework application. First gracefully, and then quite violently.
View KillingPlay.sh
maxShutdownTime=15
function stopServiceProcess {
kill $pid || return 1
for ((i=0; i<maxShutdownTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
You can’t perform that action at this time.