Skip to content

Instantly share code, notes, and snippets.

@hyrmn
hyrmn / LineCounter.cs
Last active Sep 10, 2021
Surprisingly slower
View LineCounter.cs
using System.Numerics;
namespace nlc;
public class LineCounter
{
public const int BufferSize = 128 * 1024;
private const byte rune = (byte)'\n';
private static readonly Vector<byte> mask = new(rune);
@hyrmn
hyrmn / 00_OldReliable.cs
Last active Sep 10, 2021
Ways to swap numbers (.NET 6.0)
View 00_OldReliable.cs
using System.Diagnostics;
int x = 10;
int y = 20;
var tmp = x;
x = y;
y = tmp;
Debug.Assert(x == 20);
@hyrmn
hyrmn / why.cs
Created Sep 1, 2021
RSS + media thumbnail with XLinq and I don't know why
View why.cs
record Pan(string Style, decimal Price, string ProductUrl, string ThumbnailUrl)
{
public XElement ToRss(XNamespace mediaNs)
{
return new XElement("item",
new XElement("title", Style),
new XElement("description", $"Buy now for {Price}"),
new XElement("link", ProductUrl),
new XElement(mediaNs + "thumbnail", new XAttribute("url", ThumbnailUrl))
);
@hyrmn
hyrmn / program.cs
Created Mar 5, 2021
File manipulation with ImageSharp
View program.cs
import the following nuget package:
> Install-Package SixLabors.ImageSharp.Drawing -Version 1.0.0-beta11
@hyrmn
hyrmn / lambda.md
Created Jul 1, 2020
Method groups are slower!!!!!!!!1
View lambda.md
public void MapUserWithStaticMapper2()
{
    var users = new[]
    {
        new User(1, "Bob Almighty", "Green", DateTimeOffset.Now),
        new User(2, "Alice Wunder", "Orange", DateTimeOffset.Now)
    };

    var view = users.Select(u => UserSummary.MapFrom(u));
View the file when you run "notepad $PROFILE"
set-alias -name npp -value "${env:ProgramFiles}\Notepad++\notepad++.exe"
set-alias -name ed -value "code"
remove-item alias:curl -EA SilentlyContinue
$DefaultUser = 'hyrma'
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
@hyrmn
hyrmn / hello.zig
Created Jan 27, 2020
Reading a file and counting carriage returns in Zig 0.5.x+master
View hello.zig
const std = @import("std");
const os = std.os;
const fs = std.fs;
const File = std.fs.File;
pub fn main() !void {
const stdout = &std.io.getStdOut().outStream().stream;
//try stdout.print("Hello, {}!\n", .{"world"});
var file = try fs.openFileAbsolute("c:\\data\\bigsum.txt", File.OpenFlags{});
defer file.close();
@hyrmn
hyrmn / netcore linecount
Created Jan 21, 2020
netcore 3.1. hardcoded to the location of a 1.6gb text file
View netcore linecount
class Program
{
static void Main(string[] args)
{
var count = 0;
using var reader = File.OpenText(@"C:\code\go\src\github.com\hyrmn\lc\pkg\lc\testdata\bigsum.txt");
var buffer = new Span<char>(new char[1024]);
int readLength;
View files.cs
public class PartitionedNamingStrategy
{
private static readonly Dictionary<string, Func<string, string>> NamingStrategies = new Dictionary<string, Func<string, string>>
{
{"none", s => s},
{"bymachine", s => $"{Environment.MachineName}_{s}"}
};
private readonly string _strategy;
@hyrmn
hyrmn / main.cs
Created Oct 29, 2019
Two simple local functions
View main.cs
static decimal CalculateBalance(AccountId accountId)
{
//assume these things come in externally
var charges = new[] {new Charge(100M)};
var payments = new[] {new Payment(20M)};
var chargeBalance = Σ(charges.Select(c => c.Amount));
var paymentCredit = Σ(payments.Select(p => p.Amount));
return chargeBalance - paymentCredit;