Skip to content

Instantly share code, notes, and snippets.

View Thorium's full-sized avatar

Tuomas Hietanen Thorium

View GitHub Profile
@Thorium
Thorium / AbstractProject.fs
Last active February 16, 2024 15:23
Use (or replace) existing Logary via Microsoft.Extensions.Logging.Abstractions. This can be used to abstract Logary behind Microsoft.Extensions.Logging so it's easier to remove from existing infra (or migrate to, whatever).
// This expects you have 2 projects, this first one has dependendency only to Microsoft.Extensions.Logging.Abstractions
// (and temporarily usage of Microsoft.Extensions.Logging.Console ).
module AbstractProject
let loggerFactory =
Microsoft.Extensions.Logging.LoggerFactory.Create(fun builder ->
let _ = Microsoft.Extensions.Logging.ConsoleLoggerExtensions.AddSimpleConsole builder
())
/// This is ILogger.
@Thorium
Thorium / readJson.fsx
Created November 16, 2023 23:12
Using Utf8JsonReader from System.Text.Json with FSharp
//High-performance JSON parsing with System.Text.Json and F#
// Example translated to F# from
// https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/use-utf8jsonreader
#r "nuget:System.Text.Json"
open System
open System.Text
open System.Text.Json
let options = JsonReaderOptions(
@Thorium
Thorium / gist:1972349
Created March 4, 2012 10:43
Timestamp with timezone (YYYYMMDDhhmmssffff+zzzz)
let myTimeStamp =
let zone = System.TimeZone.CurrentTimeZone.GetUtcOffset System.DateTime.Now
let prefix = match (zone<System.TimeSpan.Zero) with | true -> "-" | _ -> "+"
System.DateTime.UtcNow.ToString("yyyyMMddHHmmssffff") + prefix + zone.ToString("hhss");
@Thorium
Thorium / bindingredirects.fsx
Last active February 14, 2023 13:46
Parse bindingRedirects with Linq2Xml
// Find binding-redirects with linq-to-xml from a .config file.
// This might be useful for then parsing config-changes e.g. by System.Linq.Enumerable.Except
#r @"System.Xml.Linq.dll"
open System.Xml.Linq
let parseBindingRedirects (filename:string) =
let xn s = XName.Get(s,"urn:schemas-microsoft-com:asm.v1")
let xml = XDocument.Load filename
let depAssemblies = xml.Descendants(xn "dependentAssembly")
seq {
@Thorium
Thorium / myvisitor.fs
Created November 8, 2019 14:11
F# LINQ-Expression-tree visitor
// A simple expression tree visitor, to inject a lambda inside another lambda parameters.
open Microsoft.FSharp.Linq.RuntimeHelpers
open System
open System.Linq.Expressions
//F#-helper method for Linq.Expressions: fssnip.net/ts/title/F-lambda-to-C-LINQ-Expression
module Lambda =
let toExpression (``f# lambda`` : Quotations.Expr<'a>) =
``f# lambda``
|> LeafExpressionConverter.QuotationToExpression
@Thorium
Thorium / BtcBalance.fs
Created February 2, 2017 21:54
Get Bitcoin wallet account balance by public key
// Using FSharp.Data
type WalletData =
FSharp.Data.JsonProvider<
"""{"unspent_outputs":[{"value":9000000000},{"value":10}]}""">
let getBalance publicKey =
let balance =
try
WalletData.Load(
@Thorium
Thorium / BtcPrice.fsx
Created February 2, 2017 18:14
Bitcoin current price data from blockchain.info
//Using FSharp.Data
type BtcData = FSharp.Data.JsonProvider<"""{
"USD":{"15m":1.1,"last":1.1,"buy":1.1,"sell":1.1,"symbol":"$"},
"EUR":{"15m":1.1,"last":1.1,"buy":1.1,"sell":1.1,"symbol":"€"},
"GBP":{"15m":1.1,"last":1.1,"buy":1.1,"sell":1.1,"symbol":"£"}
}""">
let prices = BtcData.Load("https://blockchain.info/ticker")
//prices.Eur.Buy : val it : decimal = 923.52M (at 02/02/2017)
//prices.Gbp.Sell : val it : decimal = 794.61M (at 02/02/2017)
@Thorium
Thorium / tiktok.ts
Created April 7, 2022 17:10
TikTok Pixel tag valid TypeScript / JavaScript
function tiktokTag() {
try {
window.TiktokAnalyticsObject = 'ttq';
var ttq = window.ttq = window.ttq || [];
ttq.methods = ["page", "track", "identify", "instances", "debug", "on", "off", "once", "ready", "alias", "group", "enableCookie", "disableCookie"];
ttq.setAndDefer = function(t, e) {
t[e] = function() {
t.push([e].concat(Array.prototype.slice.call(arguments, 0)))
}
};
@Thorium
Thorium / GoogleAnalytics.ts
Last active March 16, 2022 22:09
Google Analytics Typescript
/// <reference path="./../../paket-files/borisyankov/DefinitelyTyped/google.analytics/ga.d.ts" />
export var gaNewElem : any = {};
export var gaElems : any = {};
function gaInit(){
var currdate : any = new Date();
/* tslint:disable:no-string-literal */
/* tslint:disable:semicolon */
@Thorium
Thorium / MapTools.fsx
Created February 7, 2022 19:12
KML / Google Maps / Oculus Wander
/// Set of little F# (FSharp) functions as script tools to work with
/// - KML files (Keyhole Markup Language) used by Google Earth and Google Maps
/// - Google Maps Streetview API,
/// - Oculus Wander Application (by Parkline Interactive, LLC) bookmarks: Wander_Favorites.json
/// You have to first set your Google API key to environment variables as api-key and enable
/// Streetview API for that
// System.Environment.SetEnvironmentVariable("api-key", "...")
#r "nuget: FSharp.Data"