Skip to content

Instantly share code, notes, and snippets.

View alexandru's full-sized avatar
😺
Having fun

Alexandru Nedelcu alexandru

😺
Having fun
View GitHub Profile
#!/usr/bin/env bash
# Install script adapted from:
# https://gist.github.com/dwayne/2983873
NODE_DIR="$HOME/.nodejs"
echo 'export PATH=$HOME/$NODE_DIR/bin:$PATH' >> ~/.bashrc
echo 'export NODE_PATH=$HOME/$NODE_DIR/lib/node_modules' >> ~/.bashrc
source ~/.bashrc
package object extensions {
import play.api.mvc._
implicit class ResultExtensions(val result: PlainResult)
extends AnyVal {
def withLangCookie(lang: Lang)(implicit app: Application): PlainResult =
result.withCookies(Cookie(Play.langCookieName, lang.code, httpOnly = false))
}
}
@alexandru
alexandru / Environment.scala
Last active December 27, 2015 13:49
Play / Subcut dependency injection in modules
// Helper for keeping track of the active project bindings
// (because we don't want to hard-code those inside singleton objects)
object Environment {
def currentBindings =
ref.get match {
case null =>
throw new NullPointerException("No BindingModule currently registered (app not running?)")
case value =>
value
@alexandru
alexandru / Ref.scala
Created January 10, 2014 23:09
I'm implementing my own FSM Actors because akka.actor.FSM is disappointing. Part of the implementation is a new way of dealing with data mutation on events. I want Vars, but I want to enforce the scope in which reads or writes can happen. Following is an implementation for scoped, thread-safe Refs that also preserve their previous state before t…
package fsm
import scala.annotation.implicitNotFound
import scala.util.control._
import RefContext._
import Ref._
/**
* Ref is a scoped type-safe mutable reference.
*
// CONTEXT: https://groups.google.com/forum/#!topic/scala-user/8YpX1VkIkDs
import scala.annotation.implicitNotFound
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
/**
* Macro-driven type for proving that an implicit parameter does not exist
* in scope.
*/
@alexandru
alexandru / cache-sample-2.scala
Created September 21, 2014 12:30
Immutable cache implementation
import scala.annotation.tailrec
import scala.collection.SortedSet
import scala.concurrent.duration.{Duration, _}
import Cache._
import Cache.ExpiryInfo.ordering
/**
* An immutable cache implementation.
@alexandru
alexandru / designer.html
Created December 29, 2014 16:07
designer
<link href="../topeka-elements/category-images.html" rel="import">
<link href="../core-icon/core-icon.html" rel="import">
<link href="../core-icons/core-icons.html" rel="import">
<link href="../core-icons/av-icons.html" rel="import">
<link href="../paper-fab/paper-fab.html" rel="import">
<polymer-element name="my-element">
<template>
<style>
def recoverWith[T](source: Observable[T], pf: PartialFunction[Throwable, Observable[T]]) =
Observable.create[T] { subscriber =>
implicit val s = subscriber.scheduler
val o = subscriber.observer
source.unsafeSubscribe(new Observer[T] {
def onNext(elem: T) =
o.onNext(elem)
def onError(ex: Throwable) = {
import java.security.MessageDigest
import java.util
import javax.crypto.Cipher
import javax.crypto.spec.SecretKeySpec
import org.apache.commons.codec.binary.Base64
/**
* Sample:
* {{{
* scala> val key = "My very own, very private key here!"
import language.experimental.macros
import scala.reflect.macros.Context
/**
* Provides type-safe equality and inequality operators, implemented with
* macros for efficiency reasons.
*/
implicit class TypeSafeEquals[T](val self: T) extends AnyVal {
def ===[U](other: U): Boolean = macro TypeSafeEquals.equalsImpl[T, U]