Are Databases and Functional Programming at odds?

后端 未结 10 1695
梦如初夏
梦如初夏 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.

    0 讨论(0)
  • 2020-12-12 10:13

    Databases and Functional Programming can be fused.

    for example:

    Clojure is a functional programming language based on relational database theory.

                   Clojure -> DBMS, Super Foxpro
                       STM -> Transaction,MVCC
    Persistent Collections -> db, table, col
                  hash-map -> indexed data
                     Watch -> trigger, log
                      Spec -> constraint
                  Core API -> SQL, Built-in function
                  function -> Stored Procedure
                 Meta Data -> System Table
    
    

    Note: In the latest spec2, spec is more like RMDB. see: spec-alpha2 wiki: Schema-and-select

    I advocate: Building a relational data model on top of hash-map to achieve a combination of NoSQL and RMDB advantages. This is actually a reverse implementation of posgtresql.

    Duck Typing: If it looks like a duck and quacks like a duck, it must be a duck.

    If clojure's data model like a RMDB, clojure's facilities like a RMDB and clojure's data manipulation like a RMDB, clojure must be a RMDB.

    Clojure is a functional programming language based on relational database theory

    Everything is RMDB

    Implement relational data model and programming based on hash-map (NoSQL)

    0 讨论(0)
  • 2020-12-12 10:17

    You should look at the paper "Out of the Tar Pit" by Ben Moseley and Peter Marks, available here: "Out of the Tar Pit" (Feb. 6, 2006)

    It is a modern classic which details a programming paradigm/system called Functional-Relational Programming. While not directly relating to databases, it discusses how to isolate interactions with the outside world (databases, for example) from the functional core of a system.

    The paper also discusses how to implement a system where the internal state of the application is defined and modified using a relational algebra, which obviously is related to relational databases.

    This paper will not give an an exact answer to how to integrate databases and functional programming, but it will help you design a system to minimize the problem.

    0 讨论(0)
  • 2020-12-12 10:17

    I'm most comfortable with Haskell. The most prominent Haskell web framework (comparable to Rails and Django) is called Yesod. It seems to have a pretty cool, type-safe, multi-backend ORM. Have a look at the Persistance chapter in their book.

    0 讨论(0)
提交回复
热议问题