Skip to content

Instantly share code, notes, and snippets.

import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
import language.experimental.fewerBraces
class SampleSpec extends AnyWordSpec:
"A Set" when:
"empty" should:
"have size 0" in:
@shinharad
shinharad / CirceEitherCodec.scala
Last active May 28, 2020 01:18
circeでEitherのCodecを定義する
implicit def EitherDecoder[L: Decoder, R: Decoder]: Decoder[Either[L, R]] =
Decoder[R].map(Right.apply) or Decoder[L].map(Left.apply)
implicit def EitherEncoder[L: Encoder, R: Encoder]: Encoder[Either[L, R]] = {
case Left(l) => Encoder[L].apply(l)
case Right(r) => Encoder[R].apply(r)
}
{-# LANGUAGE RankNTypes #-}
import Data.IORef
import System.IO.Unsafe (unsafePerformIO)
newtype ST s a = ST
{ unsafeRunST :: a
}
instance Functor (ST s) where
{-# LANGUAGE GADTs #-}
module Main where
data Exp r where
Const :: (Show r) => r -> Exp r
Add :: (Num r) => Exp r -> Exp r -> Exp r
Subtract :: (Num r) => Exp r -> Exp r -> Exp r
Eq :: (Eq r) => Exp r -> Exp r -> Exp Bool
And :: Exp Bool -> Exp Bool -> Exp Bool
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Main where
import Control.Applicative
import Control.Monad.Identity
import Control.Monad.Reader
import Control.Monad.Writer
data Config =
object InvertibleScramble {
def scramble(value: Int): Int = {
require(value > 0)
def f =
multiplyOdd1 andThen
trim andThen
reverse andThen
@shinharad
shinharad / CodeGolf.scala
Last active April 23, 2017 07:30
10進数、2進数、8進数のいずれで表現しても回文数となる数のうち、10進数の10以上で最小の値を求める
type A=Int;type B=Seq[A]
def f(n:A,r:A,a:B=Nil):B=if(n<=0)a else f(n/r,r,n%r+:a)
def g(n:B)=n==n.reverse
def h(n:A):A=if(g(f(n,2))&&g(f(n,8))&&g(f(n,10)))n else h(n+1)
print(h(10))