Skip to content

Instantly share code, notes, and snippets.

object FizzBuzz {
def main(args: Array[String]) {
val fizzbuzz = (1 to 100).map { i =>
(i % 3, i % 5) match {
case (0, 0) => "fizzbuzz"
case (0, _) => "fizz"
case (_, 0) => "buzz"
case _ => i.toString
}
}
@Biacco42
Biacco42 / TypeClassTest.hs
Created May 25, 2017 16:50
Haskell Type Class Study
{-# LANGUAGE FlexibleInstances #-}
main = do
print $ mySum ([1, 2, 3] :: [Int])
print $ mySum (["hoge", "piyo", "huga"] :: [String])
class AddMonoid a where
unit :: a
add :: a -> a -> a
@Biacco42
Biacco42 / TypeClassPlayGround.scala
Created May 25, 2017 16:45
TypeClass PlayGround
package info.biacco42.test.typeclass
/**
* Created by sanok on 2017/05/25.
*/
trait Addable[T] {
val unit: T
def add(x: T, y: T): T
(1 ..< 100).forEach {
switch ($0 % 3, $0 % 5) {
case (0, 0):
print("FizzBuzz")
case (0, _):
print("Fizz")
case (_, 0):
print("Buzz")
default:
print($0)
object Main extends App{
implicit val createValueInt: Int = 3
implicit val createValueStrign: String = "po"
def createValue[T](implicit value: T) = value
val valueInt: Int = createValue
println(valueInt)
val valueString: String = createValue
object Main extends App{
trait Creatable[T] {
def create(): T
}
class Cat(val name: String)
// 自作の型への適用
implicit object Cat extends Creatable[Cat] {
def create() = new Cat("taro")