This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# side effects | |
function square { | |
$script:squared = $script:x * $script:x | |
} | |
# pure | |
function square($x) { | |
$x * $x | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# imperative | |
$myVar = $null | |
if ($x -gt 10) { | |
$myVar = $true | |
} else { | |
$myVar = $false | |
} | |
# declarative | |
$myVar = if ($x -gt 10) {$true} else {$false} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$add = { | |
Param($a, $b) | |
$a + $b | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add($a, $b) { | |
$a + $b | |
} | |
$add = $function:add |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$add = [scriptblock](Get-Content add.ps1 -Raw) | |
$add > add.ps1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$n = 14 | |
function addToN($a) { | |
$n = $a + $n | |
} | |
&$function:addToN 3 | |
$n # `14` | |
.$function:addToN 3 | |
$n # `17` |