fsprojects / FSharpx.Extras

Functional programming and other utilities from the original "fsharpx" project
https://fsprojects.github.io/FSharpx.Extras/
The Unlicense
682 stars 146 forks source link

Please include samples that use or explain the State monad #413

Closed sonicbhoc closed 2 years ago

sonicbhoc commented 2 years ago

Description

I feel like the State monad could be perfect for the code I'm writing, but I can't get it to to work for me. I don't understand it, and most of what I find out there is for Haskell.

Repro steps

  1. Look for examples.
  2. Find no examples.

Expected behavior

  1. Look for examples.
  2. Find examples.
gdziadkiewicz commented 2 years ago

Hi, I'm willing to provide samples. Can you share your use case?

In the meantime, I will provide a generic sample that shows how to read, write and execute the monad.

sonicbhoc commented 2 years ago

Sure. I am doing an event-sourced aggregate. I'm new to the language and the Functional Programming paradigm in general and I'm sure I can abstract away a lot more than I am now (I need to learn more about monoids and the like; hence my question).

I have 2 workflows that pass around state: one is validation and the other is actually updating the state by modifying an element of the list holding the types. I'm not even sure the state monad is the right way to do what I'm doing, honestly.

gdziadkiewicz commented 2 years ago

Can you check the draft of the generic sample in https://github.com/gdziadkiewicz/FSharpx.Extras/tree/Fix_%23413_add_state_sample ?

gdziadkiewicz commented 2 years ago

While working on it I noticed that the State monad functions are not fully documented - would be nice to fix that.

sonicbhoc commented 2 years ago

Hey, sorry for the lack of communication. I see the replies to this thread and I've had my nose to the grindstone so to speak.

The state sample really does clear things up for me. Thank you. Now that I see how it is used, I can make much more informed decisions on when and where (or if at all) to use it. I also thank you for the documented functions in that module.

I would also really appreciate #411 getting the same treatment. I'm trying to learn all of this and think in a more functional manner, but I won't pretend that some of this stuff isn't mind-bending to me.