Skip to content

Instantly share code, notes, and snippets.

View dadhi's full-sized avatar
🎯
Focusing

Maksim Volkau dadhi

🎯
Focusing
View GitHub Profile
@dadhi
dadhi / FizzBuzz.LINQ.cs
Created June 25, 2013 07:01
FizzBuzz on C# as generic solution using LINQ.
[Test]
public void FizzBuzz()
{
var rules = new Dictionary<int, string> { { 3, "Fizz" }, { 5, "Buzz" } };
Func<int, string> translate = i =>
rules.Aggregate((string)null, (s, x) => i % x.Key == 0 ? (s == null ? x.Value : s + x.Value) : s)
?? i.ToString();
var words = Enumerable.Range(9, 7).Select(translate).ToArray();
CollectionAssert.AreEqual(new[] { "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" }, words);
@dadhi
dadhi / FizzBuzz..OptionFold.fs
Created June 25, 2013 07:04
FizzBuzz on F# as generic solution.
let rules = [(3, "Fizz"); (5, "Buzz")]
let translate x =
None |> List.foldBack (fun (i, word) say ->
if x % i = 0 then Some <| Option.fold (+) word say
else say) rules
for n in 1..100 do
printfn "%s" <| match translate n with | Some s -> s | _ -> string n
@dadhi
dadhi / InsertTabsOrSpaces-vc2010macro.vb
Last active October 26, 2017 09:19
Visual Studio 2010 macros to set "Insert spaces" or "Keep tabs" in Tools -> Options -> Text Editor -> C# -> Tabs page. Thanks to answer http://stackoverflow.com/a/1721896/2492669. For those who switching between solutions with spaces and tabs. As for me, I have assigned macros on ctrl+shift+alt+s and ctrl+shift+alt+t and quite happy now.
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
@dadhi
dadhi / RenameCSharpProject.ps1
Last active July 27, 2023 19:50
PowerShell script to rename C# Project.
<#
PowerShell script to rename C# Project step-by-step:
* Copying project folder to folder with new project name
* Renaming .csproj file and other files with project name
* Changing project name reference in .sln solution file
* Changing RootNamespace and AssemblyName in .csproj file
* Renaming project inside AssemblyInfo.cs
#>
param(
[parameter(
using System;
using System.Linq;
using System.Threading;
using NUnit.Framework;
namespace Playground
{
[TestFixture]
public class RefTests
{
@dadhi
dadhi / MultimethodsWithDynamicTests
Last active January 13, 2016 10:48
Proof of concept and very simple Multimethods implementation, that uses dynamic keyword from .Net 4.0. Thanks goes to <http://blogs.msdn.com/b/laurionb/archive/2009/08/13/multimethods-in-c-4-0-with-dynamic.aspx>.
using NUnit.Framework;
using Microsoft.CSharp.RuntimeBinder;
namespace MultiMethodsWithDymanic
{
[TestFixture]
public class MultimethodsTests
{
[Test]
public void What_is_good_for_rabbit_is_not_good_for_wolf()
@dadhi
dadhi / DiscriminatedUnion3.cs
Created May 28, 2014 10:32
Liked this implementation of F# Discriminated Union in C# - http://stackoverflow.com/a/3199453/2492669
using System;
namespace Juliet
{
class Program
{
static void Main(string[] args)
{
Union3<int, char, string>[] unions = new Union3<int,char,string>[]
{
@dadhi
dadhi / increment-count.bat
Created June 17, 2014 06:14
Batch script for persistent counter. Could be used for implementing state machine or suffixing backup files, etc. Uses "count file" to store counter between script executions.
@echo off
if [%1]==[] (echo error: Please specify countfile as command line argument. Could be count.txt or similar. && exit /B 1)
set COUNTFILE=%1
(set /P COUNT=<%COUNTFILE%)2>nul || set COUNT=0
set /A COUNT+=1
echo:%COUNT%>%COUNTFILE%
@dadhi
dadhi / gist:109b84ecb7dffe29f9bc
Last active May 14, 2021 13:43
Token-replacement template engine for PowerShell
<#
Token-replacement template engine for PowerShell courtesy to http://www.bricelam.net/2012/09/simple-template-engine-for-powershell.html
Usage:
Merge-Tokens 'Hello, $target$! My name is $self$.' @{ target = 'World'; self = 'Brice' }
#>
function Merge-Tokens($template, $tokens)
{
return [regex]::Replace($template, '\$(?<token>\w+)\$',
{ param($match) $tokens[$match.Groups['token'].Value] })
@dadhi
dadhi / Either.cs
Last active April 12, 2016 10:04
Take on minimal yet extensible approach to implementing Either/Or/DiscriminatedUnion
using System;
using System.Reflection;
using NUnit.Framework;
namespace Playground
{
[TestFixture]
public class EitherTests
{
[Test]