F#5 like a Pythonista on steroids

scripting with #r NuGet and comparing some python syntax

F#5 in action in vscode + ionide

Getting Started

  • install vscode and add all ionide extensions
  • install Microsoft.dotnet-interactive as a global tool
mkdir fsx && cd fsx; touch new.fsx; code .

VSCode and IONIDE

I will also compare a bit of F# and Python between the lines, as python is the de-facto standard for scripting in 2021.

A function in Python

def sum(a,b): 
return a + b

The same function in F#

let sum a b = a + b // or quicker, let sum = (+)

A class in Python

class Dog:
species = "Canis familiaris"

def __init__(self, name, age):
self.name = name
self.age = age

def description(self):
return f"{self.name} is {self.age} years old"

def speak(self, sound):
return f"{self.name} says {sound}"

A class in F#

type Dog(name,age) =
member val Description =
$"{name} is {age} years old"
member this.Speak(sound) =
$"{name} says {sound}"
member val Species = "canis familiaris"

Reading a JSON file

In Python

import jsonwith open('test.json', 'r') as myfile:data = myfile.read()obj = json.loads(data)print(obj["hello"])

In F# with JsonParser

#r "nuget: FSharp.Data"open FSharp.Dataopen FSharp.Data.JsonExtensions // ? oplet result = 
(__SOURCE_DIRECTORY__ + "/test.json")
|> System.IO.File.ReadAllText
|> JsonValue.Parse
printfn $"{result?Hello}"

In F# with Type Providers

#r "nuget: FSharp.Data"open FSharp.Data[<Literal>]let jsonFilePath = __SOURCE_DIRECTORY__ + @"/test.json"type MyJson = JsonProvider<jsonFilePath>let result = MyJson.Load(__SOURCE_DIRECTORY__ + "/prod.json")printfn $"{result.Hello}" //strongly typed!

Reading a CSV file

Name,Surname,Age
John,Red,33
Mike,Bianchi,55
...

Python

import csv

with open('test.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')

for row in csv_reader:
print(row[0]) //Name

F#

#r "nuget: FSharp.Data"open FSharp.Data[<Literal>]let csvPath = __SOURCE_DIRECTORY__ + "/test.csv"type MyCsv = CsvProvider<csvPath, ",">for row in MyCsv.GetSample().Rows do   printfn $"{row.Name}" // first column header = Name

Plotting some lines with Plotly

Python

import plotly.express as px

df = list(range(1, 11)) //1...10
fig = px.line(df)
fig.show()

F#

#r "nuget: XPlot.Plotly"

open XPlot.Plotly

[ 1 .. 10 ] |> Chart.Line |> Chart.Show

Starting a local web server

Python

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
return 'Hello World!'

app.run(host='0.0.0.0', port=81)

F#

#r "nuget: Suave"open Suave  startWebServer defaultConfig (Successful.OK "Hello World!")