Hacker News new | ask | show | jobs
by kaba0 1265 days ago
Well, in a non-side effecting world these could still be a function of the state of the computer. mousePos(initialState) could for example be used to get the mousePos in a given state (functional reactive programming may also be interesting for you)

Another approach would be to divide the mutating, side-effecting world from the pure one by introducing a wrapper around the former. Let’s call that wrapper IO and create a few helper functions that can sequence these IO wrappers one after the other. The important part is that such a wrapper can provide a value, but that can only ever be read/seen by the next element in a sequence. So any mutating program can be described as such sequence and be executed at the end, while the program you wrote is perfectly pure. This is basically what a monad is, and a haskell function for reading the mouse position would look like mousePos() -> IO(Pos(int, int)) (in some hybrid type notation)