In Haskell, map
has type:
map :: (a -> b) -> [a] -> [b]
Note that a
and b
are not absolute types but type variables, which mean that they can be any type as long as each variable always refer to the same type in a specific function call. How to do the same thing in Go?
Go does not have a Hindley-Milner type system like Haskell's, so it can't express exactly the same things, like types with variables. The way type-agnostic functions are done in Go is with interfaces. If you want to express "any type", that's usually written as an empty interface (interface{}
). The type of the Map function in the standard library is:
func Map(iter Iterable, f func(interface{}) interface{}) Iterable
So instead of taking a function that goes from a
to b
, it takes a function that goes from interface{}
to interface{}
.
Chuck has already answered, but you may find my ty
library useful: http://godoc.org/github.com/BurntSushi/ty/fun