Updating nested immutable data structures

后端 未结 5 1872
长情又很酷
长情又很酷 2020-12-14 02:39

I want to update a nested, immutable data structure (I attached a small example of a hypothetical game.) And I wonder whether this can be done a little more elegantly.

5条回答
  •  借酒劲吻你
    2020-12-14 03:13

    I asked a similar question, but about haskell: Is there a Haskell idiom for updating a nested data structure?

    The excellent answers mentioned a concept known as functional lenses.


    Unfortunately, I don't know what the package is, or if it even exists, for F#.

    Update: two knowledgeable F#-ists (F#-ers? F#as?) left useful links about this in comments, so I'll post them here:

    • @TomasPetricek suggested FSharpX and this website describing it

    • @RyanRiley gave the link for the package

    It's awesome that these two guys took the time to read my answer, comment and improve it, as they're both developers of FSharpX!


    More extraneous information: I was motivated to figure out how to do this by Clojure's assoc-in and update-in functions, which proved to me that it is possible in functional languages! Of course, Clojure's dynamic typing makes it simpler than in Haskell/F#. Haskell's solution involves templating, I believe.

提交回复
热议问题