by chatmasta · 8y ago · 0 comments
Here [0] is an example of how I've used this technique in functional JavaScript code. Please don't judge too hard as this was my first JS library 2+ years ago... certainly lots of problems in there (although it has been running untouched in production since then!)
[0] https://github.com/milesrichardson/kewt.js/blob/master/kewt....
For example:
No, I don't need a "cartoon intro to DNS over HTTP" to understand basic networking concepts. Nor do I need a story about "Dr Frankenfunctor and the Monadster" to understand functional programming concepts. Even when I was a teenager reading about OOP, I didn't want to read about the "Car class inheriting from the Vehicle class" or the "Dog class inheriting from the Animal class."
When reading technical content, I always prefer prose that treats its readers as professionals, rather than five-year-olds incapable of understanding complexity without resorting to vacuous and incomplete analogies. The problem is that complex topics are complex. By their very nature they rarely fit into convoluted analogies, and would be better explained by simple use cases and examples.
As this huge, detailed, functional programming resource demonstrates, it's perfectly possible to write clear and detailed functional programming guides without that jargon.
F# for Fuin and profit has oodles of pragmatic, every-day, help and information in addition to a repository of relatively 'esoteric' issues for FP (particularly on .NET), for work-a-day coders. It's a deeply accessible guide and does an outstanding job of reframing techniques unknown to OOP devs in terms they understand while leaving deeper mathematical issues to their appropriate forums.
I'm also gonna take issue with calling FP terms "math-babble". It's just called "math" ;)
Many others may prefer something in between "cartoon intros" and cs graduate school publications.
that said, this seems like it could potentially be a thought-terminating cliché:
> The problem is that complex topics are complex.
I cannot see the reason for featuring javascript in this context.
A promise would be the case in an async functions but railway like presented work in sync functions.
Promises also sorta recursively build up and tear down (IIRC) while railway is a "pass along" situation; each function that generates and error calls the next function with the error where you then simply pass it along (or handle it).
Not sure what you mean by "recursively build up and tear down" but this JS snippet sure looks a lot like the F# one in the slides
const updateCustomerWithErrorHandling = (...args) =>
receiveRequest(...args)
.then(validateRequest)
.then(canonicalizeEmail)
.then(updateDbFromRequest)
.then(sendEmail)
.then(handleSuccess, handleError)
Here, each of the functions can return a result (async or not) or reject with Promise.reject or by throwing. If any of the functions rejects, the rest of the functions in this "happy path" are not called.Consider code like this:
p1
.then((value) => {
console.log(value);
return Promise.reject('oh, no!');
})
.catch((e) => {
console.log(e);
// Handle the error if you want, and keep going
return Promise.resolve()
})
.then(() => {
console.log('This still gets executed')
})
How is this not amenable to the railway pattern? You define how you handle the error within a function in the promise chain. Error handlers are fully composable with success handlers, which seems to be the important element necessary for implementing the railway pattern.Rxjs streams are monads because you can map a stream to a stream which wraps the inner stream in a source stream, which you can then flatMap.
That's not quite true. `then` is a weird hybrid of `map` and `bind`. If its callback returns a Promise it acts like `bind` (you don't get a nested Promise), but otherwise it acts like `map`.
And to be pedantic, Promise is neither a functor nor monad because `then` doesn't follow the necessary laws.
The railway oriented approach is then looking at neat ways of working with these kind of results in the place of try/catch when dealing with errors.