luarrow.lua introduces true pipeline operators and Haskell-style function composition to Lua, enhancing code clarity and elegance. With powerful operator overloading, it allows for expressive and readable code structures that resemble familiar syntax from languages like OCaml and PHP. Transform Lua code into cleaner, more manageable expressions.
luarrow is a Lua library designed to bring functional programming paradigms into the Lua ecosystem, specifically true pipeline operators and elegant Haskell-style function composition. This library enhances the readability and expressiveness of Lua code by allowing developers to implement functional programming techniques that are typically found in languages like Haskell, OCaml, and Elixir.
Key Features
-
True Pipeline Operators: Implement functional pipelines through natural left-to-right flow, utilizing the
%and^operators. This enables syntax such asx % f ^ g, making function chaining intuitive and easy to read. -
Haskell-inspired Function Composition: The use of
*andfunallows for Haskell-like function compositions, letting users writef * g % xinstead of the more cumbersomef(g(x)), eliminating the clutter of parentheses. -
Zero Dependencies: luarrow is a pure Lua implementation, ensuring it can be easily integrated into any Lua project without external dependencies.
-
Excellent Performance: In LuaJIT environments, pre-composed functions exhibit virtually no overhead compared to traditional Lua constructs, ensuring that performance remains optimal.
Usage Examples
Pipeline Composition
Use the arrow function for pipeline-style processing:
local arrow = require('luarrow').arrow
local _ = 42
% arrow(function(x) return x - 2 end)
^ arrow(function(x) return x * 10 end)
^ arrow(function(x) return x + 1 end)
^ arrow(print) -- Outputs: 401
Haskell-style Composition
For a more mathematical approach, the fun function enables right-to-left composition:
local fun = require('luarrow').fun
local function f(x) return x + 1 end
local function g(x) return x * 10 end
local function h(x) return x - 2 end
local result = fun(f) * fun(g) * fun(h) % 42
print(result) -- Outputs: 401
Real-World Applications
Create data transformation pipelines and streamline list processing:
local fun = require('luarrow').fun
local numbers = {1, 2, 3, 4, 5, 6}
local is_even = function(x) return x % 2 == 0 end
local double = function(x) return x * 2 end
local result = fun(map(double)) * fun(filter(is_even)) % numbers
print(result) -- Outputs: { 4, 8, 12 }
Documentation and Learning Resources
For detailed guidance on the API and practical usage, refer to the API Reference and explore practical examples in the Examples Directory.
luarrow aims to make Lua coding expressive, elegant, and a more enjoyable experience for developers, fostering cleaner and more maintainable code that reads like poetry.
No comments yet.
Sign in to be the first to comment.