Functional Programming Resources

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Reaction score
405
Location
This purpose of this thread will be to share any resources about Functional Programming, in the hope of making it a bit easier for anyone wanting to learn more about this paradigm.

Where possible I suggest we tag any resource suggestion, for example:
  • Targeted level: Beginner, Intermediate, Advanced
  • Language, e.g. Scala, Javascript, Kotlin, Haskell, Swift, etc...

I will in during the course of today add some links, but also please go ahead and add any you have found helpful.
 
Last edited:
Please add:
Targeted level: Beginner, Intermediate
Language: PHP, Python

For me :)
 
Please add:
Targeted level: Beginner, Intermediate
Language: PHP, Python

For me :)
Here is a good set of repositories to fill in a number of standard library and language limitations of PHP:

PHP Functional Resources
  • functional-php - Primitives for functional programming in PHP
  • Non-standard PHP library (NSPL) - Compact functional programming oriented code and more
  • Pinq - PHP Integrated Query, a real LINQ library for PHP
  • YaLinqo - Yet Another LINQ to Objects for PHP
  • ginq - LINQ to Object inspired DSL for PHP
  • pramda - Practical Functional Programming in PHP
  • prelude - Functional library for PHP

Link: https://github.com/xgrommx/awesome-functional-programming#php

Intermediate to Advanced:
[table="width: 100%, class: outer_border"]
[tr]
[td]

Intermediate to Advanced types repository (created from functional-PHP)
Monad types available in the project:
  • State Monad
  • IO Monad
  • Free Monad
  • Either Monad
  • Maybe Monad
  • Reader Monad
  • Writer Monad

This is a good base from which to recreate what I demonstrated ito of input validation and concurrency
Link: https://github.com/widmogrod/php-functional
Dependency Link:: https://github.com/functional-php/fantasy-land

Ps. Maybe is the equivalent of the Scala Option type, and Either matches the same type in Scala. Naturally both will need some minor modification to enable concurrency I.e. To delink the applicative code from its Monadic design (replace flatMap). For Futures; both of these repos provide more than enough of the plumbing to recreate the Future type in PHP.


[/td]
[/tr]
[/table]
 
Last edited:
Java Functional Resources

highj
Firstly off the bat is an unbelievable bit of work to implement most of core algebras using a form of Higher Kind Type Polymorphism in Java -- an impossible task considering Java's generics doesn't support HKTs.

Higher Kinded Types (HKTs)
[table="width: 100%, class: outer_border"]
[tr]
[td]Higher-kinded polymorphism describes the presence of parametrically polymorphic values that abstract over types that are not grounded by the typical generic placeholder variables.[/td]
[/tr]
[/table]

Link: https://github.com/highj/highj


Functional Java
Functional Java is an open source library facilitating functional programming in Java. The library implements numerous basic and advanced programming abstractions that assist composition oriented development.

Link: https://github.com/functionaljava/functionaljava

Vavr
Vavr core is a functional library for Java. It helps to reduce the amount of code and to increase the robustness. A first step towards functional programming is to start thinking in immutable values. Vavr provides immutable collections and the necessary functions and control structures to operate on these values.

Link: http://www.vavr.io
 
Last edited:
Kotlin Functional Resources

arrow-kt
Functional companion to Kotlin's Standard Library; impressive amount of work.

link: http://arrow-kt.io
 
Been doing quite a bit of php lately, and even though lstrojny/functional-php is great, it is always a bit irritating the syntax needed to use filter and map for example.

So I just wrote a little fluent array helper class to make life that little bit better, adding methods as I need them.

Code:
class FluentArray
{

    /**
     * @var Traversable|array
     */
    private $array;

    /**
     * FluentArray constructor.
     * @param Traversable|array $array
     */
    public function __construct($array)
    {
        $this->array = $array;
    }

    /**
     * @param Traversable|array $array
     * @return FluentArray
     */
    public static function from($array)
    {
        return new self($array);
    }

    /**
     * @return array|Traversable
     */
    public function toArray()
    {
        return $this->array;
    }

    /**
     * @param callable $callback
     * @return $this
     */
    public function filter($callback)
    {
        $this->array = filter($this->array, $callback);
        return $this;
    }

    /**
     * @param callable $callback
     * @return $this
     */
    public function map($callback)
    {
        $this->array = map($this->array, $callback);
        return $this;
    }

     //snipped

}

Usage

Code:
    /**
    * @test
    */
    public function chainingWorks()
    {
        $given = [1, 2, 3, 4, 5, 6, 7];
        $expected = filter($given, [TestFunctions::class, 'isEven']);
        $expected = map($expected , [TestFunctions::class, 'multiplyByTwo']);
        $actual = FluentArray::from($given)
            ->filter([TestFunctions::class, 'isEven'])
            ->map([TestFunctions::class, 'multiplyByTwo'])
            ->toArray();
        Assert::thatArray($actual)
            ->hasSize(sizeof($expected))
            ->isEqualTo($expected);
    }
 
Last edited:
Been doing quite a bit of php lately, and even though lstrojny/functional-php is great, it is always a bit irritating the syntax needed to use filter and map for example.

So I just wrote a little fluent array helper class to make life that little bit better, adding methods as I need them.

Code:
class FluentArray
{

    /**
     * @var Traversable|array
     */
    private $array;

    /**
     * FluentArray constructor.
     * @param Traversable|array $array
     */
    public function __construct($array)
    {
        $this->array = $array;
    }

    /**
     * @param Traversable|array $array
     * @return FluentArray
     */
    public static function from($array)
    {
        return new self($array);
    }

    /**
     * @return array|Traversable
     */
    public function toArray()
    {
        return $this->array;
    }

    /**
     * @param callable $callback
     * @return $this
     */
    public function filter($callback)
    {
        $this->array = filter($this->array, $callback);
        return $this;
    }

    /**
     * @param callable $callback
     * @return $this
     */
    public function map($callback)
    {
        $this->array = map($this->array, $callback);
        return $this;
    }

     //snipped

}

Usage

Code:
    /**
    * @test
    */
    public function chainingWorks()
    {
        $given = [1, 2, 3, 4, 5, 6, 7];
        $expected = filter($given, [TestFunctions::class, 'isEven']);
        $expected = map($expected , [TestFunctions::class, 'multiplyByTwo']);
        $actual = FluentArray::from($given)
            ->filter([TestFunctions::class, 'isEven'])
            ->map([TestFunctions::class, 'multiplyByTwo'])
            ->toArray();
        Assert::thatArray($actual)
            ->hasSize(sizeof($expected))
            ->isEqualTo($expected);
    }
Suggest you have a look at this:
https://github.com/sebastiaanluca/php-pipe-operator

Best solution I've found in the absence of real pipe operators in PHP.
 
Also what do you think about a functional thread focused solely on exploring some basic FP in PHP. Let me know of any ideas you have for something like this.

I have a few handy functions that I can introduce; easy solutions that are good in any language.
 
C# Functional Library
C# Functional Programming Language Extensions
This library uses and abuses the features of C# to provide a functional-programming 'Base class library', that, if you squint, can look like extensions to the language itself. The desire here is to make programming in C# much more reliable and to make the engineer's inertia flow in the direction of declarative and functional code rather than imperative.

Join the chat: https://gitter.im/louthy/language-ext
Link: https://github.com/louthy/language-ext
 
Top
Sign up to the MyBroadband newsletter
X