Skip to content

Instantly share code, notes, and snippets.

View rinotc's full-sized avatar

Chiba Toshinori rinotc

View GitHub Profile
@rinotc
rinotc / ObjectCreationBuilderJavaLikeSample.scala
Last active August 1, 2021 03:12
Java like なビルダーパターンのサンプル
class NutrionFacts(builder: NutrionFacts.Builder) {
val servingSize: Int = builder.servingSize
val sergings: Int = builder.sergings
val calories: Int = builder.calories
val fat: Int = builder.fat
val sodium: Int = builder.sodium
val carbohydrate: Int = builder.carbohydrate
}
object NutrionFacts {
@rinotc
rinotc / ObjectCreationSampleCaseClass.scala
Created August 1, 2021 02:56
Scala で case class を使った場合の、デフォルトパラメータが必要な時のBuilder Patternサンプル
case class NutritionFacts(
servingSize: Int,
servings: Int,
calories: Int = 0,
fat: Int = 0,
sodium: Int = 0,
carbohydrate: Int = 0
)
val nf = NutritionFacts(1, 1, fat = 10)
@rinotc
rinotc / ObjectCreationBuilderPatternSample.java
Created August 1, 2021 02:13
オブジェクト生成、ビルダーパターンのサンプル
class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
public static class Builder {
// 必須パラメータ
@rinotc
rinotc / ObjectCreationSampleJavaBeans.java
Created August 1, 2021 01:55
オブジェクト生成方法、JavaBeans パターンサンプル
class NutritionFacts {
private int servingSize = -1; // 必須
private int servings = -1; // 必須
private int calories = 0; // オプション
private int fat = 0; // オプション
private int sodium = 0; // オプション
private int carbohydrate = 0; // オプション
public void setServingSize(int servingSize) {
this.servingSize = servingSize;
@rinotc
rinotc / TelescopingConstractor.java
Created August 1, 2021 01:43
テレスコーピング・コンストラクタのサンプル
class NutritionFacts {
private final int servingSize; // 必須
private final int servings; // 必須
private final int calories; // オプション
private final int fat; // オプション
private final int sodium; // オプション
private final int carbohydrate; // オプション
public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
@rinotc
rinotc / flip.hs
Created April 29, 2021 15:34
haskell 勉強メモ9 flip
flip' :: (a -> b -> c) -> (b -> a -> c)
flip' f = g
where g x y = f y x
@rinotc
rinotc / zipWith.scala
Created April 29, 2021 15:11
haskell勉強メモ9 zipWith Scala版
def zipWith[A, B, C](f: (A, B) => C)(listA: List[A])(listB: List[B]): List[C] = {
(listA, listB) match {
case (Nil, _) => Nil
case (_, Nil) => Nil
case (a :: as, b :: bs) => f(a, b) +: zipWith(f)(as)(bs)
}
}
zipWith { (a: Int, b: Int) => a + b }(List(1,2,3))(List(4,5,6))
@rinotc
rinotc / zipWith.hs
Created April 29, 2021 14:57
haskell勉強メモ9 zipWith
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (x : xs) (y : ys) = f x y : zipWith' f xs ys