Skip to content

Instantly share code, notes, and snippets.

View kmorcinek's full-sized avatar

Krzysztof Morcinek kmorcinek

View GitHub Profile
using System;
using System.ComponentModel;
using System.Diagnostics;
public static class OptionExtensions
{
public static Either<TSuccess, TFailure> ToEither<TSuccess, TFailure>(this Option<TSuccess> option, TFailure failureValue)
{
if (option.HasValue)
return new Either<TSuccess, TFailure>(option.Value);
using System;
using System.ComponentModel;
using System.Diagnostics;
public static class EitherExtensions
{
[DebuggerStepThrough]
public static Either<TNew, TFailure> IfSuccess<TCurrent, TNew, TFailure>(this Either<TCurrent, TFailure> either, Func<TCurrent, Either<TNew, TFailure>> continuation)
{
if (!either.Succeeded)
using System;
using System.Diagnostics.CodeAnalysis;
public struct Option<T> : IEquatable<Option<T>>
{
[SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
public static readonly Option<T> None = new Option<T>();
private readonly T _value;
private readonly bool _hasValue;
@kmorcinek
kmorcinek / .gitattributes
Last active May 5, 2017 17:58
Example .gitattributes file I use for C#/JS projects on Windows
# Auto detect text files and perform LF normalization: http://stackoverflow.com/questions/170961/whats-the-best-crlf-carriage-return-line-feed-handling-strategy-with-git/10855862#10855862
* text=auto
*.sql diff
*.ts diff
*.xml diff
using System;
using System.Collections.Generic;
using System.Linq;
public static class CollectionExtensions
{
public static void RemoveAll<T>(this ICollection<T> @this, Func<T, bool> predicate)
{
List<T> list = @this as List<T>;
using Newtonsoft.Json;
using Xunit;
[Fact]
public void SerializationTest()
{
var money = Money.Create(1, "USD");
string json = JsonConvert.SerializeObject(money);
Money result = JsonConvert.DeserializeObject<Money>(json);
@kmorcinek
kmorcinek / bash.bashrc
Created April 24, 2017 16:00
bash.bashrc for git
alias noyp="cd C:/SRC/NameOfYourProject/" # noyp is example abbreviation from NameOfYourProject
alias blef="cd C:/Work/Github/Blef/"
alias brlocal="git br | grep local"
@kmorcinek
kmorcinek / EnumGuard.cs
Created November 17, 2017 20:31
EnumGuard for better exception throwing when code have not handled enum values
using System;
using JetBrains.Annotations;
public class EnumGuard
{
[Pure]
public static ArgumentOutOfRangeException CreateMissingEnumException<T>(string paramName, T value)
where T : struct
{
return new ArgumentOutOfRangeException(
@kmorcinek
kmorcinek / ISynchronizeInvokeExtensions.cs
Created May 1, 2018 07:14
Solve a cross-threading Exception in WinForms
using System;
using System.ComponentModel;
public static class ISynchronizeInvokeExtensions
{
public static void InvokeOnUiThread<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke
{
if (@this.InvokeRequired)
{
@this.Invoke(action, new object[] { @this });
@kmorcinek
kmorcinek / .gitattributes
Last active April 20, 2019 08:57
Handles EOL and how to compare files based on extensions
# taken from https://github.com/aspnet/SignalR-samples/blob/master/.gitattributes
# Auto detect text files and perform LF normalization
* text=auto
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain