Skip to content

Instantly share code, notes, and snippets.

@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 }
@codecontemplator
codecontemplator / BinomialHeap.hs
Last active October 30, 2016 10:25
Implementation of Binomial heap from the book Purely functional data structures by Chris Okasaki
data BTree a = Node Int a [(BTree a)] deriving Show
data BHeap a = Heap [BTree a] deriving Show
empty :: BHeap a
empty = Heap []
link :: Ord a => BTree a -> BTree a -> BTree a
link t1@(Node r x1 c1) t2@(Node _ x2 c2) =
if x1 < x2 then
using System;
using System.Linq;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Collections.Generic;
namespace ConsoleApplication
{
public static class MoreLinq
{
import Data.List
solve xs t = nub $ map sort $ solve' xs [] t
where
solve' _ solution 0 = [solution]
solve' [] _ _ = []
solve' (x:xs) solution t = solve' xs (x:solution) (t-x) ++ solve' xs solution t
test = solve [1,3,3,3,9] 10
--[[3,3,3,1],[9,1]]
[string]$doc = @"
{
"parameters" : {
"valuename": {
"value": "123"
}
}
}
"@
@codecontemplator
codecontemplator / mapping-size-calc.ps1
Last active June 9, 2017 12:38
Count mapping size per application
$doc = Get-Content .\mapping.txt -raw
$json = $doc.ToLower() | ConvertFrom-Json
function calc-num-properties($o)
{
$count = 0
if ($o)
{
var JSONPath = require('JSONPath');
const doc = require('./mapping.json');
const mappings = JSONPath({json: doc, path: "$['serilog-2017.05'].mappings.*", resultType: "all" });
function countProps(obj) {
var result = 0;
if (obj) {
Object.getOwnPropertyNames(obj).forEach(function(p) {
result += 1 + countProps(obj[p].properties);
@codecontemplator
codecontemplator / maybe-monad.ps1
Last active October 10, 2017 18:55
Maybe monad in powershell
function mbind
{
param(
[parameter(valuefrompipeline=$true)]$Ma,
[parameter(position=0)][scriptblock]$fMb
)
process
{
if ($Ma) {
@codecontemplator
codecontemplator / Settings.ps1
Created November 25, 2017 20:49
A way of handling settings / configuration parameters in powershell (experimental)
function Merge($x, $y)
{
$y.GetEnumerator() | % { $x[$_.Name] = $_.Value }
return $x
}
function Get-Settings
{
[CmdletBinding()]