News for hackers. Stuff that matters.
Re:Safe dead code removal in a pure functional language by brundolf on Thursday January 28, 2021 @05:58PM (#25950744)
Where my mind initially goes is to have a pure function going from id -> records, and then pass those records off to a separate procedure that persists them to the database. The same records can then also be passed around to the UI or whatever else.

> Often this would get called several times for the same declaration id, usually generating new records, so it's easier to have the function return the id's rather than trying to reconstruct what it did.

Are the "new" records meaningfully different in some way, or are they just new instances in memory of what is fundamentally the same value? In the former case, there's state somewhere that's causing each one to be different (there is not a pure relationship from id -> record set). In the latter case, you don't actually need multiple instances (unless you expect them to be mutated by other code down the line and are guarding against side-effects, in which case you could defensively clone the pure function's result as needed instead of calling it multiple times).

Edit: it's possible I misunderstood, and that what's happening is you're inserting data into a database which has its own auto-incrementing IDs that it then returns to you. If so, the coupling between value creation and state/mutation exists in the database's API itself, and so is outside of your control. There's not much you can do in that case because you don't own that code.

Re:Safe dead code removal in a pure functional language by magicalhippo on Thursday January 28, 2021 @06:19PM (#25950946)
Thanks for the response, I enjoy challenging my procedural upbringing.

> Are the "new" records meaningfully different in some way

Yeah usually. A typical example is something external added data to the declaration, and new records are generated to reflect this.

And yeah, auto-incs is the norm.

Re:Safe dead code removal in a pure functional language by brundolf on Thursday January 28, 2021 @06:41PM (#25951147)
Yeah the overall mindset is just to look for pieces of the form "convert values A..X into value Z", which can be lopped off into a pure function. Wherever you can do this, you probably want to.

But this generally only works if all of the information going into Z is under your code's purview. If the auto-increment were happening in your code instead of in the database then you could make the records a pure function of the domain data + the next auto-incremented ID, and the procedural code which calls the pure function would retrieve the ID and increment it, call the pure function with it to generate the records, and then call another impure function to persist them. But of course moving the auto-increment logic and state from the DB to your application code is a huge undertaking involving many other trade-offs, and is not necessarily recommended just for the sake of something like this.

Re:Safe dead code removal in a pure functional language by Akronymus on Friday January 29, 2021 @05:23AM (#25955899)
>...clone the pure function's result as needed instead of calling it multiple times

It is reasonably easy to do a caching function that takes arbitrary functions and data and caches their result as a map of tuples (data, function, timestamp for example). Definitely not always the right thing to do, but could eek out some performance in where you get the same data multiple times.

Edit: I found that it is called memoization: Looks to me to very easily be extensible to allow timestamps too, to check if the record is older than X.

https://stackoverflow.com/questions/833180/handy-f-snippets/...

Re:Safe dead code removal in a pure functional language by brundolf on Friday January 29, 2021 @10:37AM (#25958306)
Yes, but:

> unless you expect them to be mutated by other code down the line and are guarding against side-effects

Sometimes you hand off a mutable object to some other code and that code mutates it for its own purposes. If you hand the same object to multiple functions in this case, they'll all see each other's changes and if this wasn't intentional then voila, you've got a bug. This is one motivation for immutable data structures: you don't have to worry about whether an object might get mutated so you don't have to eagerly clone it. But in an existing codebase it may not be a trivial thing to simply introduce immutability. This is why, instead, I suggested making that defensive cloning explicit and decoupled from the rest of the logic.

Re:Safe dead code removal in a pure functional language by Akronymus on Monday February 01, 2021 @02:35AM (#25986455)
In that case, you could adjust the memoize function to return a copy. Altough that would lead to other problems such as shallow or deep copy and such.