Skip to content

Instantly share code, notes, and snippets.

View gsscoder's full-sized avatar
💭
obsessive coding disorder

coder (π³) gsscoder

💭
obsessive coding disorder
View GitHub Profile
@gsscoder
gsscoder / ImageUtil.cs
Last active April 19, 2024 11:59
C# image utilities
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
public static class ImageUtil
{
public static Image Resize(this Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
@gsscoder
gsscoder / process_darwin-2.go
Last active June 4, 2023 18:59
From github.com/gsscoder/goproc as in commit 998e482ecd
// Copyright 2015 Giacomo Stelluti Scala. All rights reserved. See doc/License.md in the project root for license information.
package process
/*
#include <stdlib.h>
#include "libproc.h"
*/
import "C"
import "unsafe"
@gsscoder
gsscoder / BubbleSort.sol
Created February 3, 2023 13:49
Bubble sort implementation in Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract BubbleSort {
function sort(uint[] memory array) public pure returns (uint[] memory) {
bool swapped;
for (uint i = 1; i < array.length; i++) {
swapped = false;
@gsscoder
gsscoder / factorial.ts
Last active October 23, 2022 06:48
Typescript script to calculate factorial of a number
// $ tsc factorial.ts && node factorial.js
// 3628800
function factorial(num: number) : number {
if (num == 0) return 1
else return num * factorial(num - 1)
}
console.log(factorial(10))
@gsscoder
gsscoder / EnumerableExtensions_Partition.cs
Created December 10, 2021 11:16
C# extension method to partition sequences by a value
using System;
using System.Collections.Generic;
using System.Linq;
using SharpX.Extensions;
static class EnumerableExtensions
{
public static IEnumerable<IEnumerable<T>> Partition<T, TKey>(this IEnumerable<T> source, Func<T, TKey> selector)
{
var result = new List<IEnumerable<T>>();
@gsscoder
gsscoder / ExportJsonARMTemplate.cs
Last active December 9, 2021 07:59
C# sample that demonstrates programmatic JSON AzureRM template export
// requires Microsoft.IdentityModel.Clients.ActiveDirectory 5.29
// Microsoft.Azure.Management.ResourceManager 3.15.1-preview
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
@gsscoder
gsscoder / Guard.cs
Last active November 27, 2021 13:07
C# arguments validation guard class
using System;
using System.Linq;
using System.Runtime.CompilerServices;
static class Guard
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AgainstNull(string argumentName, object value)
{
if (value == null) throw new ArgumentNullException(argumentName, $"{argumentName} cannot be null.");
@gsscoder
gsscoder / LowerCaseNamingStrategy.cs
Created July 9, 2020 09:02
C# Json.NET naming strategy to serialize enum values as lowercase strings
/*
public class YourClass
{
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter), converterParameters: typeof(LowerCaseNamingStrategy))]
public YourEnum YourProperty { get; set; }
}
*/
using Newtonsoft.Json.Serialization;
@gsscoder
gsscoder / ConsoleEx.cs
Last active April 23, 2021 08:21
C# console helper (colors and bold)
using System;
sealed class ConsoleEx
{
public void Write(ConsoleColor color, string format, params object[] arg)
{
var current = Console.ForegroundColor;
Console.ForegroundColor = color;
Console.Write(format, arg);
Console.ForegroundColor = current;
@gsscoder
gsscoder / Require-AzCliVersion.ps1
Last active April 23, 2021 08:20
PowerShell function to check Azure CLI version
function Require-AzCliVersion {
[OutputType([void])]
param(
[Parameter(Mandatory, ValueFromPipeline)] [ValidatePattern("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,9}$")] [string] $Version,
[switch] $Fail
)
$actual = ('az version' | Invoke-Expression | ConvertFrom-Json).'azure-cli'
if ($actual -eq $Version) {
"Azure CLI present in pinned version $Version" | Write-Verbose
} else {