Skip to content

Instantly share code, notes, and snippets.

View mgravell's full-sized avatar
🏠
Working from home

Marc Gravell mgravell

🏠
Working from home
View GitHub Profile
readonly struct Foo
{
private readonly int[] _arr;
public Foo(int length) => _arr = new int[length];
public int Length => _arr?.Length ?? 0;
public ref int this[int index] => ref _arr[index];
public FooEnumerator GetEnumerator() => new FooEnumerator(_arr);
public struct FooEnumerator
using System;
class Foo
{
int _value;
public ref int Value => ref _value;
static void Main()
{
var obj = new Foo { Value = 12 };
Console.WriteLine(obj.Value);
string s = string.Join(",", Enumerable.Repeat("blah", 50));
var charSpan = s.AsSpan();
var vectorized = MemoryMarshal.Cast<char, Vector<ushort>>(charSpan);
Console.WriteLine($"vector chunks: " + vectorized.Length);
foreach(var v in vectorized)
{
if (Vector.EqualsAny(v, spaces)) { ...}
}
// todo: mop up any remainded
using StackExchange.Redis;
using System;
static class P
{
static void Main()
{
// this is a simpler runtime check if you don't have redis handy
// Pipelines.Sockets.Unofficial.SocketConnection.AssertDependencies();
try
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Buffers;
using System.Buffers.Text;
using System.Runtime.CompilerServices;
using System.Text;
public static class P
{
public async Task<Token> WaitOneAsync()
{
/* There are two types of semaphore examples for redis; the simple ones don't
* properly account for differences in the system clocks of different clients;
* the more advances ones *do*, but are much more complex. But if we're using
* Lua, we can get the *server* to make the time decisions, then the clock of
* the client is *irrelevant*, and we can use the simple versions. Yay!
*
* Now; normally in redis Lua/EVAL you are not allowed to access the system time,
* as that will behave differently during replication; so: we use the
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
class Foo
{
[SomeAttrib]
public EntityLocalizer A { get; set; }
# option 1:
**stop using Y/N** - there is a "bit" type that is intended for storing booleans
# option 2:
cast in the database during the select
select cast(case SomeColumn when 'Y' then 1 when 'N' then 0 else null end as bit) as [SomeColumn]
using System;
using System.Reflection;
using System.Linq;
using System.Data;
static class P
{
static void Main()
{
ShowClassesWithEquality(typeof(string).Assembly);
using System;
class EpicAttribute : Attribute
{
public string Name { get; set; }
}
[Epic(Name = "awesome!")]
static class P
{
static void Main() {