Skip to content

Instantly share code, notes, and snippets.

@jhewlett
jhewlett / Courses.fs
Last active June 6, 2021 17:57
Example of interacting with Elasticsearch via REST calls in F#
// Domain-specific module that uses the lower-level Elastic module
namespace RawElastic
open Thoth.Json.Net
open System.Net.Http
open System
module String =
let split (sep : string) (target : string) =
@jhewlett
jhewlett / iots-slonik.ts
Last active July 8, 2021 17:25
io-ts + slonik
import {
TaggedTemplateLiteralInvocationType,
QueryResultRowType,
DatabasePoolType
} from 'slonik'
import * as t from 'io-ts'
import { Either } from 'fp-ts/Either'
const query = async <T>(
sqlQuery: TaggedTemplateLiteralInvocationType<QueryResultRowType>,
@jhewlett
jhewlett / HttpClient.FSharp.fs
Last active May 14, 2023 15:42
Functional wrapper around System.Net.Http.HttpClient. Inspired in part by Http.fs (https://github.com/haf/Http.fs) and FSharp.Data (https://fsharp.github.io/FSharp.Data/library/Http.html)
namespace HttpClient.FSharp
open System
open System.Net.Http
type HttpMethod =
| Post
| Put
| Delete
| Get
@jhewlett
jhewlett / f#-data-access.fs
Created February 23, 2020 12:13
F# Data Access Example
//db.fs - helpers
module Db =
open System.Data.SQLite
open System.Data
open Dapper
let createConnectionFactory (connectionString : string) =
let createConnection () =
new SQLiteConnection(connectionString) :> IDbConnection
@jhewlett
jhewlett / pure-di-with-lifetimes.fs
Created January 27, 2020 06:21
Pure DI in F# ASP.NET Core Web API, with examples of how to handle singleton and per-request disposables
namespace PureDI
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Microsoft.AspNetCore.Mvc
open Microsoft.AspNetCore.Mvc.Controllers
open Microsoft.Extensions.Logging
open Microsoft.AspNetCore
open Microsoft.AspNetCore.Hosting
@jhewlett
jhewlett / pure-di.fs
Last active January 26, 2020 03:25
Pure DI in F# ASP.NET Core Web API
namespace PureDI
open Microsoft.AspNetCore.Builder
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Microsoft.AspNetCore.Mvc
open Microsoft.AspNetCore.Mvc.Controllers
open Microsoft.AspNetCore.Hosting
type Person = {
@jhewlett
jhewlett / new-fsharp-kata.sh
Created April 8, 2019 03:39
Template for F# Coding Exercies (simple class library and test project)
mkdir Kata
cd Kata
dotnet new sln
mkdir App
cd App
dotnet new classlib -lang F#
@jhewlett
jhewlett / combined-sequence.fs
Last active December 18, 2018 19:43
Combined sequence of checks
let finalResult =
seq {
yield firstCheckResult ()
yield secondCheckResult ()
yield thirdCheckResult ()
}
|> combineResults
@jhewlett
jhewlett / combine-results.fs
Last active December 18, 2018 19:44
Combine results
let combineResults (results : Result<unit, NotAuthorized> seq) : Result<unit, NotAuthorized> =
results
|> Seq.tryFind (fun r ->
match r with
| Ok () -> true
| Error _ -> false)
|> Option.defaultValue (Error NotAuthorized)
@jhewlett
jhewlett / sequence-checks.fs
Last active December 18, 2018 19:44
Sequence of checks
let allResults : Result<unit, NotAuthorized> seq =
seq {
yield firstCheckResult ()
yield secondCheckResult ()
yield thirdCheckResult ()
}