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

Temporary Workaround When Multiple Python Versions Cause Problems

I wrote and maintain an AMR (automated meter reading) store/transfer system. The system is implemented in Python, and has both command line and Django components, which share Python modules. Django is using Python 2.6.6, and the command line version is using 2.7.3.

Last Spring, I had to use pysftp and wound up breaking the web application. There is no pysftp installed for Python 2.6.6.

Eventually, this server is going to be rebuilt, but, until it is, I restored the web site using a bit of a kludge, by performing this workaround. The web site (Django) component does not use pysftp.

It’s funny that until you try something, you don’t know if it will work. Here is the workaround:


import sys
import os

if our_python_version >= (2,7,3):
   import pysftp

Since Django was built using Python 2.6.6 and no Django modules use sftp this is a good workaround, until I can rebuild this server.

This is what I like about these more modern languages, like PythonClojure, and even languages like Perl. You can interdict during a module load.

Leave a comment

Filed under Automated Meter Reading, Django, python

Baklavillin Walnuttobutericin

A Baklava Safety Warning

A Baklava Safety Warning

It is that time of year. There are a lot of Greek festivals at local churches. I have always had very good baklava from those festivals. Home made baklava  is made from the pure stuff, honey, walnuts, butter, and filo dough. Some people even use rose water, though it is rare. A friend of ours who makes baklava put this warning on a package of home made. I took the warning seriously.

Leave a comment

Filed under Musings

Greater Boston Bone Marrow Drive

https://octopusgrabbus.wordpress.com/2015/03/14/bone-marrow-drive/

Leave a comment

Filed under General