Skip to content

Instantly share code, notes, and snippets.

@yuba
yuba / CachedIterable.java
Last active January 4, 2016 21:39
CachedIterable<T>
package net.miuras;
import com.sun.istack.internal.NotNull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Even if you iterate a CachedIterable object several times, the source Iterable object will be iterated only once.
*
@yuba
yuba / ProducerConsumerIterablel.java
Last active January 4, 2016 21:49
ProducerConsumerIterable<T>
package net.miuras;
import com.sun.istack.internal.NotNull;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.concurrent.LinkedBlockingQueue;
/**
* When you iterate ProducerConsumerIterable object, the source Iterable object will be iterated in another thread, which you can call a producer thread.
*/
@yuba
yuba / DateTimeUtil.cs
Last active August 29, 2015 13:56
.NETのDateTimeを正しく(タイムゾーンが異なるインスタンス同士でも絶対時刻の前後で)比較するメソッド
using System;
namespace Common
{
public static class DateTimeUtil
{
/// <summary>
/// 二つの時刻値の大きい方を返します。
/// ただし、タイムゾーン設定が異なる場合、Utc>Local>Unspecifiedの優先順位で高い方にそろえます。
/// LocalをUtcにそろえるときは時刻換算が発生します。
@yuba
yuba / Javaでzip関数.java
Created March 18, 2014 02:31
http://d.hatena.ne.jp/Nagise/20140315/1394884271 の、Iterator, Iterableを一個のクラスにまとめて実装してしまうのは .iterator() を複数回呼び出したときに問題があるので匿名クラスを使った実装を書いてみました。
public static <T1,T2> Iterable<Tuple<T1,T2>> zip (
final Iterable<? extends T1> src1,
final Iterable<? extends T2> src2) {
return new Iterable<Tuple<T1, T2>>() {
@Override
public Iterator<Tuple<T1, T2>> iterator() {
final Iterator<? extends T1> iterator1 = src1.iterator();
final Iterator<? extends T2> iterator2 = src2.iterator();
return new Iterator<Tuple<T1, T2>>() {
@Override
@yuba
yuba / Either.ts
Last active August 29, 2015 13:57
Either in TypeScript
module Functional {
export class Either<L, R> {
constructor(public apply: <TResult>(funcL: (p: L) => TResult, funcR: (p: R) => TResult) => TResult) { }
static left<L, R>(obj: L): Either<L, R> {
return new Either<L, R>(<TResult>(funcL: (p: L) => TResult, funcR: (p: R) => TResult) => funcL(obj));
}
static right<L, R>(obj: R): Either<L, R> {
return new Either<L, R>(<TResult>(funcL: (p: L) => TResult, funcR: (p: R) => TResult) => funcR(obj));
@yuba
yuba / Try.java
Created March 26, 2014 14:55
"@zakky_dev: Aの処理に失敗したらBをやり、それにも失敗したらCをやり、そこで失敗したら例外をスローする。どの処理でも成功したらDの処理を実行する。こういったことやりたいのかなりあるんだけど、どうしよっかなー。" https://twitter.com/zakky_dev/status/448654622970753024
/**
* 渡されたアクションを先頭からどれか成功するまで順に実行します。すべて失敗した場合最後の例外をRuntimeExceptionでくるんで投げます。
* Tries to execute given actions until one of them succeeds. Throws RuntimeException if all of them fail.
*/
public class Try {
public static void these(Runnable... actions) {
Throwable lastThrown = null;
for (Runnable action: actions) {
try {
action.run();
@yuba
yuba / AbstractCollection.java
Created June 1, 2014 13:21
JavaコレクションクラスのtoString()をスレッドローカル変数で補強する ref: http://qiita.com/yuba/items/35fada18850578edea73
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
@yuba
yuba / IEnvironmentFetcher.cs
Last active August 29, 2015 14:02
現在時刻の取得をコード中に直に書かない ref: http://qiita.com/yuba/items/f52f90c4bd249d24b7d6
public interface IEnvironmentFetcher {
DateTime GetTime();
}
@yuba
yuba / file0.c
Last active March 27, 2019 13:04
図形情報を扱うのに大事なことは、全部高校で教わった ref: https://qiita.com/yuba/items/2f35d1e5e162f44fc1b3
typedef struct{
double x;
double y;
} vector2D;
vector2D nearest(vector2D A, vector2D B, vector2D P) {
vector2D a, b;
double r;
a.x = B.x - A.x;
@yuba
yuba / file0.sql
Last active October 24, 2017 10:29
論理削除と一意性制約を両立させる方法・DB製品別 ref: http://qiita.com/yuba/items/70165875cfe02b03513d
CREATE UNIQUE INDEX mail_is_unique ON users (mail)
WHERE status = 0;