Skip to content

Instantly share code, notes, and snippets.

@ryoppy
ryoppy / snippets.scala
Last active December 15, 2015 02:09
いろんなスニペット置き場
// SeqをMapに
// Seq(1,2,3) => Map(1 -> 2, 2 -> 3, 3 -> 4)
Seq(1, 2, 3).flatMap { i =>
Map(i -> (i + 1))
}.toMap
// SomeにnullいれてもNoneにはならない
Some(null) == None // false
Option(null) == None // true
@ryoppy
ryoppy / gist:5193936
Created March 19, 2013 05:37
specs2でクラス内のテストをタグで指定して実行する方法

specs2でテストでtagをつけて

"hugehuge" in {
  //
} tag("a")

指定して実行

@ryoppy
ryoppy / ZipSample.scala
Created March 21, 2013 08:53
Zipが便利すぎたのでメモ
val pp1 = Seq(1, 2) zip Seq(3, 4)
println(pp1)
// List((1,3), (2,4))
case class Image(width: Seq[Int], height: Seq[Int])
val images = Seq(Image(Seq(200, 300), Seq(210, 310)), Image(Seq(400, 500), Seq(510, 610)))
val pp2 = images.flatMap { b =>
b.width zip b.height
@ryoppy
ryoppy / CakePattern.scala
Created May 1, 2013 05:57
traitで子のプロパティなど参照したいとき
abstract class Interface(_id: Int) {
def id: Int = _id
}
trait Like {
self : Interface =>
def get: Int = this.id
}
trait Param {
@ryoppy
ryoppy / file0.js
Created July 11, 2013 09:14
オブジェクトのキーでアルファベット順にソート ref: http://qiita.com/ryoppy@github/items/ee1cd051cce4c6668899
var params = {b: 1, a: 5, c: 2};
_.pick(params, _.keys(params).sort()); // {a: 5, b:1, c:2}
@ryoppy
ryoppy / file0.js
Created July 11, 2013 13:34
オブジェクトをアルファベット順でGETパラメータに変換 ref: http://qiita.com/ryoppy@github/items/4373bba239ae7320181f
var obj = {b: 1, a: 3, c:2};
_.map(_.keys(obj).sort(), function(key) {
return key + '=' + obj[key];
}).join('&');
// a=3&b=1&c=2
@ryoppy
ryoppy / file0.js
Created July 17, 2013 22:51
serializeArrayをname:valueのオブジェクトに変換 ref: http://qiita.com/ryoppy@github/items/0ceb0c19b92da34c7ce1
/**
* $formのデータをkey:valueで取得
* @param {jQuery} $form
* @return {Object} {name: "value", ...}
*/
getValues: function($form) {
return _.chain($form.serializeArray())
.map(function(obj) { return [obj.name, obj.value] })
.object()
.value();
@ryoppy
ryoppy / hbase-standalone
Created August 14, 2013 04:22
/etc/init.d/hbase-standalone
#!/bin/sh
#
# hbase - this script starts and stops the hbase-standalone daemon
#
# chkconfig: - 85 15
# description: hbase-standalone daemon
# processname: hbase-standalone
# Source function library.
. /etc/rc.d/init.d/functions
@ryoppy
ryoppy / TextIncrementer.scala
Created August 23, 2013 11:17
TextIncrementer
/**
* 文字列内の数値をインクリメントする
* ex) TextIncrementer("abc123efg456hij").++() // abc124efg457hij
* ex) TextIncrementer("あいう1 2 3えお").++() // あいう2 3 4えお
* @param t インクリメントしたい文字列
* @param incrValue いくつインクリメントするか
* @return インクリメントした文字列
*/
case class TextIncrementer(t: String, incrValue: Int = 1) {
def ++(i: Int = 0, n: String = ""): String = {
@ryoppy
ryoppy / FileWatcher.scala
Last active December 22, 2015 10:29
Scalaでファイル変更監視
import java.nio.file.{ FileSystem, FileSystems, WatchKey, WatchService, StandardWatchEventKinds }
import java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY
import scala.collection.JavaConverters._
import scala.concurrent._
import ExecutionContext.Implicits.global
object Main {
def main(args: Array[String]) {
FileWatcher("/tmp/build/classes", "hoge.html").watch {
println("change")