Skip to content

Instantly share code, notes, and snippets.

View jbrestan's full-sized avatar

Honza Brestan jbrestan

View GitHub Profile
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
@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:

@jbrestan
jbrestan / AutoLogger.cs
Last active August 29, 2015 14:26
Dynamic logger experiment. Allows writing descriptive log calls without the need to implement them by hand.
public class AutoLogger : DynamicObject
{
private readonly Action<string> _logMethod;
const string SplitterName = "splitNameBy";
public AutoLogger(Action<string> logMethod)
{
_logMethod = logMethod;
}
public IDictionary<string, TValue> Dict<TValue>(
params Func<dynamic, TValue>[] pairs)
{
return pairs.ToDictionary(pair => pair.Method.GetParameters()[0].Name,
pair => pair(null));
}
@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; } }