8 ms·
Impureim Sandwich (2020)
- mumblemumble 3y agoThe impureim sandwich is often thought of as a sort of impractical purism that only a Haskell programmer could love. But it's really quite useful in imperative and object-oriented code, too. For example, in typical enterprise programming there are quite a few elaborate and complicated practices that aren't needed to nearly the same extent (if at all) when the code is structured as an impureim sandwich. Dependency injection, mockist testing, service locators, and singletons all come to mind.
- ttoinou 3y agoThis is also a mathematical tool you can use every time you're making maths. If you have two bijective set, and you have some functions in only one of those sets, you first transform the set with the bijective function, operate in the second set, retransform back with the reverse of the bijective function, given X and Y spaces g(X)=f_to_g( f( Y = g_to_f( X ) ) ) For example you can have operations in a normalized space. You first normalize your data, operate in it, then rescale your result back to the original space. For example you want to apply square root to a normalized variable (if you don't normalize, you don't know if the function will increase or decrease your variable, between 0 and 1 square root always increases the value). If you know your max is N, you should no N.sqrt(X/N). This might seem obvious to you ? But what if you had f(x)=cos(pi.x^3.5) instead of f=sqrt.. ? Also, operating in a normalized space you can chain your functions. Yeah, just like chaining functions in functional programming. (Using dot . to multiply because of HN formatting) You know how to do an arithmetic mean / average in maths ? Well, if you chain that with a change of space with the power functions, you'll get all others means (minimum, maximum, harmonic mean, quadratic, even the geometric mean using exp and log !). Now if you prove something in your normal arithmetic mean, you might be able to apply this easily in your new mean that you created by chaining a bijective change of space. See Generalized mean https://en.wikipedia.org/wiki/Generalized_mean https://en.wikipedia.org/wiki/Generalized_mean In physics you can remove the units and work only with dimensionless quantities https://en.wikipedia.org/wiki/List_of_dimensionless_quantities https://en.wikipedia.org/wiki/List_of_dimensionless_quantiti... and operate with them and only get back to real physics units when you need to "get back to the real world" In color theory, you might want to rotate the hue ? Create a bijection between RGB and a new colorspace that has hue like HSL or HSV (or more complex if you're ready to fight with more maths), RotateHueRGB(colorRGB,hue)=HSVtoRGB( RGBtoHSV( colorRGB ) + (hue,0,0) ) I could go on an on for weeks about this technique. It is fundamental to me to learning and understanding Science, no idea why so few people are talking about this. I didn't learn this thanks to him so I can't 100% confirm the information I didn't read the book yet, but I remember some french maths popularizer / youtuber who called that "The Umbrella Principle" and wrote a book about it ("Le théorème du parapluie" Mickaël Launay - Micmaths channel on YouTube https://www.youtube.com/channel/UC4PasDd25MXqlXBogBw9CAg https://www.youtube.com/channel/UC4PasDd25MXqlXBogBw9CAg ). Umbrella because you're getting your umbrella out (first layer of the bread), walk under the rain without getting wet (the meat), then close your umbrella (final layer of bread). Enjoy ! Let me know what others applications you find ;)
- corethree 3y agoThis concept you're describing is formalized in mathematics. In category theory it is called a functor. In haskell it is an actual language primitive also called a functor. Note that a functor, however, is more broad. The relationship between the two "subjects" does not necessarily need to be bijective. It can be one way. There are conversions to new spaces with no direct way to convert back the the original entity in the old space. Quaternions and euler angles are an example of this one way conversion where there are multiple possibilities from euler to quat. What's being talked about here is very different from the concept you describe although they are both part of functional programming.
- hansvm 3y agoIt's also useful if your transformations aren't bijective. A lot of hyperreal proofs look like (a) Push your real-valued problem to the hyperreals, (b) Prove something interesting, and (c) push some subset/transformation of that interesting thing back down to the reals. As a very concrete example (eliding details for obvious reasons), to prove Kőnig's lemma you can (0) Start with a connected, infinite, locally finite graph, (a) Embed that infinite graph in any properly nonstandard extension thereof, (b) Use the transfer principle to find an infinite "hyper" path (no longer a path because it's indexed by the hypernaturals), and (c) Project that back to a standard path in the subgraph induced by restricting to the original infinite graph. That is precisely the infinite path you were looking for. Not that Kőnig's lemma is hard to prove with standard techniques per se, but it illustrates the point.
- cdcarter 3y agoIf you haven't seen it, it's worth checking out Gary Bernhardt's "Functional Core, Imperative Shell" stream, which gets into how to apply this idea. https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell https://www.destroyallsoftware.com/screencasts/catalog/funct...
- svat 3y agoAlong similar lines, see also "Improve your code by separating mechanism from policy" https://lambdaisland.com/blog/2022-03-10-mechanism-vs-policy https://lambdaisland.com/blog/2022-03-10-mechanism-vs-policy The connection between all three is to put the "pure" / "mechanism" / "functional" code in middle, and the "impure" / "policy" / "imperative" code on the outside. (The concepts are not identical, but there is substantial overlap.) (There is also some overlap with John Ousterhout's A Philosophy of Software Design idea of "deep modules": don't put policy stuff, i.e. arbitrary decisions, inside the module.)
- bafe 3y agoIsn't this in essence the idea behind effects systems, where you reify side effects, manipulate them as values and execute them "at the edge" of your program?
- jeffreygoesto 3y agoCan't help but draw an analogy to Gauss' law in electrodynamics. Pure functions have a divergence of zero, they can transport stuff but neither create nor destroy.
- corethree 3y agoOn hacker news someone told me a another name for this pattern: "Functional core, imperative shell". edit: I see other people have mentioned it. One of the biggest issues with this pattern is if there's a ton of IO that's necessary for the application. If that's the case you'll see that the "bread" of the sandwich's is gigantic while the "meat" is very small and tiny. This is the case with most web applications. The bread is sort of the main problem though but with this pattern we don't actually attempt to solve that problem, instead we just try to segregate the main problem away from pure code that intrinsically doesn't have that problem anyway. Don't get me wrong. The segregation is correct, but the problem of IO and mutating state is still unsolved.
- jfoutz 3y agoIt’s unsolved. The bread part is poker, and the meat part is chess. Yes, there is a ton of io, but the meat can pick the strategy. Try this call no more than 3 times in 60ms, and get back to me. It’s complicated and frustrating and not always worth it, but even with lots of io, the logic, the strategy, can live in the pure compositional parts. Your mileage may vary, but it’s often worth a little thought at least.
- corethree 3y agoOf course. Nowadays though it's mostly metadata programming. Your haskell code is writing code for your database query. So you end up using functional code to do tons of IO and the data it sends to IO is just more code.
- lmm 3y agoThe IO doesn't usually involve business logic, almost by definition. A web service can usually make a pretty good sandwich if it's actually doing anything - you do a thin state fetch, a thick business computation on that state, and then a thin write / return to the user. There are definitely cases where your business logic and your I/O are more tightly interleaved so that you can't really separate them the same way, and the post mentions that and links to https://blog.ploeh.dk/2017/07/10/pure-interactions/ https://blog.ploeh.dk/2017/07/10/pure-interactions/ as a deeper guide for those cases.