Skip to content

Instantly share code, notes, and snippets.

View savaged's full-sized avatar
418

David Savage savaged

418
View GitHub Profile
@savaged
savaged / CircPlotter.fs
Created November 1, 2022 10:29
Fun plotting a circle in the console
open System
let R = 18
let H = R * 2
let setDisplayPosition (x, y) =
try
Console.SetCursorPosition(x, y)
with
@savaged
savaged / CircPlotter.cs
Last active November 1, 2022 10:13
Fun plotting a circle in the console in a FP style
CircPlotter.PlotCirc();
static class CircPlotter
{
const int R = 18;
static int H => R * 2;
static void SetDisplayPosition(int x, int y)
{
@savaged
savaged / Flatten.cs
Created October 24, 2022 10:30
C# SelectMany example
Lease[] leases =
{ new Lease { Leasee = "David",
Assets = new List<string>{ "Workstation18", "Laptop5" } },
new Lease { Leasee = "Pawel",
Assets = new List<string>{ "Workstation30" } },
new Lease { Leasee = "Rhys",
Assets = new List<string>{ "Workstation24", "Headset3" } },
new Lease { Leasee = "Karen",
Assets = new List<string>{ "Laptop31", "MS-Keyboard", "Headset2" } } };
@savaged
savaged / Rot13.cs
Last active October 21, 2022 08:15
Caesar Cipher in C# (with Linq)
if (args?.Count() > 0)
Console.WriteLine(args[0].Select(c =>
c switch
{
>= 'a' and <= 'm' or >= 'A' and <= 'M' => (char)(c + 13),
>= 'n' and <= 'z' or >= 'N' and <= 'Z' => (char)(c - 13),
_ => c
}
).ToArray());
@savaged
savaged / RecursiveLevenshteinDistance.cs
Last active October 5, 2022 07:13
Recursive version of Levenshtein distance algorithm
namespace savaged.RecursionFun
{
/// <summary>
/// See https://programm.top/en/c-sharp/algorithm/levenshtein-distance/
/// </summary>
public static class RecursiveLevenshteinDistance
{
public static int Distance(string value1, string value2) =>
Distance(value1, value1?.Length ?? 0, value2, value2?.Length ?? 0);
@savaged
savaged / fn_isnumeric.sql
Created August 17, 2022 10:46
MySql custom function for checking if a string is intended to represent a number
DROP FUNCTION IF EXISTS `fn_isnumeric`;
DELIMITER //
CREATE FUNCTION `fn_isnumeric`(
s VARCHAR(100))
RETURNS TINYINT
NO SQL
BEGIN
@savaged
savaged / savaged.cli.ConsoleWithDI.App.cs
Last active June 15, 2022 11:11
Console App with dependency injection
using Autofac;
using savaged.cli.io;
using savaged.cli.modules;
namespace savaged.cli
{
/// <Summary>
/// Usage in Program.cs
/// App.Current.MainModule.Run();
/// </Summary>
@savaged
savaged / coding-principles-map.wiki
Created May 5, 2022 09:32
Some coding principles / things to remember

```

                                   +----------------+
    +--------------------+         | B ehaviour     |
    | ctor for structure |         | D riven        |  
    | Load for data      |         | D evelopment   |
    +--------------------+         |         G iven |
                                   |         W hen  |
      +---------------------+      |         T hen  |
      | prefer aggregation¹ |      +----------------+
      |  to inheritance²    |     

@savaged
savaged / EmptyModelObject.cs
Created April 20, 2022 07:30
Empty Model object to avoid null reference issues
public class EmptyThingModel : BaseModel
{
private static readonly ThingModel _default = new EmptyThingModel();
static EmptyThingModel() { }
private EmptyThingModel() { }
public static ThingModel Default => _default;
@savaged
savaged / WrappedConsoleCommand.cs
Created March 25, 2022 13:34
C# wrapped terminal/console command
using System;
using System.Diagnostics;
using System.Text;
namespace Savaged.ConsoleWrapping
{
class Program
{
static void Main(string[] args)
{