Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Is there a "cleaner" implementation similar to map's? Yes: let map f = id >=> switch f This follows from two of your other equations: map f = bind (switch f) g >=> h = g >...
Answer
#4: Post edited
- > Is there a "cleaner" implementation similar to `map`'s?
- Yes:
- ```
- let map f = id >=> switch f
- ```
- This follows from two of your other equations:
- ```
- map f = bind (switch f)
- g >=> h = g >> bind h
- ```
- So if you want to get `bind (switch f)` out of `(>=>)`, you can start by making `h = switch f`, then get rid of the superfluous composition by letting `g` be `id` and you're done.
- ```
g >=> h = g >> bind h <------ (h = switch f)= g >=> switch f = g >> bind (switch f)= g >=> switch f = g >> map f <------ (g = id)= id >=> switch f = id >> bind (switch f)- ```
- > Is there a "cleaner" implementation similar to `map`'s?
- Yes:
- ```
- let map f = id >=> switch f
- ```
- This follows from two of your other equations:
- ```
- map f = bind (switch f)
- g >=> h = g >> bind h
- ```
- So if you want to get `bind (switch f)` out of `(>=>)`, you can start by making `h = switch f`, then get rid of the superfluous composition by letting `g` be `id` and you're done.
- ```
- g >=> h = g >> bind h <------ (h = switch f)
- g >=> switch f = g >> bind (switch f)
- g >=> switch f = g >> map f <------ (g = id)
- id >=> switch f = id >> map f
- id >=> switch f = map f
- ```
#3: Post edited
- > Is there a "cleaner" implementation similar to `map`'s?
- Yes:
- ```
- let map f = id >=> switch f
- ```
- This follows from two of your other equations:
- ```
- map f = bind (switch f)
- g >=> h = g >> bind h
- ```
So if you want to get `bind (switch f)` out of `(>=>)`, you can start by making `h = switch f`, and then you have `g >=> switch f = g >> bind (switch f) = g >> map f`. Get rid of the superfluous composition by letting `g` be `id` and you're done.
- > Is there a "cleaner" implementation similar to `map`'s?
- Yes:
- ```
- let map f = id >=> switch f
- ```
- This follows from two of your other equations:
- ```
- map f = bind (switch f)
- g >=> h = g >> bind h
- ```
- So if you want to get `bind (switch f)` out of `(>=>)`, you can start by making `h = switch f`, then get rid of the superfluous composition by letting `g` be `id` and you're done.
- ```
- g >=> h = g >> bind h <------ (h = switch f)
- = g >=> switch f = g >> bind (switch f)
- = g >=> switch f = g >> map f <------ (g = id)
- = id >=> switch f = id >> bind (switch f)
- ```
#2: Post edited
- > Is there a "cleaner" implementation similar to `map`'s?
- Yes:
- ```
- let map f = id >=> switch f
```
- > Is there a "cleaner" implementation similar to `map`'s?
- Yes:
- ```
- let map f = id >=> switch f
- ```
- This follows from two of your other equations:
- ```
- map f = bind (switch f)
- g >=> h = g >> bind h
- ```
- So if you want to get `bind (switch f)` out of `(>=>)`, you can start by making `h = switch f`, and then you have `g >=> switch f = g >> bind (switch f) = g >> map f`. Get rid of the superfluous composition by letting `g` be `id` and you're done.