{"id":13500954,"url":"https://github.com/ezerfernandes/funkmoon","last_synced_at":"2026-02-06T23:04:59.486Z","repository":{"id":95840315,"uuid":"49037125","full_name":"ezerfernandes/funkmoon","owner":"ezerfernandes","description":"Functional Tools for Lua","archived":false,"fork":false,"pushed_at":"2024-08-31T11:31:30.000Z","size":28,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-31T19:37:42.317Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ezerfernandes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-01-05T02:38:33.000Z","updated_at":"2024-08-31T11:31:33.000Z","dependencies_parsed_at":"2023-04-02T13:34:09.761Z","dependency_job_id":null,"html_url":"https://github.com/ezerfernandes/funkmoon","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ezerfernandes%2Ffunkmoon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ezerfernandes%2Ffunkmoon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ezerfernandes%2Ffunkmoon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ezerfernandes%2Ffunkmoon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ezerfernandes","download_url":"https://codeload.github.com/ezerfernandes/funkmoon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246155998,"owners_count":20732355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T22:01:20.626Z","updated_at":"2026-02-06T23:04:59.480Z","avatar_url":"https://github.com/ezerfernandes.png","language":"Lua","readme":"# funkmoon - Functional Tools for Lua\n\n`funkmoon` is a collection of functional programming utilities for Lua, inspired by languages like Scala. It provides a set of tools to work with tables in a functional style, supporting method chaining to simulate a pipeline-like structure.\n\n## Installation\n\nTo use `funkmoon`, simply require it in your Lua scripts:\n\n```lua\nlocal funkmoon = require \"funkmoon\"\n```\n\n## Usage\n\n### As standalone functions\n\nYou can use funkmoon functions directly, as shown in the following example:\n\n```lua\nlocal list = {1, 2, -3, 4}\n\n-- Returns only even numbers.\nlocal even = funkmoon.filter(list, function(n) return n % 2 == 0 end)\n```\n\n### As methods (pipeline style)\n\nThis is the preferred way if you want to use several functions as a pipeline, with [method chaining](https://en.wikipedia.org/wiki/Method_chaining), simulating what you can do in functional languages like Scala, for example.\n\n```lua\n-- Returns the sum of the squares of even numbers.\nlocal list = funkmoon.FunctionalTable({1, 2, -3, 4, 9, 8})\n\nlocal sumOfSquaresEvenNumbers = list\n        :filter(function(n) return n % 2 == 0 end)\n        :map(function(n) return n*n end)\n        :reduce(function(a, b) return a + b end)\n```\n\nFunctionalTable adds a metatable that allows to call the functions as methods and use this development style.\n\n## API Reference\n\n`map(list, fn)`\n\nBuilds a new table by applying `fn` to each element of `list`.\n\n```lua\nlocal list = {3, 5, 8}\nlocal squares = funkmoon.map(list, function(n) return n*n end)\n-- Result: {9, 25, 64}\n```\n\n`flatMap(list, fn)`\n\nBuilds a new table by applying `fn` to each element of `list` and flattening the result.\n\n```lua\nlocal values = {0, 5, 10}\nlocal mapped = funkmoon.flatMap(values, function(x) return {x-1, x, x+1} end)\n-- Result: {-1, 0, 1, 4, 5, 6, 9, 10, 11}\n```\n\n`filter(list, predicate)`\n\nSelects all elements of `list` which satisfy `predicate`.\n\n```lua\nlocal list = {1, 4, 6, 3, 7}\nlocal oddNumbers = funkmoon.filter(list, function(n) return n % 2 == 1 end)\n-- Result: {1, 3, 7}\n```\n\n`filterNot(list, predicate)`\n\nSelects all elements of `list` which don't satisfy `predicate`.\n\n```lua\nlocal values = {1, 2, 3, 4}\nlocal filtered = funkmoon.filterNot(values, function(n) return n % 2 == 0 end)\n-- Result: {2, 4}\n```\n\n`foldLeft`\n\nApplies a binary function (fn) to startValue and all elements of `list`, going left to right.\n\n```lua\nlocal list = {1, 2, 3, 4, 5}\nlocal sum = funkmoon.foldLeft(list, 0)(function(acc, n) return acc + n end)\n-- Result: 16\n```\n\n`foldRight`\n\nApplies a binary function (fn) to startValue and all elements of 'list', going right to left.\n\n```lua\nlocal list = {1, 2, 3, 4}\n\n-- Concatenate numbers from right to left\nlocal result = funkmoon.foldRight(list, \"\")(function(acc, n)\n    return acc .. tostring(n)\nend)\n\n-- result: \"4321\"\n```\n\n`reduce`\n\nReduces the elements of 'list' using the binary operator 'fn'.\n\n```lua\nlocal values = {1, 2, 3, 4, 5}\nlocal result = reduce(values, function(acc, n) return acc * n end)\n-- result: 120\n```\n\n`find`\n\nFinds the first element of 'list' satisfying a predicate, if any.\n\n```lua\nlocal values = {1, 5, 8, 3, 9, 4, 6}\nlocal result = funkmoon.find(values, function(n) return n % 2 == 0 end)\n-- result: { [3] = 8 }\n```\n\n`arrayPart`\n\nReturns the array-like part of list (1 to n).\n\n```lua\nlocal mixedTable = { foo = 3, 2 = \"hello\", bar = \"world\", 1 = 42 }\nlocal result = funkmoon.arrayPart(mixedTable)\n-- result: {42, \"hello\"}\n```\n\n`partition`\n\nPartitions 'list' in two tables according to 'predicate'.\n\n```lua\nlocal evens, odds = partition({1, 3, 2, 7, 4, 9}, function(n) return n % 2 == 0 end)\n-- evens = {2, 4}; odds = {1, 3, 7, 9}\n```\n\n`takeWhile`\n\nTakes longest prefix of elements of 'list' that satisfy 'predicate'.\n\n```lua\nlocal testList = {1, 2, 3, 4, 0, 2, 5}\nlocal result = funkmoon.takeWhile(testList, function(n) return n \u003c 4 end)\n-- result: {1, 2, 3}\n```\n\n`dropWhile`\n\nDrops longest prefix of elements of 'list' that satisfy 'predicate'.\n\n```lua\nlocal testList = {1, 2, 3, 4, 0, 2, 5}\nlocal result = funkmoon.takeWhile(testList, function(n) return n \u003c 4 end)\n-- result: {4, 0, 2, 5}\n```\n\n`any`\n\nTests whether 'predicate' holds for some of the elements of 'list'.\n\n```lua\nlocal values = {1, 2, 7, 0}\nlocal result = funkmoon.any(values, function(n) return n \u003e 5 end)\n-- result: true\n```\n\n`all`\n\nTests whether 'predicate' holds for all elements of 'list'.\n\n```lua\nlocal values = {1, 2, 7, 0}\nlocal result = funkmoon.all(values, function(n) return n \u003e 5 end)\n-- result: false\n```\n\n`corresponds`\n\nTests whether every element of 'list' relates to the corresponding element of 'otherList' by satisfying a test predicate.\n\n```lua\nlocal aList = {1, 2, 3}\nlocal anotherList = {2, 4, 6}\nlocal result = funkmoon.corresponds(aList, anotherList)(function(a, b) return 2*a == b end)\n-- result: true\n```\n\n`fill`\n\nCreates a table with 'value' repeated 'n' times.\n\n```lua\nlocal result = funkmoon.fill(3)(\"hello!\")\n-- result: {\"hello!\", \"hello!\", \"hello!\"}\n```\n\n`distinct`\n\nBuilds a new list from this 'list' with no duplicate elements.\n\n```lua\nlist = {1, 2, 2, 3, 1, 2, 5}\nresult = funkmoon.distinct(list)\n-- result: {1, 2, 3, 5}\n```\n\n`groupBy`\n\nGets the elements and keys from 'list' and partitions them by the result of the function fn(key, element), returning a new table where fn(key, element) are the keys and the values are tables with the keys and values.\n\n```lua\nlist = {1, 2, 3, 4}\ngrouped = funkmoon.groupBy(list, function(_, n) return n % 2 == 0 end)\n-- grouped: {[true] = {{2, 2}, {4, 4}}, [false] = {{1, 1}, {3, 3}}}\n```\n\n`partial`\n\nReturns a new function with partial application of the given arguments.\n\n```lua\nlocal function sum(a, b)\n        return a + b\nend\n\nlocal increment = funkmoon.partial(sum, 1)\n\nlocal result = increment(4)\n-- result: 5\n```\n\n`apply`\n\nApplies a function using list as an argument\n\n```lua\nvalues = {0, 9, -4}\n\n-- sum the three values\nresult = funkmoon.apply(values, function(a, b, c) return a+b+c end)\n```\n\n`isEmpty`\n\nTest whether `list` is empty.\n\n```lua\nresult = funkmoon.isEmpty({}) -- result: true\nresult = funkmoon.isEmpty({2, 3}) -- result: false\n```\n\n`max`\n\nReturns a functional table with the greatest element of a table.\n\n```lua\n-- result: 9\nlocal result = funkmoon.max({-3, 2, 9, 4})\n\n-- square of the greatest number\nlocal result = funkmoon.FunctionalTable(values)\n        :max()\n        :apply(function (n) return n * n end)\n```\n\n`min`\n\nReturns a functional table with the smallest element of a table.\n\n```lua\n-- result: -3\nlocal result = funkmoon.min({-3, 2, 9, 4})\n\n-- checks if the smallest number is positive.\nlocal result = funkmoon.FunctionalTable(values)\n        :min()\n        :all(function(n) return n \u003e 0 end)\n```\n\n`ifEmpty`\n\nReturns a functional table with the smallest element of a table.\n\n```lua\n-- result = 5\nlocal result = funkmoon.FunctionalTable({ 1, 2, 3, 4 })\n        :filter(function(n) return n \u003e 4 end)\n        :ifEmpty(5)\n```\n\n`zip`\n\nReturns a new table formed from 'list' and 'otherList' by combining corresponding elements in pairs.\n\n```lua\nlocal x = {1, 2}\nlocal y = {\"blue\", \"green\"}\nlocal result = funkmoon.zip(x, y)\n-- result: { {1, \"blue\"}, {2, \"green\"} }\n```\n\n`unzip`\n\nConverts this 'list' of pairs into two tables of the first and second half of each pair.\n\n```lua\nlocal list = { {5, 'a'}, {8, 'b'} }\nlocal numbers, letters = funkmoon.unzip(list)\n-- numbers = {5, 8}; letters = {'a', 'b'}\n```\n\n`slice`\n\nReturns a new table with the elements of 'list' from 'from' to 'to'.\n\n```lua\nlocal list = {5, 21, 8, 2, 9, 11}\nlocal result = funkmoon.slice(list, 3, 5)\n-- result: {8, 2, 9}\n```\n\n`reverse`\n\nReturns a new table with the elements of 'list' reversed.\n\n```lua\nlocal list = {5, 2, 4, 1}\nlocal result = funkmoon.reverse(list)\n-- result: {1, 4, 2, 5}\n```\n\n`range`\n\n```lua\n\nlocal result = funkmoon.range(1, 5, 2)\n-- result: {1, 3, 5}\n```\n\n`irange`\n\n```lua\nfor i in funkmoon.irange(1, 4, 2) do\n    print(i)\nend\n--[[\nprinted:\n1\n3\n]]\n```\n\n`fill` and `ifill`\n\nFills a list with repeated values.\n\n```lua\nlocal filledList = funkmoon.fill(5)(\"hello\")\n-- filledList: {\"hello\", \"hello\", \"hello\", \"hello\", \"hello\"}\n\n```\n\n`stream` and `istream`\n\nCreates an iterator that generates values or repeats a function call.\n\n```lua\nlocal stream = funkmoon.stream(function(x) return x + 1 end, 0)\nlocal result = {}\nfor i = 1, 5 do\n    result[i] = stream()\nend\n-- result: {1, 2, 3, 4, 5}\n\nlocal count = 0\nfor _ in funkmoon.itimes(3, function() count = count + 1 end) do end\n-- count: 3\n```\n\n`itimes`\n\nCreates an iterator that calls a function a specified number of times.\n\n```lua\nlocal count = 0\nfor _ in funkmoon.itimes(3, function()\n    count = count + 1\n    print(\"This is call number \" .. count)\nend) do\n    -- The loop will run 3 times\nend\n\n-- Output:\n-- This is call number 1\n-- This is call number 2\n-- This is call number 3\n```\n\n## Contributing\n\nContributions are welcome! Please fork the repository and submit a pull request.\n\n## License\n\nThis project is licensed under the MIT License.\n","funding_links":[],"categories":["Lua"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fezerfernandes%2Ffunkmoon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fezerfernandes%2Ffunkmoon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fezerfernandes%2Ffunkmoon/lists"}