{"id":23991216,"url":"https://github.com/zaataylor/cartesian","last_synced_at":"2025-09-02T13:11:02.295Z","repository":{"id":64646145,"uuid":"556142058","full_name":"zaataylor/cartesian","owner":"zaataylor","description":"A Golang package for computing Cartesian products","archived":false,"fork":false,"pushed_at":"2022-10-28T05:32:57.000Z","size":26,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-25T04:43:52.894Z","etag":null,"topics":["combinatorics","go","golang","math"],"latest_commit_sha":null,"homepage":"","language":"Go","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/zaataylor.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}},"created_at":"2022-10-23T06:29:04.000Z","updated_at":"2023-02-19T17:15:03.000Z","dependencies_parsed_at":"2022-12-12T05:45:28.973Z","dependency_job_id":null,"html_url":"https://github.com/zaataylor/cartesian","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zaataylor/cartesian","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaataylor%2Fcartesian","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaataylor%2Fcartesian/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaataylor%2Fcartesian/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaataylor%2Fcartesian/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zaataylor","download_url":"https://codeload.github.com/zaataylor/cartesian/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zaataylor%2Fcartesian/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273287953,"owners_count":25078625,"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","status":"online","status_checked_at":"2025-09-02T02:00:09.530Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["combinatorics","go","golang","math"],"created_at":"2025-01-07T19:30:02.697Z","updated_at":"2025-09-02T13:11:02.266Z","avatar_url":"https://github.com/zaataylor.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ca href=\"https://project-types.github.io/#club\"\u003e\n\t\u003cimg src=\"https://img.shields.io/badge/project%20type-club-ff69b4\" alt=\"Club Badge\"\u003e\n\u003c/a\u003e\n\n# Table of Contents\n- [Description](#description)\n\t- [What is the Cartesian Product?](#what-is-the-cartesian-product)\n\t- [What is `cartesian`?](#what-is-cartesian)\n- [Requires](#requires)\n- [Example](#example)\n- [Using `cartesian`](#using-cartesian)\n\t- [Creating the `CartesianProduct`](#creating-the-cartesianproduct)\n\t- [Iterating over `CartesianProduct` Elements](#iterating-over-cartesianproduct-elements)\n\t- [Computing the full Cartesian product and using `Values()`](#computing-the-full-cartesian-product-and-using-values)\n\t- [When to use `Indices()` instead of `Values()`](#when-to-use-indices-instead-of-values)\n\n# Description\n\n## What is the Cartesian Product?\nGiven two sets of \"things\", `A` and `B`, the [Cartesian product](https://en.wikipedia.org/wiki/Cartesian_product) is the set of all possible pairs `(a, b)` where `a` is a thing from set `A` and `b` is a thing from set `B`. A real life example: let's say you have 3 shirts and 2 pairs of pants. What possible outfits can you create?\n\nCall the set of shirts `S`. Then, `S = {shirt-1, shirt-2, shirt-3}`.\nCall the set of pants `P`. Then `P = {pants-1, pants-2}`.\nHow many ways can we combine these items into an outfit (i.e. a shirt and pants)? Let's represent the possibilities as pairs of `(s, p)`, where `s` is in `S` and `p` is in `P`. Then we have:\n\n```\n(shirt-1, pants-1)\n(shirt-2, pants-1)\n(shirt-3, pants-1)\n(shirt-1, pants-2)\n(shirt-2, pants-2)\n(shirt-3, pants-2)\n```\n\nThis collection of ordered pairs is the Cartesian product of `S` and `P`.\n\n## What is `cartesian`?\n\n`cartesian` is a Go package that makes it easy to compute and return the Cartesian product of an arbitrary number of slices of varying types. \n\n# Requires\n- Go 1.18 or higher\n\n# Example\n\nConsider the following code:\n\n```golang\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/zaataylor/cartesian/cartesian\"\n)\n\ntype Job struct {\n\tname string\n\tid   int\n}\n\nfunc main() {\n\tsliceA := []int{1, 8}\n\tsliceB := []bool{true, false}\n\tsliceJ := []Job{\n\t\t{\n\t\t\tname: \"test job\",\n\t\t\tid:   1,\n\t\t},\n\t\t{\n\t\t\tname: \"another test job\",\n\t\t\tid:   2,\n\t\t},\n\t}\n\n\tslices := []any{sliceA, sliceB, sliceJ}\n\t// Construct Cartesian product of these slices\n\tcp := cartesian.NewCartesianProduct(slices)\n\tfmt.Printf(\"Cartesian product of slices:\\n%s\", cp)\n}\n```\n\nRunning this code returns:\n\n```\nCartesian product of slices:\n\n[\n  [1, true, {name:test job id:1}], \n  [1, true, {name:another test job id:2}], \n  [1, false, {name:test job id:1}], \n  [1, false, {name:another test job id:2}], \n  [8, true, {name:test job id:1}], \n  [8, true, {name:another test job id:2}], \n  [8, false, {name:test job id:1}], \n  [8, false, {name:another test job id:2}], \n]\n ```\n\n# Using `cartesian`\nThe sections below illustrate how to use `cartesian` effectively.\n\n## Creating the `CartesianProduct`\nPut the slices you want to compute the cartesian of inside of an `[]any`-type slice. Then, provide this slice as input to `NewCartesianProduct()`. For example:\n```golang\nsliceA := []int{4, 5, 8}\nsliceB := []bool{true, false}\ninput := []any{sliceA, sliceB}\n\ncp := cartesian.NewCartesianProduct(input)\n```\n\n## Iterating over `CartesianProduct` Elements\nFirst, use `Iterator()` to create a `CartesianProductIterator`. Then, use the iterator's `HasNext()` method as an indicator of when to continue iterating, and `Next()` to return the iterands themselves. If you want to iterate over indices instead, use `NextIndices()`. For example:\n```golang\n// Construct the Cartesian product\nsliceA := []int{4, 5, 8}\nsliceB := []bool{true, false}\ninput := []any{sliceA, sliceB}\ncp := cartesian.NewCartesianProduct(input)\n\n// Construct the Cartesian Product iterator\ncpi := cp.Iterator()\n\n\n// Iterate over its values and print them out\nfor cpi.HasNext() {\n\tvalue := cpi.Next()\n\tfmt.Printf(\"Value is: %v\\n\", element)\n}\n\n// Reset the iterator, if you'd like...\ncpi.ResetIterator()\n\n// Iterate over its indices and print them out\nfor cpi.HasNext() {\n\tindices := cpi.NextIndices()\n\tfmt.Printf(\"Indices are: %v\\n\", indices)\n}\n\n// ...or, create a new iterator\nnewCpi := cp.Iterator()\n```\n\n## Computing the full Cartesian product and using `Values()`\nWhen you first call `NewCartesianProduct()`, it computes the full Cartesian Product as part of its initialization process. You can then use `Values()` to print or iterate over the values of the product. Example:\n```golang\n// Construct the Cartesian product\nsliceA := []int{4, 5, 8}\nsliceB := []bool{true, false}\ninput := []any{sliceA, sliceB}\n\ncp := cartesian.NewCartesianProduct(input)\n\n// Iterate over its values and print them out\nfor _, v := range cp.Values() {\n    fmt.Printf(\"Value is: %#v\\n\", v)\n\t// Assign values to individual variables\n\tfirstValue, secondValue := v[0], v[1]\n\tfmt.Printf(\"First item: %v; Second item: %v\", firstValue, secondValue)\n}\n```\n\n## When to use `Indices()` instead of `Values()`\nThere are times when you might want/need to obtain indices into each of the passed-in slices, then directly index into each slice yourself to obtain the values corresponding to an element of the Cartesian product. In this case, it's best to use `Indices()`, as it will return a slice of `int`s where each element is an index into a specific slice. One use case for this is computing Cartesian products with a slice of functions and slices of args, then applying the args from the product to the functions:\n```golang\n// Is it stonks???\nfunc isStonksFunc(isStonks bool, company string) string {\n\tif isStonks {\n\t\treturn fmt.Sprintf(\"%s is stonks! 👍\", company)\n\t}\n\treturn fmt.Sprintf(\"%s is NOT stonks! 👎\", company)\n}\n\nfunc isOneFunc(isOne bool, _ string) string {\n\tif isOne {\n\t\treturn \"isOne\"\n\t}\n\treturn \"isZero\"\n}\n\nfunc main() {\n\tsliceB := []bool{true, false}\n\tsliceS := []string{\"MSFT\", \"GOOG\", \"META\"}\n\t// Function slice\n\tsliceF := make([]func(bool, string) string, 0)\n\tsliceF = append(sliceF, isStonksFunc)\n\tsliceF = append(sliceF, isOneFunc)\n\t\n\t// Construct Cartesian product\n\tanotherInput := []any{sliceF, sliceB, sliceS}\n\tcp := cartesian.NewCartesianProduct(anotherInput)\n\n\t// Explanation: Iterate over indices, and use them to obtain a specific\n\t// Cartesian product by indexing into each slice one by one.\n\t// Then, split the product into a function and two args, and apply those\n\t// args to the function, printing out the result.\n\tfor _, indexes := range cp.Indices() {\n\t\tfn, boolArg, stringArg := sliceF[indexes[0]], sliceB[indexes[1]], sliceS[indexes[2]]\n\t\tfmt.Printf(\"Function Name: %v, boolArg: %v, stringArg: %v --\u003e Result: %v\\n\", cartesian.GetFunctionName(fn), boolArg, stringArg, fn(boolArg, stringArg))\n\t}\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzaataylor%2Fcartesian","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzaataylor%2Fcartesian","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzaataylor%2Fcartesian/lists"}