Last active
September 24, 2015 09:56
-
-
Save yyYank/12807d0b264f76c78b33 to your computer and use it in GitHub Desktop.
KotlinでFunctorをやろうとするは良いが、概念自体これで合ってるのかもKotlin的にベストかも分からないアレ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// この辺りを眺めながら書いている | |
// https://gist.github.com/gakuzzzz/8d497609012863b3ea50#functor | |
// https://github.com/backpaper0/sandbox/blob/master/functor-applicative-study/src/main/scala/functor-applicative-study.scala | |
// https://bitbucket.org/cocoatomo/categorical/src/ac1a9340e607abc1691dd9f07b9f8f253a418fd6/src/main/java/co/coatomo/math/categorical/typeclass/functor/FList.java?at=default&fileviewer=file-view-default | |
interface Function1<T> { | |
fun <B> apply(value: T): Functor<B> | |
} | |
interface Function2<T, R> : Functor<T> { | |
fun apply(t : T) : Functor<R> | |
} | |
interface Functor<A> { | |
// 一つのFunctor値と「普通の値を取り普通の値を返す関数」を引数にとり Functor値を返す…らしい | |
fun <B> map(functor : Functor<A>, f : Function1<A>) : Functor<B> | |
} | |
interface Apply<A, B> : Functor<A> { | |
// Apply値とApplyに包まれた関数を受け取って、 | |
// 内包されている値を内包されている関数に適用して、 | |
// Applyに内包された結果値を返す処理 | |
fun ap<B>(apply : Apply<A, B>, f : Apply<A, Function2<A, B>>) : B | |
} | |
interface Maybe<A> : Apply<A, Maybe<A>> | |
class Just<A>(value : A) : Maybe<A> { | |
val value = value | |
override fun <B> map(functor: Functor<A>, f: Function1<A>): Functor<B> = f.apply(value) | |
override fun <B> ap(apply: Apply<A, B>, f: Apply<A, Function2<A, B>>): B { | |
throw UnsupportedOperationException() | |
} | |
} | |
class Cons<A>(value : A) : Maybe<A> { | |
override fun <B> map(functor: Functor<A>, f: Function1<A>): Functor<B> { | |
throw UnsupportedOperationException() | |
} | |
override fun <B> ap(apply: Apply<A, B>, f: Apply<A, Function2<A, B>>): B { | |
throw UnsupportedOperationException() | |
} | |
} | |
interface List<A> : Apply<A, List<A>> | |
class Nil<A> : List<A> { | |
override fun <B> map(functor: Functor<A>, f: Function1<A>): Functor<B> { | |
throw UnsupportedOperationException() | |
} | |
override fun <B> ap(apply: Apply<A, B>, f: Apply<A, Function2<A, B>>): B { | |
throw UnsupportedOperationException() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment