Skip to content

Instantly share code, notes, and snippets.

Avatar
🦎

Amin Khansari akhansari

🦎
View GitHub Profile
@akhansari
akhansari / event-sourced-user.fsx
Last active Sep 23, 2022
F# : Event Sourcing in a nutshell
View event-sourced-user.fsx
// ========= Event Sourcing in a nutshell
(*
FriendlyName: string
Aggregate friendly name.
Initial: 'State
Initial (empty) state we will start with.
Decide: 'Command -> 'State -> 'Event list
@akhansari
akhansari / onion-1.fs
Last active Sep 22, 2022
F# : Onion architecture in a nutshell
View onion-1.fs
// 1. pure, don't think about IO at all
module Domain =
let add x y = x + y
// 2. think about IO but not its implementation
module App =
let add (getX: unit -> Async<int32>) y =
async {
let! x = getX ()
return Domain.add x y
@akhansari
akhansari / EsBankAccount.cs
Last active Jul 27, 2022
C# prototype of Decider pattern. F# version: https://github.com/akhansari/EsBankAccount
View EsBankAccount.cs
namespace EsBankAccount.Account;
using Events = IReadOnlyCollection<IEvent>;
public record Transaction(decimal Amount, DateTime Date);
// events
public interface IEvent { } // used to mimic a discriminated union
public record Deposited(Transaction Transaction) : IEvent;
public record Withdrawn(Transaction Transaction) : IEvent;
View flurl.fsx
#r "nuget: Flurl.Http"
module HttpApi =
open System
open System.Net
open System.Text.Json
open Flurl.Http
type Serializer (opt) =
interface Configuration.ISerializer with
View BowlingKata.fsx
type Pins = int
let [<Literal>] MaxPins : Pins = 10
type Frame =
| Strike of Pins
| Spare of Pins * Pins
| Open of Pins * Pins
| Pending of Pins
| Final of Pins * Pins option * Pins option
@akhansari
akhansari / .gitconfig
Last active Jun 7, 2022
Multiple Git Configs
View .gitconfig
[core]
excludesFile = ~/.gitignore
[user]
name = My Name
[includeIf "gitdir:~/git/"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/github/"]
path = ~/.gitconfig-home
@akhansari
akhansari / tasks.json
Created Feb 28, 2018
VSCode task for Hugo with error detection.
View tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Hugo",
"type": "shell",
"command": "hugo serve",
"group": "build",
"isBackground": true,
"problemMatcher": {
@akhansari
akhansari / AsyncSeq.fs
Last active Mar 3, 2022
F#: IEnumerable to IAsyncEnumerable
View AsyncSeq.fs
module AsyncSeq =
open System.Collections.Generic
open System.Threading.Tasks
let cancelled (cancellationToken: CancellationToken) =
Task.FromCanceled<bool> cancellationToken
|> ValueTask<bool>
let ofSeq (sq: Task<'T> seq) = {
new IAsyncEnumerable<'T> with
@akhansari
akhansari / XmlTools.iss
Last active Feb 24, 2022
Xml tools for InnoSetup
View XmlTools.iss
[Code]
function LoadValueFromXML(const AFileName, APath: string): string;
var
XMLNode: Variant;
XMLDocument: Variant;
begin
Log('Get Xml text node: ' + AFileName);
Result := '';
@akhansari
akhansari / Program1-Minimal.fs
Last active Feb 11, 2022
F# : Different ways to create an API without a dependency
View Program1-Minimal.fs
open System
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Routing
type Item = { Id: int32; Name: string; Price: decimal }
let rnd = Random()
let getProducts () =
[ for _ in 1..3 do { Id = rnd.Next 100; Name = "Blueberry"; Price = 10m } ]