This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class JsonRpcBody[P <: Product](jsonrpc: String, method: String, params: P, id: String) | |
case class SampleParams(_1: String, _2: Int) extends Product2[String, Int] | |
// JsonRpcBody[SampleParams] にデコードするためには、DecodeJson[JsonRpcBody[SampleParams]] を | |
// 定義する必要がある。しかし、SampleParams 以外にも増えるのは予想され、その度に DecodeJson[JsonRpcBody[OtherParams]] などを | |
// 書くと大変めんどう。params の解釈の仕方が違うだけなのに、jsonrpc, method, id の部分も解釈しなきゃだし。 | |
// と言うわけで以下のように書く。 | |
implicit def jsonRpcBodyDecodeJson[P <: Product](implicit paramsDecodeJson: DecodeJson[P]): DecodeJson[JsonRpcBody[P]] = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import user | |
import message | |
object Main { | |
def main(args: Array[String]) { | |
import MessageSender._ | |
val natsuki = User("Natsuki Ando") | |
val sakura = User("Sakura Domyoji") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IDみたいな仕組みがあって | |
trait Identifier[A] { | |
val value: A | |
} | |
case class UserId(value: UUID) extends Identifier[UUID] | |
case class GroupId(value: UUID) extends Identifier[UUID] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Main { | |
implicit class Capping[+A](value: A) { | |
def cap[B >: A](limit: B)(implicit ordering: Ordering[B]): B = { | |
if (ordering.lteq(value, limit)) value else limit | |
} | |
} | |
def main(args: Array[String]) { | |
println( 99.cap(100)) // => 99 | |
println(101.cap(100)) // => 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Violation(message: String) | |
class Funny[V](value: V) { | |
def validateWith(validator: Validator[V]): Validity[V] = validator(value) | |
} | |
object Funny { | |
def apply[V](value: V): Funny[V] = new Funny(value) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set :unicorn_config_path, -> { current_path.join('config', 'unicorn.rb') } | |
set :unicorn_pid_path, -> { current_path.join('tmp', 'pids', 'unicorn.pid') } | |
set :unicorn_env, -> { fetch(:rails_env) || fetch(:stage) } | |
SSHKit.config.command_map[:unicorn_stop] = 'kill -QUIT' | |
SSHKit.config.command_map[:unicorn_restart] = 'kill -USR2' | |
namespace :deploy do | |
def unicorn_running? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Expression { | |
def +(right: Expression) = Addition(this, right) | |
def *(right: Expression) = Multiplication(this, right) | |
def **(right: Expression) = Exponential(this, right) | |
} | |
object Expression { | |
implicit def intToConst(value: Int) = Const(value) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Color = function() { | |
if (arguments.length == 1) | |
[this.r, this.g, this.b] = Color.parse(arguments[0]); | |
else if (arguments.length == 3) | |
[this.r, this.g, this.b] = arguments; | |
else | |
throw 'invalid number of arguments, takes one or three arguments'; | |
}; | |
Color.parse = function(color) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
attr_accessor :current_phase | |
def self.thereafter(phase) | |
-> do | |
current_phase.nil? or current_phase >= phase | |
end | |
end | |
validates :email, presence: true, if: thereafter(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Definable | |
def self.redefine(object, method_name, &process) | |
original_method = object.method(method_name) | |
object.define_singleton_method(method_name) do |*args, &block| | |
process[*args, &block] | |
original_method[*args, &block] | |
end | |
end |
NewerOlder