Skip to content

Instantly share code, notes, and snippets.

@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 / gist:7620852
Created November 23, 2013 22:31
Hopfield network implementation
// Learn more about F# at http://fsharp.net
// See the 'F# Tutorial' project for more help.
type Network = { mutable s : array<int>; w : double[,]; n : int }
with
static member create (n : int) (patterns : seq<array<int>>) =
{
n = n
s = Array.create n 0
w = Array2D.init n n (fun i j ->
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 / gausselimination.js
Created May 30, 2015 14:13
Example implementation of gauss elimination in javascript
function print(M, msg) {
console.log("======" + msg + "=========")
for(var k=0; k<M.length; ++k) {
console.log(M[k]);
}
console.log("==========================")
}
function diagonalize(M) {
var m = M.length;
@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 #-}