Skip to content

Instantly share code, notes, and snippets.

View ruxo's full-sized avatar

Ruxo ruxo

  • Bangkok
View GitHub Profile
@ruxo
ruxo / Program.cs
Last active April 8, 2024 13:23
Show how to use LanguageExt's Eff(ect) with memo to cache value.Note that `LanguageExt.Core` library is needed for running.
using System;
using LanguageExt;
using static LanguageExt.Prelude;
public static class Program {
public static void Main (string[] args) {
var program =
from v1 in Load
from _1 in eff(() => Console.WriteLine($"1st value = {v1}"))
from v2 in Load
@ruxo
ruxo / pinger.fsx
Last active February 24, 2024 12:15
Simple example of Akka.Net remoting. To run the example, start Ponger first. ``` dotnet fsi ponger.fsx ``` Then start Pinger in another terminal. ``` dotnet fsi pinger.fsx ```
#r "nuget:Akka.Remote"
open System
open Akka.Actor
open Akka.Configuration
type Pinger() =
inherit UntypedActor()
let Context = ActorBase.Context
@ruxo
ruxo / ConcurrentMailBox.cs
Last active September 2, 2023 23:10
Simple mailbox processor (Single-thread apartment)
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
namespace RZ.Foundation;
/// <summary>
/// A mailbox processor that allows some concurrent dispatching.
/// </summary>
/// <param name="logger">Microsoft Logger</param>
/// <param name="concurrentLimit">Maximum number of concurrent threads allowed to process the mailbox</param>
@ruxo
ruxo / Serializer.cs
Last active August 29, 2023 00:13
LanguageExt's Seq<T> serializer for Mongo DB
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.Serializers;
using LanguageExt;
public class SeqSerializationProvider : IBsonSerializationProvider
{
public IBsonSerializer? GetSerializer(Type type)
{
#nowarn "760"
#r "nuget:Confluent.Kafka"
open System
open System.Diagnostics
open System.Threading
open Confluent.Kafka
[<Literal>]
let Topic = "test-topic"
@ruxo
ruxo / Program.fs
Last active May 2, 2023 23:10
Demonstrate ad-hoc polymorphism in F#. It uses inline function with Statically Resolved Type Parameters feature. Try it in [repl.it](https://replit.com/@ruxozheng/F-Ad-hoc-polymorphism)
// From: https://angrydexterous.github.io/typeclassish.html
type Num<'A> =
abstract member Add: 'A -> 'A -> 'A
abstract member Subtract: 'A -> 'A -> 'A
[<Struct; NoComparison; NoEquality>]
type TInt =
interface Num<int> with
member my.Add x y = x + y
@ruxo
ruxo / AdaptiveExtension.fs
Last active April 28, 2023 23:20
(Broken) AsyncVal for UI async execution for FSharp.Data.Adaptive
[<AutoOpen>]
module Tirax.KMS.AdaptiveExtension
open System
open System.Runtime.CompilerServices
open FSharp.Data.Adaptive
[<Struct; IsReadOnly>]
type AsyncResult<'T> =
| Loading
function New-IsoFile
{
<# .Synopsis Creates a new .iso file .Description The New-IsoFile cmdlet creates a new .iso file containing content from chosen folders .Example New-IsoFile "c:\tools","c:Downloads\utils" This command creates a .iso file in $env:temp folder (default location) that contains c:\tools and c:\downloads\utils folders. The folders themselves are included at the root of the .iso image. .Example New-IsoFile -FromClipboard -Verbose Before running this command, select and copy (Ctrl-C) files/folders in Explorer first. .Example dir c:\WinPE | New-IsoFile -Path c:\temp\WinPE.iso -BootFile "${env:ProgramFiles(x86)}\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\efisys.bin" -Media DVDPLUSR -Title "WinPE" This command creates a bootable .iso file containing the content from c:\WinPE folder, but the folder itself isn't included. Boot file etfsboot.com can be found in Windows ADK. Refer to IMAPI_MEDIA_PHYSICAL_TYPE enumeration for possible media types: http://msdn.mi
[package]
name = "io_monad_guessing_game"
version = "0.1.0"
authors = ["Ruxo Zheng <me@ruxoz.net>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1"
@ruxo
ruxo / IO.cs
Last active February 5, 2023 08:27
Simple number guessing game with Aff/Eff..
using LanguageExt;
using LanguageExt.Common;
using NetConsole = System.Console;
// ReSharper disable InconsistentNaming
namespace TestConsole;
public delegate Either<Error, A> IO<in Env, A>(Env env);
interface ConsoleIO