Skip to content

Instantly share code, notes, and snippets.

@SaahilClaypool
SaahilClaypool / output.txt
Created December 7, 2024 04:14
Go Function calling, but with reflection to auto create functions.
> What is the difference in weather between San Fran and Chicago in C
calling open ai
Function call: getWeather({"Location": "San Francisco"}) -> Cold, 22 C
Function call: getWeather({"Location": "Chicago"}) -> Sunny, 25 C
calling open ai
Function call: add({"Left":25,"Right":-22}) -> 3
calling open ai
@SaahilClaypool
SaahilClaypool / DapperConstructorSourceGenerator.cs
Created May 31, 2023 19:31
Dapper constructor source generator
global using Microsoft.CodeAnalysis;
global using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Diagnostics;
namespace Srcgen;
/// <summary>
/// Author: Saahil Claypool
/// https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md
@SaahilClaypool
SaahilClaypool / PollyRateLimiter.cs
Last active February 17, 2023 15:18
Polly Rate Limiting with Retry
using Polly;
using Polly.RateLimit;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
var task = (int i) => Console.WriteLine($"Firing {i}");
var rateLimit = Policy.RateLimitAsync(10, TimeSpan.FromMinutes(1), 5);
@SaahilClaypool
SaahilClaypool / AsyncHelper.cs
Created June 26, 2022 23:07
Sync over Async Helper
// Copyright (c) Microsoft Corporation, Inc. All rights reserved.
// Copyright (c) Saahil Claypool
// Licensed under the MIT License
namespace SyncOverAsync;
public static class AsyncHelper
{
private static readonly TaskFactory _myTaskFactory =
new(CancellationToken.None,
@SaahilClaypool
SaahilClaypool / either_maybe.cs
Last active June 21, 2022 01:37
EitherMaybe
public struct Maybe<T>
{
private readonly T _value;
public T Value { get => IsSome ? _value : throw new NullReferenceException(); }
public bool IsSome { get; set; }
public Maybe()
{
IsSome = false;
_value = default!;
@SaahilClaypool
SaahilClaypool / Result.cs
Last active September 20, 2024 07:42
C# Result & Option type
using System;
namespace Utils
{
public struct Option<T>
{
public T Value { get; set; }
public bool HasValue { get; set; }
private Option(T value, bool hasValue)
@SaahilClaypool
SaahilClaypool / UnitTests.csproj
Created April 25, 2021 01:59
In-project unit tests c#
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<GenerateProgramFile>false</GenerateProgramFile>
<!-- This stops c# from creating th stub main automatically -->
<!-- https://andrewlock.net/fixing-the-error-program-has-more-than-one-entry-point-defined-for-console-apps-containing-xunit-tests/ -->
</PropertyGroup>
@SaahilClaypool
SaahilClaypool / private-build-plans.toml
Last active June 6, 2021 16:27
Iosevka build plans
[buildPlans.iosevka-saapro]
family = "Iosevka SaaPro"
desc = "Hybrid, like iA Writer’s Duo"
spacing = "quasi-proportional"
no-cv-ss = true
no-ligation = false
snapshotFamily = 'iosevka-saapro'
[buildPlans.iosevka-saapro.variants]
inherits = "ss20"
@SaahilClaypool
SaahilClaypool / JWT_Startup.cs
Created January 12, 2021 19:22
Google JWT (without secret validation)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@SaahilClaypool
SaahilClaypool / SelfSignedAspNetCore.md
Last active November 9, 2020 16:59
Dotnet core self signed certificate on linux

install certificates for asp.net core

openssl genrsa -des3 -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem  -addext "subjectAltName = DNS:localhost"
openssl pkcs12 -export -out certificate.pfx -inkey rootCA.key -in rootCA.pem 
openssl pkcs12 -in certificate.pfx -clcerts -out pfx.pem
sudo cp pfx.pem /usr/local/share/ca-certificates/pfx.crt
sudo cp pfx.pem /usr/share/ca-certificates/pfx.crt
sudo bash -c "echo "pfx.crt" &gt;&gt; /etc/ca-certificates.conf"