So an F# Sequence Should Stick Around For a While

If you create a sequence in Clojure, the sequence sticks around within the scope of its creation. That is, once a Clojure sequence is created and bound to a var, nothing special has to be done to make sure its contents stick around. This is not so with F#.

Take this program


open System
open System.Collections.Generic
open System.Text
open System.IO
#nowarn "40"

let rec readLines () =
    seq {
    let line = Console.ReadLine()
    if not (line.Equals("")) then
           yield Int32.Parse(line)
           yield! readLines ()
    }

[<EntryPoint>]
let main argv =
    let inSeq = readLines ()    

    inSeq

    |> Seq.length

    |> printfn "%d lines read"

    // This will keep it alive enough to read your output
    Console.ReadKey() |> ignore
    0

Because of a sequences’ inherent laziness, the reading in of numbers does not begin until inSeq’s length is computed. Without caching inSeq, you will have to read the integers in again if you want to iterate the sequence, unless this change is made


let inSeq = readLines () |> Seq.cache

Leave a comment

Filed under Clojure, F#

Generating F# Sequences From Console Input

I got a very nice answer on SO to why a function was not executing.


open System
open System.Collections.Generic
open System.Text
open System.IO
#nowarn "40"

let rec readlines () =
     seq {
           let line = Console.ReadLine()
           if not (line.Equals("")) then
              yield line
              yield! readlines ()
}

[<EntryPoint>]
let main argv =
      let inSeq = readlines ()

      inSeq
      |> Seq.length
      |> printfn "%d lines read"

      // This will keep it alive enough to read your output
      Console.ReadKey() |> ignore
      0

Leave a comment

Filed under F#

The Sugar Coated Documentary

In May of this year, I came across an interesting documentary on Netflix. Then I recently heard about one of the people mentioned in the documentary, a dentist in San Fransisco in an NPR news story.

Even if you disagree with the documentary, at least it might make you think. Oh, and I did enjoy birthday cake several times this Summer. I am not a monk.

Leave a comment

Filed under Health

Nice F# List.fold example

Got this example from here.


open System
open System.Threading
open System.Collections.Generic
open System.Linq
open System.Text
open System.Threading.Tasks
open System.IO
open Microsoft.VisualBasic.FileIO


let main argv =
   let data = [(Cats,4);
               (Dogs,5);
               (Mice,3);
               (Elephants,2)]

   let count = List.fold (fun acc (nm,x) acc+x) 0 data
   printfn "Total number of animals: %d" count


Even though I’ve used map and other functions in Clojure, I’ve forgotten the basics of an inline function. The value of 0 is assigned to nm, and x takes on the first row of data.

 

Leave a comment

Filed under F#

Reading in a .csv file

Yes, it is my favorite subject, which is transforming data in .csv files. Here’s reading data into a variable in F#.


open System
open System.Threading
open System.Collections.Generic
open System.Linq
open System.Text
open System.Threading.Tasks
open System.IO
open Microsoft.VisualBasic.FileIO

[<EntryPoint>]
let main argv =
  let csv_fileH = new TextFieldParser(test1.csv)
  csv_fileH.TextFieldType = FieldType.Delimited |> ignore
  let x = csv_fileH.SetDelimiters(',')
  let csv_data = new List string[]()

  let eod = csv_fileH.EndOfData

  if not eod then
    let column_headings = csv_fileH.ReadFields()
    csv_data.Add(column_headings) |> ignore

    // Parentheses are needed after function definition to produce type bool.
    let read_rest_of_csv() =
    csv_data.Add(csv_fileH.ReadFields()) |>
    not csv_fileH.EndOfData

    while read_rest_of_csv() do ignore None

  0 // return an integer exit code

This program is associated on stackoverflow with some very good answers to a question I asked.

Leave a comment

Filed under F#

A First [and Simple] F# Sample

Anything worth learning should not be simple. Learning Clojure was not simple, and hacking with C# for the first time was not simple either, so I should not expect simple while learning F#.

