Skip to content

Instantly share code, notes, and snippets.

function Prompt
{
$pwdshort = $pwd
$maxLen = 50
if ($pwd.Path.Length -gt $maxLen)
{
$pwdshort = $pwd.Path.Substring($pwd.Path.Length-$maxLen+5)
$i = $pwdshort.IndexOf('\')
if ($i -ge 0)
{
@codecontemplator
codecontemplator / Huffman.fs
Created December 5, 2014 22:44
Sample of Huffman encoding of strings
module Huffman
// ref: http://cs.hubfs.net/topic/None/56608
open BinomialHeap
type private Tree = Leaf of char | Branch of Tree*Tree
let private initialForest (stringData : string) =
// ref: http://www.fssnip.net/n5
let createHistogram =
@codecontemplator
codecontemplator / hron-in-nodejs.js
Created March 15, 2015 00:32
HRON parsing and serialization in node.js
var hron = require('hron.js');
var settings = {
environment: "test",
dbconnection: [
"Data Source=localhost; Initial Catalog=MyDB; Integrated security=true",
{ server: "192.168.1.20", timeout: 19 }
],
appserver: "192.168.1.21"
};
import Data.Char(isDigit)
data Parser a = Parser (String -> Maybe (String,a))
instance Monad Parser where
return x = Parser (\s -> Just (s,x))
(Parser p) >>= f =
Parser $ \s ->
case p s of
Nothing -> Nothing
@codecontemplator
codecontemplator / synchronize-clocks
Last active August 29, 2015 14:21
Synchronize client time with server time
$cred = Get-Credential
$serverip = "192.168.1.1"
$serverTime = Invoke-Command -ComputerName $serverip -Credential $cred -ScriptBlock { Get-Date }
$localTime = Get-Date
Write-host "Server time: $serverTime"
Write-host "Local time: $localTime"
$timeDiff = $serverTime - $localTime
$timeDiffSecs = [Math]::abs($timeDiff.TotalSeconds)
@codecontemplator
codecontemplator / hronparser.ps1
Created January 25, 2013 23:10
quick and dirty hron parser hack. see https://github.com/mrange/hron for information on hron
function Parse-String([ref]$lines, $indent)
{
$sb = New-Object System.Text.StringBuilder
while($lines.Value[0] -match "(?<indent>^\t*)(?<value>.*)" -and $matches.indent.Length -eq $indent)
{
$lines.Value = $lines.Value[1..($lines.Value.Length)]
$sb.AppendLine($matches.value) | Out-Null
}
return $sb.ToString()
}
@codecontemplator
codecontemplator / psdbutils.ps1
Created October 17, 2013 19:01
powershell database connection utilities
<#
.DESCRIPTION
Create a database connection and add extension methods (script methods) that can load and
save a System.Data.Dataset
.PARAMETER ConnectionString
Connection string to database.
.EXAMPLE
Open connection, load data, update and create new records.
@codecontemplator
codecontemplator / pretty-comment-tool.hs
Created January 16, 2016 18:05
Code golf at stackexchange. See link at to of file
-- http://codegolf.stackexchange.com/questions/69442/make-a-simple-pretty-comment-tool
-- package: https://hackage.haskell.org/package/regex-applicative-0.2.1/docs/Text-Regex-Applicative.html
-- example: https://github.com/feuerbach/regex-applicative/wiki/Examples
-- cabal install regex-applicative
-- ghci overloadedStrings -> :set -XOverloadedStrings
--{-# LANGUAGE OverloadedStrings #-}
@codecontemplator
codecontemplator / integer_factorization.coffee
Last active April 2, 2016 23:59
Integer factorization
# run: coffee --nodejs --harmony-generators .\main.coffee <number>
# ref: https://en.wikipedia.org/wiki/Integer_factorization
primes = (max) ->
c = (true for [0..max])
i = 1
while i < max
# skip to next prime
i = i + 1
while not c[i]
[<StructuredFormatDisplay("C v:{v} c:{c} r:{r}")>]
type Cell = { v: int Set; c : int; r : int; b : int } with
static member create i v =
let (c,r) = (i%9,i/9)
let b = r/3*3 + c/3
match v with
| 0 -> { v=Set.ofSeq [1..9]; c=c; r=r; b=b }
| v -> { v=Set.singleton v; c=c; r=r; b=b }