Wednesday, May 11, 2011

Side Project: Functional Programming

There's been a revival in interest in functional programming recently. Languages like Haskel have been getting attention. I wanted to work on some of this and – thanks, again, to PowerShell for being nifty – I can do so directly using PowerShell Script Blocks.

Function programming approaches problems using state-less code and not relying on side effects. Where Object Oriented programming would start by wrapping a state in a class, along with the behaviors that modify the state, functional programming creates functions – in nearly the mathematical sense of the word – that produce the desired behaviors. Because the functions do not rely on side-effects, they work exactly as expected, every time.

I do appreciate the clarity of intent inherent in this statement of the approach. The problem that I'm having is that calculating Fibonacci’s sequence doesn't come in handy for me that often.

There has to be a way I learn something out of all this.

Here's some scripting I did:

# new-alias -name run -value invoke-command


$tick = {

param ($wheelState, $wheelSize = 10)

$wheelState++

if ($wheelState -gt ($wheelSize-1)) { 0 }

else {$wheelState}

}


$a = {

param ($b, $c = 0)


if ($b -gt 0) {

$b--;

$c = run $tick -arg $c


run $a -argument $b,$c

$c

}


}

run $a -arg 22


The first line lets you use “run” instead of “invoke-command” since I like run better. You can only set the alias once per session (maybe I'll add it to my profile later.)

This is the odometer problem that I worked on a couple of months back. It models a wheel that counts from 0 to 9 and returns to 0.

The first script block handles that 0 to 9 stuff. I even set it to take a default value of 10, allowing you to use it for any size wheel.

You know, these are hard to read. I should document them.

[LESSTHAN]#

.SYNOPSIS

Models a wheel in an odometer, including values from 0 to x, default 10.


.DESCRIPTION

Takes a value and the size of this odometer wheel and increases the odometer value

by 1 and returns that value. If the value exceeds the size of the wheel, the value is

reset to 0.

.PARAMETER $wheelState

The current state of the wheel. This value is not validate, so it could be larger than

the size of the wheel or less than 0.

.PARAMETER $wheelSize

Size of the wheel to be ticked. Default is 10 (0 to 9).

#[GREATERTHAN]

I used the PowerShell comment-based help standard. I can't just get the help output from the script block. If you turn it into a function, though, and use “get-help” you get the following:

PowerShell: get-help x


NAME

x

SYNOPSIS

Models a wheel in an odometer, including values from 0 to x, default 10.

SYNTAX

x [[-wheelState] [LESSTHAN]Object[GREATERTHAN]] [[-wheelSize] [LESSTHAN]Object[GREATERTHAN]] [[LESSTHAN]CommonParameters[GREATERTHAN]]

DESCRIPTION

Takes a value and the size of this odometer wheel and increases the odometer value

by 1 and returns that value. If the value exceeds the size of the wheel, the value is

reset to 0.


RELATED LINKS


REMARKS

To see the examples, type: "get-help x -examples".

For more information, type: "get-help x -detailed".

For technical information, type: "get-help x -full".

And the other script block:

PowerShell: get-help x


NAME

x

SYNOPSIS

Increases an odometer wheel by a specified number of ticks.

SYNTAX

x [[-increment] [LESSTHAN]Object[GREATERTHAN]] [[-state] [LESSTHAN]Object[GREATERTHAN]] [[LESSTHAN]CommonParameters[GREATERTHAN]]

DESCRIPTION

Increases an odometer wheel by the specified number of ticks. This version

assumes the default odometer wheel (10).

The default state of the odometer is 0, but it can take any value.


RELATED LINKS


REMARKS

To see the examples, type: "get-help x -examples".

For more information, type: "get-help x -detailed".

For technical information, type: "get-help x -full".

While I was going through this, I realized that not having any validation is – well, I don't like it.

In the case of functional programming, having no validation means that there are no expected outputs for a large number of possible inputs. This is just messy.

This command should cover it:


if ($wheelState -lt 0) { 0 }

If the $wheelstate is larger than the wheel size, the next line of script will make it 0 anyway. This makes 0 the default for all unknown inputs.

Here's my new script:

# new-alias -name run -value invoke-command


$tick = {

[LESSTHAN]#

.SYNOPSIS

Models a wheel in an odometer, including values from 0 to x, default 10.


.DESCRIPTION

Takes a value and the size of this odometer wheel and increases the odometer value

by 1 and returns that value. If the value exceeds the size of the wheel, the value is

reset to 0.

.PARAMETER $wheelState

The current state of the wheel. This value is not validate, so it could be larger than

the size of the wheel or less than 0.

.PARAMETER $wheelSize

Size of the wheel to be ticked. Default is 10 (0 to 9).

#[GREATERTHAN]

param ($wheelState, $wheelSize = 10)

$wheelState++

if ($wheelState -lt 0) { 0 } # Validate input: any negative value returns a state of 0.

if ($wheelState -gt ($wheelSize-1)) { 0 } # Any value greater than size returns a value of 0.

else {$wheelState} # Return wheel state other than 0.

}


$turnWheel = {

[LESSTHAN]#

.SYNOPSIS

Increases an odometer wheel by a specified number of ticks.


.DESCRIPTION

Increases an odometer wheel by the specified number of ticks. This version

assumes the default odometer wheel (10).

The default state of the odometer is 0, but it can take any value.

.PARAMETER $increment

Number of ticks to be added to wheel.

.PARAMETER $state

Current state of wheel.

#[GREATERTHAN]

param ($increment, $state = 0)

if ($increment -lt 1) { $state } # A value of 0 or less than 0 returns the state unchanged.

if ($increment -gt 0) { # While increment value is greater than 0:

$increment--; # Decrease increment value.

$state = run $tick -arg $state # Tick odometer wheel.


run $turnWheel -argument $increment,$state # Recurse this script block.

}

$state # Display state.


}

"Value 22:"

run $turnWheel -arg 22


"Value 22, starting state 10:"

run $turnWheel -arg 22,10



"Value -1:"

$x = -1

run $turnWheel -arg $x,10 #NOTE: This returns an invalid state.

The only fly in the ointment, now, is that you can get an invalid state – if the increment operation fails, you get a state that can't be the result of a tick. That is, 10 is too large for a wheel. That can be fixed later.



No comments:

Post a Comment