From 5f2d60f70e5fb5400d6136cfc0f57108095ec826 Mon Sep 17 00:00:00 2001 From: Henrik Ilgen Date: Wed, 11 Dec 2024 10:54:12 +0100 Subject: [PATCH] Basic documentation and a test for :zip --- README.md | 14 ++++++++++++-- docs/index.md | 12 ++++++++++++ test/intermediate/zip.lua | 12 ++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/intermediate/zip.lua diff --git a/README.md b/README.md index aa1d666..599d501 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # lazylualinq -[![tests](https://github.com/Henkoglobin/lazylualinq/actions/workflows/test-and-publish.yml/badge.svg)](https://github.com/Henkoglobin/lazylualinq/actions/workflows/test-and-publish.yml) [![luarocks](https://img.shields.io/luarocks/v/henkoglobin/lazylualinq?style=plastic)](https://luarocks.org/modules/henkoglobin/lazylualinq) +[![tests](https://github.com/Henkoglobin/lazylualinq/actions/workflows/test-and-publish.yml/badge.svg)](https://github.com/Henkoglobin/lazylualinq/actions/workflows/test-and-publish.yml) [![luarocks](https://img.shields.io/luarocks/v/henkoglobin/lazylualinq?style=plastic)](https://luarocks.org/modules/henkoglobin/lazylualinq) [![pages-build-deployment](https://github.com/Henkoglobin/lazylualinq/actions/workflows/pages/pages-build-deployment/badge.svg?branch=main)](https://henkoglobin.github.io/lazylualinq/) LazyLuaLinq provides a simple, _lazy_ implementation of linq-like functions for Lua. With LazyLuaLinq, you can implement data transformation in elegant, expressive _queries_ akin to SQL: @@ -401,4 +401,14 @@ local linq = require("lazylualinq") setfenv(func, env) return func end) -``` \ No newline at end of file +``` + +# Dependencies and running tests + +In order to develop lazylualinq, you should install the test dependencies: + +```bash +sudo luarocks test --prepare +``` + +Then, you can run the tests using `luarocks test`. \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index b9362b7..253b423 100644 --- a/docs/index.md +++ b/docs/index.md @@ -238,6 +238,18 @@ local seq = linq { "a", "b", "c" }:take(2) ``` ## `linq:zip(other, resultSelector)` + +`zip` combines elements from the input and the `other` list, calling `resultSelector` for each pair of values. +It will only iterate the input sequences as long as the _shorter_ of both sequences yields values. + +`resultSelector` has the following signature: `function(leftValue, leftKey, rightValue, rightKey)`. + +```lua +local seq = linq { "foo", "egg", "hello" } + :zip(linq{ "bar", "spam" }, function(l, _, r) return l .. r end) +-- seq is now equivalent to linq { "foobar", "eggspam" } +``` + ## `linq:defaultIfEmpty(defaultValue, defaultIndex)` ## `linq:reindex()` ## `linq:nonNil()` diff --git a/test/intermediate/zip.lua b/test/intermediate/zip.lua new file mode 100644 index 0000000..5df985b --- /dev/null +++ b/test/intermediate/zip.lua @@ -0,0 +1,12 @@ +describe("intermediate operator #zip", function() + local linq = require("lazylualinq") + + it("only iterates until one sequence runs out of items", function() + local iterator = linq { "foo", "egg", "hello" } + :zip(linq{ "bar", "spam" }, function(l, _, r) return l .. r end) + :getIterator() + + assert.is_same({"foobar", 1}, { iterator() }) + assert.is_same({"eggspam", 2}, { iterator() }) + end) +end) \ No newline at end of file