Skip to content

Instantly share code, notes, and snippets.

open System.IO
open System.Threading
open System.Net
open System.Net.Sockets
let mirror (clientStream:Stream) (serverStream:Stream) = async {
while true do
let! onebyte = clientStream.AsyncRead(1)
do! serverStream.AsyncWrite(onebyte)
}
@dsimunic
dsimunic / Producer-consumer class
Last active January 1, 2016 13:18
Producer-consumer class from C#5.0 in a Nutshell, Chap 23 p.954
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace PcQueue
{
/// <summary>
/// Producer-consumer class from C#5.0 in a Nutshell, Chap 23 p.954
/// </summary>
static class PathExtensions
{
public static string EnsureFolderExists(this string path)
{
if (string.IsNullOrEmpty(path))
return path;
string fullPath = Path.GetFullPath(path);
if (!Directory.Exists(fullPath))
public static void OverwriteConsoleMessage(string message)
{
Console.CursorLeft = 0;
int maxCharacterWidth = Console.WindowWidth - 1;
if (message.Length > maxCharacterWidth) {
message = message.Substring(0, maxCharacterWidth - 3) + "...";
}
message = message + new string(' ', maxCharacterWidth - message.Length);
Console.Write(message);
}
@dsimunic
dsimunic / caller_test.lua
Last active August 29, 2015 13:56
Test if a lua file is runnnig standalone or is it required
-- Obviously, if a standalone file is called with an argument that corresponds to a loaded file, it will think it's required.
-- For example:
-- lua caller_test.lua _G
-- will choose the else branch.
-- For our purposes, though, this is good enough.
if not _G.package.loaded[...] then
print("Running standalone")
else
print("Required")
end
apt-get install -y wget build-essential gettext autoconf automake libtool
MONO_VERSION=3.4.0
wget http://download.mono-project.com/sources/mono/mono-${MONO_VERSION}.tar.bz2
bunzip2 -df mono-${MONO_VERSION}.tar.bz2
tar -xf mono-${MONO_VERSION}.tar
cd mono-${MONO_VERSION}
@dsimunic
dsimunic / parse_numerals.fs
Created October 2, 2014 20:17
Parse roman numerals
type RomanDigit = Nil | I | V | X | L | C | D | M
let romanDigitToInt aRomanDigit =
match aRomanDigit with
| Nil -> 0
| I -> 1
| V -> 5
| X -> 10
| L -> 50
| C -> 100
var animator = ( function animatorScope(){
var animating = false;
return function animator( input, incoming, outgoing, alwaysAnimate ){
var module = {
controller : input.controller || function(){},
view : function animatedView( ctrl ){
var view = ( input.view || input )( ctrl );
var cfg = view.attrs.config;
@dsimunic
dsimunic / docker-preserve-ownership.md
Created January 27, 2015 17:59
Running as a non-root inside a container

Running as a non-root inside a container

Docker can start a container with a specific user by passing the id on the command line with -u. The parameter to the -u switch is either a username or id of a user existing inside the container. More precisely, the container must have valid /etc/passwd file with the defined user:

$ mkdir etc 
$ echo 'postgres:x:1000:1000::/home/postgres:/bin/sh' > etc/passwd

While not needed for this simple experiment, we might want to create group and shadow files for completeness:

'use strict';
var m = require('mithril');
var extend = require('lodash').extend;
var setFocus = require('../util/viewhelper').setFocus;
var keymage = require('keymage');
function noop(){}
function addClass(el, className) {