3 ms·
With class/object generics, you are abstracting over the type the class is operating on, so you can have a List<Int> or List<String>. The class can define a sin
by rhdunn 5y ago
With class/object generics, you are abstracting over the type the class is operating on, so you can have a List<Int> or List<String>. The class can define a single implementation on all those types, and functions using that class can be written that also operate on any List instance (List<T>).
If you have a function `f(A) -> B` (e.g. converting a String to an Integer), you can define a map operation that works on List<A> and returns a List<B> by applying f to each item in List<A>. This map operation is useful for other objects (Optional, Set, Array, etc.). It is also the building block for other functions.
Higher Kinded Types allow you to say you have some type F<T>, where F is a type (List, Array, etc.) that can operate on any instance of T in a way that you can vary T without varying F. You could then define map like:
interface Mappable<A, F<A>> {
fun map<B>(f: (A) -> B): F<B>
}
class List<T> : Mappable<T, List<T>> {
fun map<B>(f: (A) -> B): List<B> { ... }
}
What category theory does is define a common language for these operations, and combined types -- which is where you get terms like Monad from. From a programming perspective, this is just an interface that implements several other interfaces, provides a set of methods an implementation must implement, and a set of methods the implementation gets "for free". -- This is similar to implementing Iterable, List, or other collection interfaces and getting a bunch of functionality for free.
- dgb23 5y ago> What category theory does is define a common language for these operations, and combined types This made it click thank you!
- deleted 5y ago[deleted]