{"id":15616497,"url":"https://github.com/shakadak/leekwars-functional","last_synced_at":"2025-03-29T15:14:14.684Z","repository":{"id":75551412,"uuid":"66652122","full_name":"Shakadak/leekwars-functional","owner":"Shakadak","description":"list and array functions respectively implemented and reimplemented for currying and performance.","archived":false,"fork":false,"pushed_at":"2018-01-20T19:43:06.000Z","size":61,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-04T15:50:18.746Z","etag":null,"topics":["functional-programming","leek-wars","leekscript"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/Shakadak.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-26T14:01:53.000Z","updated_at":"2018-04-27T21:39:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"f50b5566-db42-4406-9200-dd1ac3ada878","html_url":"https://github.com/Shakadak/leekwars-functional","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/Shakadak%2Fleekwars-functional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakadak%2Fleekwars-functional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakadak%2Fleekwars-functional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shakadak%2Fleekwars-functional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shakadak","download_url":"https://codeload.github.com/Shakadak/leekwars-functional/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246200323,"owners_count":20739566,"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":["functional-programming","leek-wars","leekscript"],"created_at":"2024-10-03T07:09:33.732Z","updated_at":"2025-03-29T15:14:14.662Z","avatar_url":"https://github.com/Shakadak.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# leekwars-functional\nlist and array functions respectively implemented and reimplemented for currying and performance.\n\n[leekwars](https://leekwars.com)\n\n# Why\n\nI originally wanted my array functions to be curryfied, as I like to work with partial application ready at a go. For some reason, I did not think of naming them differently, and I did not think of just calling them inside the curryfied function. So I redid them with a `for` loop, although only `arrayMap` and `arrayFilter`, I did not have much use for the others at the time. Plus they seem to have a reputation of being slower than `for in` loops. I do not remember much if that is really their reputation, but they are slower for sure. So I decided to redo most of them and more.\n\nThen I came upon the idea of doing a list minilib. The same functions as for arrays, with function helpers for actually making the lists. Leekscript does not currently possess the capacity to make custom structure, so our lists are just pure functions. I may do trees later too.\nI wanted list, first because of my functional tendencies, second because I wanted something that can add and remove element in constant time, and third because the idea of bringing a new data structure to the community make me feel pretty cool. Although I doubt it will see real usage.\n\nWhile doing them, battling against infinite loops, I used my array functions to compare both the result and the performance. I was pleased to see how my lists were doing in comparison to arrays. (While sweeping random access under the rug.) Came a time where I wanted to see how much of an improvement I did against the default functions. (I like to feel good about what I do.)\nThen the comparison with arrayFoldLeft and arrayFoldRight. Holy garden is this fertilizer fast as pollination. I cannot beat it.\nAlthough it improves again the performances of other array functions, the fact that array building is so heavy make it so that lists are still worth it. So I am not too unhappy. Plus it is still cool to have the array functions not using anymore the imperative loops.\n\n# Array functions usage\n\nHere are some examples on how to use the array functions.\n```js\n[code]\nvar xs = [1, 2, 3];\nvar ret = aMap(function(x) {return x + 1;})(xs);\n// ret = [2, 3, 4]\n[/code]\n```\n\n```js\n[code]\nvar xs = [1, 2, 3];\nvar ret = aFilter(function(x) {return x === 2;})(xs);\n// ret = [2]\n[/code]\n```\n\n```js\n[code]\nfunction add(x, y) {return x + y;}\nvar xs = [1, 2, 3];\nvar ret = aFoldRu(add)(0)(xs);\n// ret = 6\n[/code]\n```\n\nImportant things to understand with foldLeft and foldRight:\n* The accumulator (second argument) is generally an identity element, like 0 in the case of addition (1 + 0 = 1). Though this is not necessary in any way.\n* The consuming function receive the both the accumulator and an element of the array as argument. In the case of foldLeft, the accumulator is on the *left* and in the case of foldRight, the accumulator is on the *right*. In case of doubt, look at the type. `b` is the accumulator, `a` is the consumed. Looking at the type signatures of aFoldRu and aFoldLu may be easier if you are not used to the notation.\n\nUsing `add` from `math`:\n```js\n[code]\nvar xs = [1, 2, 3];\nvar fs = [add(0), add(1), add(100)];\nvar ret = aApply(fs)(xs);\n// ret = [1, 2, 3, 2, 3, 4, 101, 102, 103]\n[/code]\n```\n\n```js\n[code]\nvar prepend123 = append([1, 2, 3]); // Equivalent to function(@x) { return [1, 2, 3] + x; }\nvar xs = prepend123([2, 3, 4]);\nvar ys = prepend123([]);\n// xs = [1, 2, 3, 2, 3, 4]\n// ys = [1, 2, 3]\n[/code]\n```\n\naIntersection and aRelativeComplement are functions on set.\n[Set_(mathematics)#Basic_operations](https://en.wikipedia.org/wiki/Set_(mathematics)#Basic_operations) \u003chttps://en.wikipedia.org/wiki/Set_(mathematics)#Basic_operations\u003e\n\n# List functions usage\n\nBeside the functions to construct lists and access elements, convert between list and array, everything else is the same.\n\n```js\n[code]\nvar xs = ulCons(1, ulCons(2, lSingleton(3)));\n// xs = (1, (2, (3)))\n[/code]\n```\n\n```js\n[code]\nvar xs = lFromArray([1, 2, 3]);\nvar ys = lToArray(xs);\n// xs = (1, (2, (3)))\n// ys = [1, 2, 3]\n[/code]\n```\n\n```js\n[code]\nvar xs = lFromArray([1, 2]);\nvar h = lHead(xs);\nvar t = lTail(xs);\nvar error = lTail(t);\n// h = 1\n// t = (2)\n// error: lTail: Empty list\n[/code]\n```\n\n```js\n[code]\nvar xs = lCons(1)(lSingleton(2));\nvar x1, xs1;\nxs(x1, xs1);\nvar x2, xs2;\nxs1(x2, xs2);\n// xs = (1, (2))\n// x1 = 1\n// xs1 = (2)\n// x2 = 2\n// xs2 = null -- remember, we do not have the mean do define our own data types\n[/code]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshakadak%2Fleekwars-functional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshakadak%2Fleekwars-functional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshakadak%2Fleekwars-functional/lists"}