Skip to content

Instantly share code, notes, and snippets.

View chriskuech's full-sized avatar

Chris Kuech chriskuech

  • Datum Source, ex MSFT
View GitHub Profile
# names of the dimensions in the order that we take the cross product
$crossProductOrder = "Category1", "Category2", "Category3"
# get the object's identity vector, as member of the cross product
function Get-Vector([psobject] $obj) {
$crossProductOrder | % {$obj.$_}
}
# get the path to the object in the file system
function Get-VectorPath([psobject] $obj) {
# create the string identifier to describe the value within a dimension
# use `_` to describe when a value is not a member of a set
function Get-VectorComponent([psobject] $obj, [string] $dim) {
$v = $obj.$dim
@{$true = $v; $false = "_"}[$v -as [bool]]
}
# update Get-Vector to use Get-VectorComponent
function Get-Vector([psobject] $obj) {
# side effects
function square {
$script:squared = $script:x * $script:x
}
# pure
function square($x) {
$x * $x
}
# imperative
$myVar = $null
if ($x -gt 10) {
$myVar = $true
} else {
$myVar = $false
}
# declarative
$myVar = if ($x -gt 10) {$true} else {$false}
# iterative
function traverse($tree) {
$stack = [System.Collections.Stack]::new()
$stack.Push($tree)
while ($stack.Count) {
$node = $stack.Pop()
if ($node.Left) {
$stack.Push($node.Left)
}
if ($node.Right) {
# immutable read function (read the property directly)
function read($hash, $k) {
$hash[$k]
}
# immutable write function (copy on write)
function write($hash, $k, $v) {
$newHash = $hash.Clone()
$newHash[$k] = $v
$newHash
$add = {
Param($a, $b)
$a + $b
}
function add($a, $b) {
$a + $b
}
$add = $function:add
$add = [scriptblock](Get-Content add.ps1 -Raw)
$add > add.ps1
$n = 14
function addToN($a) {
$n = $a + $n
}
&$function:addToN 3
$n # `14`
.$function:addToN 3
$n # `17`