Skip to content

Instantly share code, notes, and snippets.

View Centaur's full-sized avatar

oldpig Centaur

  • singerdream.com
  • Shanghai
View GitHub Profile
@Centaur
Centaur / Dispatcher.scala
Created October 20, 2014 12:33
dispatcher
package flux
/**
* Created by IntelliJ IDEA.
* User: xf
* Date: 14/10/20
* Time: 下午5:49
*/
trait Payload[T] {
@Centaur
Centaur / CommaSeperatedData.scala
Created October 31, 2014 04:08
dynamically run scala code
import tools.nsc.interpreter.IMain
import tools.nsc.Settings
object CommaSeperatedData extends App {
val data = """ "Doe, John", 35, 225, "5'10\"", "555-0123" """
val settings = new Settings
settings.usejavacp.value = true
val n = new IMain(settings)
n.interpret("List(" + data + ")")

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
realm=Sonatype Nexus Repository Manager
host=nexus.company.com
user=admin
password=admin123
@Centaur
Centaur / egret.scala
Last active August 29, 2015 14:09
forEgret
def name2id1(name: String): Option[String] = name match {
case "name1" => Some("id1")
case "name2" => Some("id2")
case "name3" => Some("id3")
case _ => None
}
def name2id2(name: String): Option[String] = name match {
case "name1" => None
case "name2" => Some("id2")
case "name3" => Some("id3")
@Centaur
Centaur / dry.scala
Last active August 29, 2015 14:09
Refactor protocolMap
import util._
abstract class SrcProtocol(val id: String)
case class CompileSrc(override val id:String) extends SrcProtocol(id)
case class TestSrc(override val id:String) extends SrcProtocol(id)
case class SystemSrc(override val id:String) extends SrcProtocol(id)
abstract class DstProtocol(val id: String)
@Centaur
Centaur / gist:ed0b55907d2261b572cd
Last active August 29, 2015 14:17
nested `andThen` transformed to normal function application

先从简单的情况入手,假设 f1 的类型是 A => B, 看看

    f2 andThen {
	    _ andThen f1
    }

到底是什么意思 先去掉语法糖,完整形式为:

/*
* Copyright (C) 2013 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@annotation.tailrec
def isSorted[A](as: Seq[A], ordered: (A, A)=>Boolean):Boolean = {
as match {
case Seq() => true
case Seq(_) => true
case Seq(head, second, rest@_*) =>
ordered(head, second) && isSorted(second +: rest, ordered)
}
}
def bubbleSort[A: Ordering](xs: List[A]): List[A] = {
@annotation.tailrec
def bubble(ys: List[A], init: List[A], max: A): (List[A], A) = ys match {
case Nil => (init, max)
case h :: tail =>
if(implicitly[Ordering[A]].compare(h, max) > 0)
bubble(tail, max::init, h)
else