Are Databases and Functional Programming at odds?

后端 未结 10 1747
梦如初夏
梦如初夏 2020-12-12 09:21

I\'ve been a web developer for some time now, and have recently started learning some functional programming. Like others, I\'ve had some significant trouble apply many of

10条回答
  •  离开以前
    2020-12-12 10:13

    A database is the perfect way to keep track of state in a stateless API. If you subscribe to REST, then your goal is to write stateless code that interacts with a datastore (or some other backend) that keeps track of state information in a transparent way so that your client doesn't have to.

    The idea of an Object-Relational Mapper, where you import a database record as an object and then modify it, is just as applicable and useful to functional programming as it is to object oriented programming. The one caveat is that functional programming does not modify the object in place, but the database API can allow you to modify the record in place. The control flow of your client would look something like this:

    • Import the record as an object (the database API can lock the record at this point),
    • Read the object and branch based on its contents as you like,
    • Package a new object with your desired modifications,
    • Pass the new object to the appropriate API call which updates the record on the database.

    The database will update the record with your changes. Pure functional programming might disallow reassigning variables within the scope of your program, but your database API can still allow in-place updates.

提交回复
热议问题