title: What is the appropriate code structure in F# when you need to keep data around?
body: <p>I have now about 10 full days of experience with F#. I am porting one of our C# libraries both as practice but also because it really needed a rewrite.</p>
  
  <p>The library is a utility library that connects to a socket, processes the incoming data and allows queries. The library maintains a state which is regularly updated through the socket events.</p>
  
  <p>I have read (everywhere) that in functional languages, classes are not really appropriate and I should pass things I need from one call to the next.</p>
  
  <p>However, in the case of a library like this, there are things that have to stay in place and be available as needed.</p>
  
  <p>One example is the socket handle itself, and also the state of the data that has been processed.</p>
  
  <p>This is an example of code which lends itself very well to object oriented design, but it seems to go against some of the F# principles I read.</p>
  
  <p>So, in the F# world:
   - Is it accepted that some objects have to be stored, accessible globally and partially mutated? I can't recreate 10kb of data because 2 bytes changed. And the socket messages tell me what fields to change, what objects to add / update / delete from lists.
   - Is there a common accepted way, state based code is structured?</p>
  
  <p>Currently I have made a master class that contains sub-classes; each of them processing a category of messages. This is a very OO design, but that's the world I come from.</p>
  
  <p>With F# constructs and with the lack of partial classes, I felt like it was the ideal way to break down something large in small components.
  The library is dealing with stock trading, so to give an example, the class hierarchy is like:</p>
  
  <pre><code>MasterClass
  -&gt; State of our portfolio (A)
  -&gt; Socket handling (B)
  -&gt; Processing currency changes class, called by events from B, updating A 
  -&gt; Processing trades changes class, called by events from B, updating A 
  -&gt; Processing order changes class, called by events from B, updating A 
  ... etc
  </code></pre>
  
  <p>Is there a more 'F#' way to do this?</p>
  
  <p>To clarify and make sure this question doesn't get arbitrarily closed: I'm specifically looking for F# examples, or guidelines, that allow to implement the kind of system described above in a 'F#' way, because right now, I'm still thinking OO and I can't find resources showing how to structure something complex in F#. Small examples, yes, lots of them, but a complex real-world use case, I haven't found so far.</p>
  
  <hr>
  
  <p>Edit:
  Here is some clarification about how the code is used:</p>
  
  <p>It is a library used to track the state of our accounts, as well as market situation, on a trading platform.</p>
  
  <p>The code receives 7 categories of messages, through sockets, from the platform. Each message category has its own content, format, etc as they deal with different aspects of the state (positions held, last transactions, open orders, etc).
  All messages are sent in full, to get the full state, upon connection, and then only deltas for each message as size / speed is critical; there is rarely more than 10 messages per second.</p>
  
  <p>In the C# code, each message type has its own handler with its own class and own state object. The different message types do not share anything and have very different structures. The code receiving the socket messages parses the type and dispatches the message to the right handler.</p>
  
  <p>The states are kept in ram and important data is pushed to InfluxDB for visualization.</p>
  
  <p>One a message is decoded, an event is triggered to alert any listener.</p>
  
  <p>So essentially, I'm trying to replicate 7 classes that get / parse a message, apply delta update to their state and alert a listener. Each of these classes is also performing some calculations, etc and they are roughly 5-6 pages of C# code each.</p>
  
  <p>My question is if I should keep a similar structure in F# where one large object (the socket listener) has 7 classes that each handle a message type, have mutable storage because a delta update can sometimes be tiny, or is there any language construct that would allow me to make one large object, spread on several files; possibly this would mean less code and yet retain readability, but I'm not sure how this is typically done in F#.</p>