Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 9: strings aren't functors – suggest using trees instead #179

Open
glebec opened this issue Mar 22, 2019 · 1 comment
Open

Chapter 9: strings aren't functors – suggest using trees instead #179

glebec opened this issue Mar 22, 2019 · 1 comment

Comments

@glebec
Copy link

glebec commented Mar 22, 2019

In #72 @DrBoolean already pointed this out, but since that (closed) issue addressed several sub-issues I think it's ok to make a single issue for this.

In (now-)Chapter 9, you use String as an example of functors. However, Strings aren't actually functors, because the things inside them cannot be any arbitrary type. In other words, there is no such thing as a "String of type x" – strings are always composed of characters and nothing else.

-- an `Array of a` can be transformed into an `Array of b`
Array.prototype.map :: [a] ~> (a -> b) -> [b]

-- a `String of chars` cannot be transformed into a `String of <other>`
String.prototype.mapLike :: String char ~> (char -> b) -> String char
-- (`b` values are turned into chars implicitly)

Instead of strings, I might suggest that the most intuitive example (for JS devs) of a functor apart from Arrays would be Trees.

-- a `Tree of a` can be transformed into a `Tree of b`
Tree.prototype.map :: Tree a ~> (a -> b) -> Tree b

The downside, unfortunately, is that trees are not a built-in first-class data structure, but must be implemented. Of course, that is not especially complicated, though the details (class-based? factory? prototypal map method? top-level map function?) will depend on your particular taste.

Other examples… Promise acts as a functor sometimes, though not all the time (due to the difference between returning values vs. returning promises in then); event emitters / RxJS observables can act as functors, though these are even farther from vanilla JS built-ins; functional idioms like Maybe, Either, Tuple… personally, I think Tree is a better candidate than these less-universally-familiar entities.

@glebec glebec changed the title Strings aren't functors – suggest using trees instead Chapter 9: strings aren't functors – suggest using trees instead Mar 22, 2019
@getify
Copy link
Owner

getify commented Mar 22, 2019

Thanks for the suggestion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants