Skip to content

Instantly share code, notes, and snippets.

View opavlyshak's full-sized avatar

Oleksandr Pavlyshak opavlyshak

View GitHub Profile
@opavlyshak
opavlyshak / AllocateConsole.cs
Created May 18, 2012 18:30
Create and show console window in .NET
using System.Runtime.InteropServices;
namespace Application
{
public class ConsoleUtils
{
[DllImport("kernel32.dll", EntryPoint = "AllocConsole", CharSet = CharSet.Unicode)]
public static extern bool AllocConsole();
}
}
@opavlyshak
opavlyshak / create-file-of-length
Created November 6, 2012 12:42
Generate file of arbitrary size in PowerShell
$f = New-Object System.IO.FileStream d:\temp\50MB-file.dat, Create, ReadWrite
$f.SetLength(50MB)
$f.Close()
@opavlyshak
opavlyshak / stringPermutations.fs
Created December 18, 2012 09:58
Find string permutations in F#
let stringPermutations (s : string) =
let rec permute result rest =
match rest with
| "" -> [result]
| _ ->
rest
|> Seq.mapi (fun i -> fun c ->
permute (result + c.ToString()) (rest.Remove(i, 1)))
|> Seq.concat
|> Seq.toList
using System;
// Example from acticle by Eric Lippert http://blogs.msdn.com/b/ericlippert/archive/2011/03/17/implementing-the-virtual-method-pattern-in-c-part-one.aspx
namespace VirtualMethodsImplementation
{
internal sealed class VTable
{
public readonly Func<Animal, string> Complain;
public readonly Func<Animal, string> MakeNoise;
@opavlyshak
opavlyshak / CompanyWorkers.cpp
Last active December 12, 2015 00:18
Sample of C++ code convention
/*
Code convention summary:
- don't use underscores '_' (except in constants)
- lines should not be longer than 100 symbols
- indent using EITHER ALWAYS 4 spaces OR ALWAYS Tabs. Don't mix tabs and spaces.
*/
#include <string>
// Declaration and implementation should be split into "CompanyWorkers.h" and "CompanyWorkers.cpp" files.
@opavlyshak
opavlyshak / gist:4951436
Created February 14, 2013 08:57
Read everything from stdin in C++
std::string text = "";
std::string line;
while (std::getline(std::cin, line))
{
text = text + line + "\r\n";
}
@opavlyshak
opavlyshak / FibonacciWithTemplates.cpp
Created April 26, 2013 14:04
Using C++ templates to compute Fibonacci numbers at compile time
#include <iostream>
template <int n> struct Fib
{
enum { val = Fib<n - 1>::val + Fib<n - 2>::val };
};
template<> struct Fib<0> { enum { val = 0 }; };
template<> struct Fib<1> { enum { val = 1 }; };
@opavlyshak
opavlyshak / BarButtonItemBinder.cs
Created June 20, 2013 19:15
Example of data binding for DevExpress WinForms BarButtonItem. BarButtonItem doesn't support data binding, for such properties as Enabled, for instance. Binder class allows to add data binding for Enabled property.
using System;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraBars;
namespace BarButtonItemDataBindingExample
{
internal sealed class BarButtonItemBinder : IBindableComponent
{
private readonly BarButtonItem barButtonItem;