Skip to content

Instantly share code, notes, and snippets.

View habib-sadullaev's full-sized avatar

habib sadullaev habib-sadullaev

  • Uzbekistan
View GitHub Profile
@habib-sadullaev
habib-sadullaev / Dynamic dispatch LINQ
Last active December 12, 2023 10:17
Dynamic dispatching LINQ for non-generic `IQueryable` and `LambdaExpression`
#r "nuget: FSharp.Interop.Dynamic"
open System
open System.Collections
open System.Linq.Expressions
open System.Linq
open FSharp.Interop.Dynamic
// query.Select(selector)
let select (selector: LambdaExpression) (query: IQueryable) : IQueryable =
@habib-sadullaev
habib-sadullaev / Revoke temp session token for AWS
Last active March 4, 2024 11:13
temporary credentials for an Amazon Web Services account or IAM user. The credentials consist of an access key ID, a secret access key, and a security token
#r "nuget:AWSSDK.SecurityToken"
open Amazon
open Amazon.Runtime.CredentialManagement
open Amazon.SecurityToken
open Amazon.SecurityToken.Model
let awsCredentials profile =
let chain = CredentialProfileStoreChain()
match chain.TryGetAWSCredentials profile with
using System;
using System.Text.Json;
var value = new { name = default(string)! };
//method #1
value = (dynamic)JsonSerializer.Deserialize("""{ "name": "xyz" }""", value.GetType())!;
Console.WriteLine(value);
//method #1
using System;
using System.Linq;
using System.Linq.Expressions;
static class _
{
static void Deconstruct(this BinaryExpression x,
out ExpressionType type,
out Expression left,
out Expression right)
type IRef =
abstract member Ref: unit -> byref<int>
type C() =
let mutable num = 10
member _.Get() = num
member _.Ref() = &num
interface IRef with
member this.Ref() = &this.Ref()
open System.Numerics
module NumericLiteralG =
let inline FromZero () : #INumber<'a> = 'a.Zero
let inline FromOne () : #INumber<'a> = 'a.One
let inline FromInt32 (number: int) : #INumber<'a> = 'a.CreateChecked(number)
let (*) x y : #IMultiplyOperators<'a, 'a, 'a> = 'a.op_Multiply(x, y)
let (/) x y : #IDivisionOperators<'a, 'a, 'a> = 'a.op_Division(x, y)
### Nullable Reference Types
https://devblogs.microsoft.com/dotnet/nullable-reference-types-in-csharp/
https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types#non-nullable-properties-and-initialization
The purpose of nullable warnings is to minimize the chance that your application throws a `System.NullReferenceException` when run.
To enable Nullable Reference Types for all code in a project, you can add the following to its `.csproj` file:
using System.Diagnostics.CodeAnalysis;
using System;
static Result<Foo, Err> GetResult(bool isErr)
=> isErr ? Result.Ok(new Foo()) : Result.Error(new Err());
Console.WriteLine(GetResult(true) switch
{
{ Success: true, Value: var value } => value,
{ Success: false, Error: var error } => error,
namespace Data.Model.Entities
{
using System;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Reflection;
public class CommentSplittingFixer : IStoreModelConvention<EdmModel>
type Tuple = Tuple with
static member inline TupleMap (Tuple, (a, b)) = fun f -> (f a, f b)
static member inline TupleMap (Tuple, (a, b, c)) = fun f -> (f a, f b, f c)
static member inline TupleMap (Tuple, (a, b, c, d)) = fun f -> (f a, f b, f c, f d)
let inline mapTuple f x =
let inline map (a: ^a) (b : ^b) = ((^a or ^b) : (static member TupleMap : ^a * ^b -> ^t) (a, b))
map Tuple x f
mapTuple (fun x -> x * x + 1) (1, 2)