Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In general, you should try using immutable data types and avoid imperative constructs such as global variables and imperative loops - although using them in F# is fine in many cases, they should be used only when there is a good reason for doing so. Here are a couple of examples where you could use functional approach:</p> <hr> <p>First of all, to make the code more functional, you should avoid using global mutable cache. Instead, your <code>RefreshCache</code> function should return the data as the result (preferably using some functional data structure, such as F# <code>Map</code> type):</p> <pre><code>let PopulateCache quoteList = quoteList // Generate a sequence of tuples containing key and value |&gt; Seq.map (fun result -&gt; result.RicCode, result) // Turn the sequence into an F# immutable map (replacement for hashtable) |&gt; Map.ofSeq </code></pre> <p>The code that uses it would be changed like this:</p> <pre><code>let cache = sr.ReadLine() |&gt; ParseQuoteString |&gt; PopulateCache printfn "[F#]Quot Size, %d" m_cache.Count let quot = m_cache.["MSFT.OQ"] // The rest of the sample stays the same </code></pre> <hr> <p>In the <code>EstablishConnection</code> function, you definitely don't need to declare a mutable variable <code>sr</code>, because in case of an exception, the function will return <code>null</code>. I would instead use <code>option</code> type to make sure that this case is handled:</p> <pre><code>let EstablishConnection() = let pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4) printfn "[F#] NamedPipeServerStream thread created..." pipeServer.WaitForConnection() printfn "[F#] Client connected." try // Wrap the result in 'Some' to denote success Some(new StreamReader(pipeServer)) with e -&gt; printfn "[F#]ERROR: %s" e.Message // Return 'None' to denote a failure None </code></pre> <p>The main loop can be written using a recursive function that stops when <code>EstablishConnection</code> fails:</p> <pre><code>let rec loop() = match EstablishConnection() with | Some(conn) -&gt; printfn "[F#] Ready to Receive data" // rest of the code loop() // continue looping | _ -&gt; () // Quit </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload