Author Archives: Octopusgrabbus

About Octopusgrabbus

Works as a programmer in municipal information technology, performing database and web programming, systems integration, and data conversion.

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

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

  let eod = csv_fileH.EndOfData

  if not eod then
    let column_headings = csv_fileH.ReadFields()
    csv_data.Add(column_headings) |&amp;gt; ignore

    // Parentheses are needed after function definition to produce type bool.
    let read_rest_of_csv() =
    csv_data.Add(csv_fileH.ReadFields()) |&amp;gt; ignore
    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