Skip to content

Instantly share code, notes, and snippets.

@x7c1
x7c1 / CommandRunner.scala
Last active February 5, 2017 15:05
Runs publishLocal on '-SNAPSHOT' version.
import sbt.State
object CommandRunner {
/**
* Convert the given command string to a release step action,
* preserving and invoking remaining commands
* Note: This was copied from https://github.com/sbt/sbt-release/blob/663cfd426361484228a21a1244b2e6b0f7656bdf/src/main/scala/ReleasePlugin.scala#L99-L115
*/
def runCommand(command: String): State => State = { st: State =>
@x7c1
x7c1 / CustomMatchers.scala
Last active January 29, 2017 00:46
substitute matcher for unusable theSameElementsAs
package x7c1.wheat.splicer.core
import org.scalatest.matchers.{MatchResult, Matcher}
trait CustomMatchers {
def containAllOf[A: Manifest : StringLike](expected: Seq[A]): Matcher[Seq[A]] = {
val asString = implicitly[StringLike[A]]
Matcher { actual =>
val unknown = expected.find(!actual.contains(_))
val toMessage = (name: A) => {
@x7c1
x7c1 / Tap.scala
Created January 24, 2017 23:13
Ruby's Object#tap by Scala
package x7c1.wheat.splicer.core.logger
object Tap {
object implicits {
implicit class Provider[A](val target: A) extends AnyVal {
def tap(f: (A => Unit)*): A = {
f foreach (_ (target))
target
}
}
@x7c1
x7c1 / Reader.scala
Last active January 8, 2017 15:18
Reader for sbt
package x7c1.wheat.splicer.lib
import sbt.Def.Initialize
import sbt.{Def, Logger, Task}
case class Reader[X, A](run: X => A) {
def map[B](f: A => B): Reader[X, B] = {
new Reader[X, B](x => f(run(x)))
}
import sbt.complete.DefaultParsers.{NotSpace, Space, failure, token}
import sbt.complete.Parser
import scala.util.{Failure, Success, Try}
object ReductiveParser {
def from(items: Seq[String]): Parser[Seq[String]] = {
new ReductiveParser(items).parser
}
}
@x7c1
x7c1 / Fate.scala
Last active June 25, 2016 04:46
Substitutes for Reader[ExecutionContext, Future[Either[L, R]]]
trait Fate[X, +L, +R]{
def map[R2](f: R => R2): Fate[X, L, R2]
def flatMap[L2 >: L, R2](f: R => Fate[X, L2, R2]): Fate[X, L2, R2]
def run[L2 >: L, R2 >: R]: X => (Either[L2, R2] => Unit) => Unit
}
object Fate {
def apply[X, L, R](underlying: X => (Either[L, R] => Unit) => Unit): Fate[X, L, R] = {
new FateImpl(underlying)
}
}
@x7c1
x7c1 / file0.java
Created April 29, 2016 10:33
Robolectric で android.util.Log の出力先を変える ref: http://qiita.com/jwhaco/items/8c7c3b031bf14436f43b
ShadowLog.stream = System.out;
@x7c1
x7c1 / EitherTask.scala
Last active April 2, 2016 16:50
Classes to handle callback with Either[L, R]
package x7c1.wheat.modern.callback.either
import x7c1.wheat.modern.callback.either.EitherTask.|
import x7c1.wheat.modern.patch.TaskAsync
import scala.concurrent.{Future, Promise}
class EitherTask [L, R] private (f: (Either[L, R] => Unit) => Unit){
def flatMap[A](g: R => L | A): L | A =
@x7c1
x7c1 / aar-build.scala
Created March 30, 2016 04:57
settings to load aar by sbt
def aar(moduleId: ModuleID, name: String): ModuleID = {
artifacts(moduleId, name, "aar")
}
def aar(moduleId: ModuleID): ModuleID = {
artifacts(moduleId, moduleId.name, "aar")
}
val jcenter = MavenRepository("jcenter", "https://jcenter.bintray.com/")
def artifacts(moduleId: ModuleID, name: String, extension: String): ModuleID =
moduleId artifacts Artifact(name, extension, extension) exclude
@x7c1
x7c1 / CallbackTask.scala
Last active November 15, 2015 14:01
sample to compose tasks with callback like onFinish
trait CallbackTask[EVENT] extends ((EVENT => Unit) => Unit) {
import scala.language.higherKinds
type This[A] <: CallbackTask[A]
type Builder[A] = ((A => Unit) => Unit) => This[A]
def map[A: Builder](f: EVENT => A): This[A] = implicitly[Builder[A]] apply {
g => apply(f andThen g)
}