Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Functional Php Laravel Package

lstrojny/functional-php

Functional PHP adds a rich set of functional programming helpers for PHP: map/filter/reduce, partial application, currying, composition, and collections utilities. Write cleaner, more declarative code without changing your framework or coding style.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add via Composer:

    composer require lstrojny/functional-php
    

    No Laravel-specific configuration is needed—this is a pure PHP package.

  2. First Use Case: Data Transformation Replace a foreach loop with map:

    use function Functional\map;
    
    $numbers = [1, 2, 3];
    $doubled = map($numbers, fn($n) => $n * 2); // [2, 4, 6]
    
  3. Key Entry Points

    • Functional\* namespace: Core functions (e.g., map, filter, reduce).
    • Functional\Collection: Immutable collection utilities (e.g., from, pipe).
    • Laravel Integration: Use with Laravel Collections via collect()->pipe(fn($c) => Functional\map($c->all(), ...)).

Implementation Patterns

1. Functional Pipelines

Chain operations immutably:

use function Functional\{map, filter, reduce};

$result = reduce(
    map(
        filter($users, fn($u) => $u['active']),
        fn($u) => $u['id']
    ),
    [],
    fn($acc, $id) => [...$acc, $id],
    []
);

2. Laravel Collection Integration

Combine with Laravel’s Collection for SQL-like operations:

$activeUsers = collect($users)
    ->filter(fn($u) => $u['active'])
    ->pipe(fn($c) => Functional\map($c->values(), fn($u) => $u['name']));

3. Partial Application

Pre-bind arguments to functions:

use function Functional\partial;

$addFive = partial('+', 5);
$result = $addFive(3); // 8

4. Currying

Break multi-argument functions into single-argument steps:

use function Functional\curry;

$add = curry(fn($a, $b) => $a + $b);
$addFive = $add(5);
$result = $addFive(3); // 8

5. Error Handling

Use try/catch with functional style:

use function Functional\{try, catch};

$safeDivide = try(fn($a, $b) => $a / $b, fn($e) => 0);
$result = $safeDivide(10, 0); // 0

6. Testing

Write declarative tests:

$this->assertEquals(
    [1, 2, 3],
    Functional\map([1, 2, 3], fn($n) => $n)
);

Gotchas and Tips

Pitfalls

  1. Immutability Overhead

    • Functional operations return new arrays/collections. Avoid chaining excessively in performance-critical loops.
    • Fix: Use Functional\Collection::from() for large datasets to optimize memory.
  2. Closure Scope Issues

    • Closures in map/filter may not capture expected variables if not declared use.
    • Fix: Explicitly declare dependencies:
      $threshold = 10;
      $filtered = filter($items, fn($item) => $item > $threshold);
      
  3. Laravel Collection Conflicts

    • Avoid mixing Functional\map with Laravel’s Collection::map directly (e.g., map($collection) fails).
    • Fix: Extract values first:
      Functional\map($collection->all(), ...);
      
  4. Lazy Evaluation Misuse

    • Functions like lazy/flatMap require understanding of lazy sequences.
    • Fix: Force evaluation with toArray() or reduce when needed.

Debugging Tips

  1. Inspect Intermediate Steps Use Functional\tap to log/debug:

    $result = tap($data, fn($d) => Log::debug('Data:', $d))
        ->pipe(fn($d) => map($d, ...));
    
  2. Type Safety

    • PHP’s dynamic typing can lead to runtime errors. Use @var hints in IDEs (e.g., PhpStorm) for functional chains.
  3. Performance Profiling

    • Functional operations may create intermediate arrays. Profile with memory_get_usage() for large datasets.

Extension Points

  1. Custom Combinators Extend with your own higher-order functions:

    function customMap($collection, callable $fn) {
        return Functional\map($collection, $fn);
    }
    
  2. Laravel Service Providers Bind functional helpers to Laravel’s container:

    $this->app->bind('functional', fn() => new Functional\Collection());
    
  3. DSL for Domain Logic Create domain-specific functions:

    function calculateTax($items) {
        return reduce($items, 0, fn($acc, $item) => $acc + ($item['price'] * 0.2));
    }
    
  4. Integration with Livewire/Alpine Use functional style in component logic:

    public function mount() {
        $this->filteredItems = filter($this->items, fn($item) => $item['visible']);
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony