fp

A library for programming in a functional style
Download

fp Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Stevan Little
  • Publisher web site:
  • http://search.cpan.org/~stevan/

fp Tags


fp Description

A library for programming in a functional style fp is a Perl module, an experiment in functional programming. It uses nothing but a combination of; subroutines, the @_ array and a few built in operators to implement a style of functional programming.None of the code above is all that interesting until you consider that at no point was variable assignment (=), if statements, or non-recursive iteration used. Although, do be entirely honest, there is actually two times when the = operator is used in the entire module. The first time is to assign the module's version, the second time is within the import routine, but those are really not parts of this library and really more infastructure anyway.Variable assignment is not utilized, instead the contents of the @_ argument array are accessed/manipulated and passed along as the return of values from functions. Recursion is the only means of iteration, we do not use any of perl's built in iteration mechanisms (for, foreach, while, etc.). All functions are non-destructive to their inputs, and just about everything returns an array of some sort, so function call chaining works quite well. It operates only on flat lists only, since perl will flatten any arrays given as arguments.This code is also written without side-effects. Meaning that each function is written to express an algorithm that produces its result rather than produce its result through the coercion of side-effects. Here is an example of what i mean, using even/odd predicate functions.with side effects: sub is_even { (($_ % 2) == 0); } sub is_odd { (($_ % 2) != 0); }without side efffects:sub is_even { ($_ < = 0) ? true : is_odd($_ - 1); }sub is_odd { ($_ < = 0) ? false : is_even($_ - 1); }SYNOPSIS use fp; # filter out all be the even numbers filter(function { is_even(head(@_)) }, range(1, 100)); # split the string, get unique list out of it # then get that list's length, and then check # that is equal to 26 is_equal_to(len(unique(explode("the quick brown fox jumped over the lazy dog and ran on down the road"))), 26); # the sum of the numbers 1 through 10 is 55 is_equal_to(sum(range(1, 10)), 55); Requirements: · Perl


fp Related Software