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 / c-sar-cipher.c
Last active March 13, 2023 08:11
Some fun with C in a functional programming style
// c-sar-cipher
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//#include "c-sar-cipher.h"
void run(char* arg, void (*cout)(char*));
void print_string(char* str);
char* apply_cipher(char* input);
@savaged
savaged / SeeFunPtr.c
Last active June 14, 2023 10:26
Fun with function pointers in C
// Program.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int (*op_on_triple_ptr)(int, int, int);
typedef int (*op_on_pair_ptr)(int, int);
typedef int (*op_on_single_ptr)(int);
void cout_for_op(char* op_name, int op_result);
@savaged
savaged / echo-command-line.sh
Last active March 7, 2023 10:00
Bash fun echoing back the command entered but with the full path
#!/usr/bin/sh
echo $(pwd)$(echo $0 | sed -r 's/^\.(\/[a-zA-Z]+[\.[a-z]*]?)$/\1/g') $1 $2 $3 $4 $5 $6 $7 $8 $9
@savaged
savaged / SavagedCipherCLI.cs
Last active July 17, 2023 07:38
Some fun with ciphers in C#
using System.Text;
App.Run(args, Console.WriteLine);
interface ICipherService
{
string Apply(string input, bool encrypt = false);
}
class XOrCipherService : ICipherService
@savaged
savaged / Rot13.CLI.cs
Created February 24, 2023 08:46
Fun C# Caesar Cipher command line tool in a functional programming style
using CaesarCipher;
if (args?.Length > 0)
Console.WriteLine(args[0].Rot13());
else
{
Console.WriteLine("Start typing (Return key to exit)");
PresentUntilReturnKey();
}
@savaged
savaged / FPish.cs
Last active February 22, 2023 08:27
FP styled C# example inspired by Isaac Abraham's book Programming With F#
using Unit = System.ValueTuple;
_ = App.Run(Console.WriteLine);
static class App
{
public static Unit Run(Action<string> outputter)
{
outputter(GetAwayWins().ToSummary());
return Unit.Create();
@savaged
savaged / ReverseEach.cs
Last active February 4, 2023 08:45
Demonstrating that the equivalent to the Haskell map function in C# is the Select in Linq
/*
* Trying to emulate the following Haskell, with the aim of demonstrating
* that the map function in C# is the Select in Linq...
*
* main = do
* print (reverseEach ["map", "select"])
*
* reverseEach l = map reverse l
*
*/
@savaged
savaged / Maybe.cs
Last active January 4, 2023 15:44
Monad pattern example for explicit null handling
namespace MonadFun;
//
// Inspired by Mikhail Shilkov's blog https://mikhail.io/2016/01/monads-explained-in-csharp/
//
public class Maybe<T> : IEquatable<Maybe<T>>
where T : class?
{
private readonly T? _value;
private Maybe() {}
@savaged
savaged / FilterDictionary.cs
Created December 19, 2022 14:52
Emulating SQL IN statement using Linq
var needles = new List<string> { "b", "d" };
var haystack = new Dictionary<string, object>
{
{ "a", 1 },
{ "b", 2 },
{ "c", false },
{ "d", "test" }
};
@savaged
savaged / FunWithRangeDuckTyping.cs
Created November 15, 2022 16:47
Example of duck typing with Ranges from Nick Chapsas
// Full credit to Nick Chapsas - See: https://youtu.be/jmmz1cInNow
foreach (var i in 0..5)
{
Console.WriteLine(i);
}
public static class Extensions
{
public static CustomIntEnumerator GetEnumerator(this Range range) => new CustomIntEnumerator(range);