Skip to content

Instantly share code, notes, and snippets.

View jbrestan's full-sized avatar

Honza Brestan jbrestan

  • Czech Republic
  • 13:33 (UTC +01:00)
  • X @jbrestan
View GitHub Profile
@jbrestan
jbrestan / Y.cs
Created December 30, 2013 02:04
C# Y combinator implementation for Func<A, R>
public static Func<A, R> Y<A, R>(Func<Func<A, R>, Func<A, R>> f)
{
Func<A, R> g = null;
g = f(a => g(a));
return g;
}
@jbrestan
jbrestan / TypeSafetyWrapper.cs
Created December 30, 2013 02:12
Wrapper for primitive and sealed types to be used where more specific meaning of the primitive/sealed type is desired (and could otherwise be solved with a subtype)
public abstract class TypeSafetyWrapper<T>
{
private readonly T value;
public TypeSafetyWrapper(T value)
{
this.value = value;
}
public T Value { get { return value; } }
public static IGetOperationResult<T> ExecuteGetOrCreateWithLock<T>(this ICouchbaseClient client,
string key, Func<T> defaultNewValue, TimeSpan keyExpiration, TimeSpan lockExpiration)
{
var initialGetResult = client.ExecuteGetWithLock<T>(key, lockExpiration);
if (initialGetResult.StatusCode == (int) StatusCodeEnums.NotFound ||
(initialGetResult.InnerResult != null &&
initialGetResult.InnerResult.StatusCode == (int) StatusCodeEnums.NotFound))
{
// Key not found. We'll have to create it with the default value and make sure we get the lock.
@jbrestan
jbrestan / FieldOffsetAbuse.cs
Last active August 29, 2015 14:07
Breaking the C# type safety without "unsafe", with explicit struct layout.
void Main()
{
var u = new Union
{
Foo = null,
Bar = new Bar()
};
u.Foo.S = "ohai!";
@jbrestan
jbrestan / tryParse.fs
Last active August 29, 2015 14:08
Generic F# wrapper over BCL "static bool TryParse(string, out T)" pattern
let inline tryParse input =
let mutable result = Unchecked.defaultof< ^a>
if (^a: (static member TryParse : string -> ^a byref -> bool) (input, &result))
then Some(result) else None
// Pagination as a function
static IQueryable<T> Paginate<T>(this IQueryable<T> query, int pageNumber, int pageSize) =>
query.Skip(pageSize*pageNumber).Take(pageSize);
// Usage
var paginatedQuery = originalQuery.Paginate(pageNumber, pageSize);
// Composition (fluent with extension methods)
var paginatedFilteredQuery = originalQuery.Where(predicate).Paginate(pageNumber, pageSize);
@jbrestan
jbrestan / OperatorAbuse.cs
Created February 25, 2015 21:00
C# operator overloading abuse. IIRC inspired by Jon Skeet's operator abuse article
[TestClass]
public class OperatorAbuseTest
{
[TestMethod]
public void TestTheAbuse()
{
var repeat = Operators.Repeat<string>();
var join = Operators.Join<string>();
var result = "Hello" <repeat> 3 <join> ",";
var expected = "Hello,Hello,Hello";
@jbrestan
jbrestan / Ensure.cs
Created February 26, 2015 20:29
NotNull runtime guard
public static class Ensure
{
public static TValue NotNull<TValue>(Expression<Func<TValue>> parameterExpression)
{
var possibleNull = parameterExpression.Compile().Invoke();
if (possibleNull == null)
{
var paramName = GetVariableName(parameterExpression);
throw new ArgumentNullException(paramName);
@jbrestan
jbrestan / zip.fs
Created May 2, 2015 08:37
(Ineffective) zip using fold
let zip xs ys =
let step acc x =
match acc with
| (rs, y::ys) -> ((x,y)::rs, ys)
| _ -> acc
Seq.fold step ([], ys) xs
|> fst
|> List.rev

Keybase proof

I hereby claim:

  • I am jbrestan on github.
  • I am jbrestan (https://keybase.io/jbrestan) on keybase.
  • I have a public key whose fingerprint is 806C 03DD D1B2 E3F3 53E4 5C3F 6912 176F 7B2A 8734

To claim this, I am signing this object: