Last active
November 13, 2024 01:59
-
-
Save dfinke/1396da48210aca892edc1077c5829de5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 map { | |
param( | |
$func, | |
[Parameter(ValueFromRemainingArguments = $true)] | |
$arrays | |
) | |
$arrays | ForEach-Object { | |
if ($arrays[0].Length -ne $_.Length) { | |
throw "All arrays must be of the same length" | |
} | |
} | |
$dataType = $arrays.gettype().name | |
if ($dataType -eq "Object[]") { | |
$arrays | ForEach-Object { | |
& $func $_ | |
} | |
} | |
elseif ($dataType -eq 'List`1') { | |
$arrayCount = $arrays.Count | |
$count = $arrays[0].Length | |
if ($func -is [scriptblock]) { | |
$funcParams = $func.Ast.ParamBlock.Parameters | ForEach-Object { $_.Name.VariablePath.UserPath } | |
} | |
else { | |
$funcParams = (Get-Command $func).Parameters.Keys -as [array] | |
} | |
for ($i = 0; $i -lt $count; $i++) { | |
$funcSplat = [ordered]@{} | |
for ($ac = 0; $ac -lt $arrayCount; $ac++) { | |
$funcSplat["$($funcParams[$ac])"] = $arrays[$ac][$i] | |
} | |
& $func @funcSplat | |
} | |
} | |
} | |
$who = 'John', 'Jane', 'Doe', 'Smith' | |
$age = 20, 30, 40, 50 | |
$where = 'New York', 'California', 'Texas', 'Florida' | |
map { | |
param($who, $age, $where) | |
[PSCustomObject][Ordered]@{ | |
Name = $who | |
Age = $age | |
Location = $where | |
} | |
} $who $age $where | |
<# | |
Name Age Location | |
---- --- -------- | |
John 20 New York | |
Jane 30 California | |
Doe 40 Texas | |
Smith 50 Florida | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Introducing a Custom 'map' Function in PowerShell for Functional Programming
As developers, we're always looking for ways to write cleaner, more efficient code. One of the paradigms that facilitate this is functional programming, which emphasizes the use of functions and immutability. While PowerShell is primarily an object-oriented scripting language, it also supports functional programming concepts. Recently, I implemented a custom
map
function in PowerShell to process multiple arrays in a functional style. In this blog post, I'll walk you through the implementation and show you how it can be used to simplify your data processing tasks.The Need for a
map
Function in PowerShellIn languages like Python and JavaScript, the
map
function is a staple for transforming data collections. It applies a given function to each item of an iterable and returns a list of the results. While PowerShell has cmdlets likeForEach-Object
, they don't natively support mapping over multiple arrays simultaneously. To bridge this gap, I created a custommap
function that can apply a function to multiple arrays in parallel.The Implementation
Here's the core of the
map
function:Key Features
Usage Example
Let's see how this function works with a practical example. Suppose we have three arrays containing names, ages, and locations:
We can use the
map
function to combine these arrays into custom objects:Output:
The
map
function applies the script block to each set of elements from the arrays ($who[$i]
,$age[$i]
,$where[$i]
) and constructs a custom object with the combined data.How It Works
Benefits
map
function allows you to focus on the transformation logic.map
function with any number of arrays and any processing logic encapsulated in a script block or function.Conclusion
The custom
map
function brings the power of functional programming's map operation to PowerShell, enabling parallel processing of multiple arrays with ease. By leveraging script blocks and parameter extraction, it provides a flexible tool for data transformation tasks.If you often find yourself processing multiple related arrays, consider adding this
map
function to your toolkit. It can simplify your scripts and make your code more expressive and maintainable.Happy scripting!