Here is a first small program, because I have found a dirth of regular simple examples out on the web, especially those calling .Net functions.


 open System
 open System.Threading
 open System.Collections.Generic
 open System.Linq
 open System.Text
 open System.Threading.Tasks
 open System.IO
 open Microsoft.VisualBasic.FileIO

[EntryPoint]
let main argv =
    let parser = new TextFieldParser(&quot;test1.csv&quot;)
    parser.TextFieldType = FieldType.Delimited |&amp;amp;amp;gt; ignore
    let x = parser.SetDelimiters(&quot;,&quot;)

    let eod = parser.EndOfData
    if not eod then
        let column_headings = parser.ReadFields()
        printf &quot;%A&quot; column_headings |&amp;amp;amp;gt; ignore
0 // return an integer exit code

This is the contents of test1.csv

&lt;br data-mce-bogus=&quot;1&quot;&gt;

AGY/DIV,STS,GIC-ID,LAST-NAME,FIRST-NAME,COVERAGE DESCRIPTION,PREMIUM,RUN-DATE,BILL MONTH&lt;br data-mce-bogus=&quot;1&quot;&gt;

 

Leave a comment

Filed under F#

Good F# Example

After a decade of Linux database work, a lot more of my time is being spent on the Microsoft platform, using Visual Studio. Because our enterprise is on Windows 7, Visual Studio 2012 is as high as I can use.

Over the past six years, I’ve learned enough Clojure to write small applets; added to my Perl knowledge; and ported a VB application to C#.

So, I am learning F# now, and was looking for a good example. I was lucky to find one that includes posting a dialog box. I had to tweak a couple of things, because this example was written for Visual Studio 2010, but it is the first good example I have found for F#.

Leave a comment

Filed under .Net Languages

Does anyone remember Major Mudd (Greater Boston)?

Long before the Boston Red Sox reversed the curse, when I was in the 4th grade, I met my first television personality, Major Mudd https://www.youtube.com/watch?v=fT7bkXXvDLM . It was down at the South Shore Plaza in Braintree, MA. He had a weekday morning cartoon show, including a serialized non-cartoon about two brothers traveling down a river and going back in time.

We remember you well, Major Mudd.

Leave a comment

Filed under Annecdotes, Persons of Note

A Valuable Summer Job

It is because of my summer job long-ago at September Farm of Rye, NH that this afternoon I was able to lug a 50lb bag of wild bird sunflower seed out to the car.

A very nice clerk offered twice to provide help, but having once hefted bails of hay and bags of Purina Oats and Omolene for the horses, I was able to take it out to the car ,and my back is fine.

So, thanks, September Farm. Although after mucking out the stalls, I needed to — as the late Moe Howard would say shower and use cologne — working there gave me good hoisting strength.

Leave a comment

Filed under Annecdotes

Learning Less Stressful Ways of Navigating Life From Children

I work with amazing people in our municipality. One of them has a young mobile, talking, pre-school daughter, and I get to hear about her daughter’s latest sayings.

I plan on incorporating two of them in the never-ending struggle to get work done and offending as few people as possible while keeping most of them from commandeering my time.

Mom swears she does not know where her daughter picked up these phrases.

These sayings are:

No, I’m good.

Not right now.

And here are some examples:

Parent: Don’t you think it’s time for bed?

Toddler: No, I’m good.

Early Childhood Teacher: How do you feel about going to pre-school? [How do you feel about advancing a grade?].

Toddler: Not right now.

So, adapting these to municipal life:

Elected Official: Come to a meeting to discuss implementing a software solution in one week that should take a month.

Municipal Employee: Not right now.

Boss: Would you come to a meeting with me that will be like having your teeth drilled without Novocaine?

Municipal Employee: No, I’m good

Now, what do we have here? Language that can be spoken in front of children, maintaining calmness, and communicating with short, concise answers.

Leave a comment

Filed under Musings