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 latest dotnet SDK for NET5
  • install vscode and add all ionide extensions
  • install Microsoft.dotnet-interactive as a global tool
mkdir fsx && cd fsx; touch new.fsx; code .


Ionide is a great F# cross platform IDE plug-in for VScode, it works pretty good for F# scripting, sometimes might need you to clear cache or restart vscode though.

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

As an example task, we want to read some fields in a json file, so parse it and display the captured value on the console.

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



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


#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


import plotly.express as px

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


#r "nuget: XPlot.Plotly"

open XPlot.Plotly

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

Starting a local web server


from flask import Flask

app = Flask(__name__)

def index():
return 'Hello World!'

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


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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store