Skip to content

Instantly share code, notes, and snippets.

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]] =
import user
import message
object Main {
def main(args: Array[String]) {
import MessageSender._
val natsuki = User("Natsuki Ando")
val sakura = User("Sakura Domyoji")
// IDみたいな仕組みがあって
trait Identifier[A] {
val value: A
}
case class UserId(value: UUID) extends Identifier[UUID]
case class GroupId(value: UUID) extends Identifier[UUID]
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
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)
}
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
func main() {
require(['file-chunk'], function(FileChunk) {
var file = pickupFile();
var fileChunk = new FileChunk(file);
var chunks = fileChunk.chop(4096);
// each: 各チャンクのpromiseのdoneを設定する
chunks.each(function(chunk) {
});
trait FlipFlap[A, B] {
def doFlipFlap(x: A): B
}
class FlipFlapOps[A, B](v: A, f: FlipFlap[A, B]) {
def flipFlap = f.doFlipFlap(v)
}
object Main {
implicit def intFlipFlap = new FlipFlap[Int, Int] {
module Submodule
def subset(&pred)
mod = self
Module.new do
include mod
public_instance_methods.each do |method_name|
method = instance_method(method_name)
undef_method method_name unless pred.call(method)
module Paperclip
module Model
extend ActiveSupport::Concern
module ClassMethods
def has_attached_file(name)
{
file_name: :original_filename,
content_type: :content_type,
file_size: :size