Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

PowerShell does have the equivalent of a "lambda" or "unnamed" function via script blocksscript blocks, but they are an advanced concept and (generally) not useful in code-golf, and so aren't covered here in this introductory topic.

PowerShell does have the equivalent of a "lambda" or "unnamed" function via script blocks, but they are an advanced concept and (generally) not useful in code-golf, and so aren't covered here in this introductory topic.

PowerShell does have the equivalent of a "lambda" or "unnamed" function via script blocks, but they are an advanced concept and (generally) not useful in code-golf, and so aren't covered here in this introductory topic.

Source Link
AdmBorkBork
  • 43.5k
  • 2
  • 26
  • 41

PowerShell

Your submission should be a program or function. It should print the output or return it. Since an implicit Write-Output happens when a script finishes execution, simply leaving the result on the pipeline also suffices.

These example submissions compute the factorial:

General I/O

# Program that prints (implicitly), takes input via command-line arguments
param($n)$p=1
for($i=1; $i -le $n; $i++){$p*=$i}
$p

# Named function that returns, could also be "filter f($n){"
function f($n){
  $p=1
  for($i=1; $i -le $n; $i++){$p*=$i}
  $p
}

PowerShell does have the equivalent of a "lambda" or "unnamed" function via script blocks, but they are an advanced concept and (generally) not useful in code-golf, and so aren't covered here in this introductory topic.

You may not expect the input to be pre-stored in a variable

# Invalid, assumes the input already exists in $n
$p=1
for($i=1; $i -le $n; $i++){$p*=$i}
$p

Nor may you output by just saving the results in a variable.

# Invalid, simply saves results to $p with no output
param($n)$p=1
for($i=1; $i -le $n; $i++){$p*=$i}

Inputs

We're pretty loose with input formats. For example, if a challenge says to take in a list of numbers, you can expect input as a PowerShell array like $l=@(1, 2, 3), not like "1 2 3". So, for this example, a PowerShell program can do

param($l)$l.GetType()

and expect that the output will say it's of type System.Array.

However, in general, it's frowned upon to take in "a list of numbers" as separate command-line arguments and use the built-in $args as the array. This is allowed in some circumstances; ask on the particular challenge.

Version

The latest version of the open-sourced repository is generally assumed, and is the version used on the "Try It Online!" site. In general, versions are backwards compatible, so if there's a feature in v2, for example, it should also be available in the latest version, and you shouldn't need to specify.

Libraries

You may import libraries and .NET features. The import commands, like Add-Type -A System.Windows.Forms, or static calls like [system.math]::Floor(), must be included in your byte count. If it's a non-default library, like the PowerShell Community Extensions, call the language "PowerShell with PSCX" in the header.

Truthy/Falsey

PowerShell is strictly typed, but it attempts to dynamically cast on the fly as necessary. For PowerShell data types, the following code snippet is used to determine if something is truthy or falsey:

if($x){
 "True"
}
else{
 "False"
}

This means that 0, $false, "" (empty string), @() (empty array), $null, and variables set to those objects (including uninitialized variables, which default to $null) are falsey, and everything else is truthy.

Most of the formatting of this post comes from @xnor's Python answer. Thanks!

Post Made Community Wiki by AdmBorkBork