https://github.com/josevalim/nested-data-structure-traversal Skip to content Sign up Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Project management - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] [search-key] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up Sign up {{ message }} josevalim / nested-data-structure-traversal * Notifications * Star 443 * Fork 132 443 stars 132 forks Star Notifications * Code * Issues 0 * Pull requests 1 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights master Switch branches/tags [ ] Branches Tags Nothing to show {{ refName }} default View all branches Nothing to show {{ refName }} default View all tags 1 branch 0 tags Go to file Code Clone HTTPS GitHub CLI [https://github.com/j] Use Git or checkout with SVN using the web URL. [gh repo clone joseva] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio If nothing happens, download the GitHub extension for Visual Studio and try again. Go back Latest commit @nickjj nickjj Merge pull request #146 from danidiaz/lens-parts-of ... 355b1ce Apr 12, 2021 Merge pull request #146 from danidiaz/lens-parts-of A lens-based Haskell solution with the partsOf combinator. 355b1ce Git stats * 332 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github Remove extra space for HTML comment Apr 8, 2021 agda Used if_then_else_ properly Apr 9, 2021 apl Update APL solution: don't mutate reset, add explanations Apr 7, 2021 bloblang Add bloblang example Apr 12, 2021 c/for Rename C example from reduce to for Apr 8, 2021 clojure Merge pull request #142 from alex-appetiti/master Apr 12, 2021 common-lisp Add Common Lisp solution Apr 8, 2021 cpp Minor improve Apr 12, 2021 crochet Delete main.crochet Apr 7, 2021 crystal fix Crystal with map implementation Apr 8, 2021 csharp Rename for-each-linq.cs to immutable-linq.cs Apr 12, 2021 d insertPosition was using the JSON const value Apr 7, 2021 dart add Dart solution Apr 8, 2021 elixir Merge pull request #132 from cr0t/add-elixir-list-foldl Apr 12, 2021 elm Remove extra files Apr 7, 2021 erlang Add an erlang solution using recursion Apr 10, 2021 fsharp Clojure and F# solutions (#129) Apr 12, 2021 gdscript Create for.gd Apr 7, 2021 gleam Merge pull request #20 from lpil/gleam-optional Apr 7, 2021 go linting Apr 7, 2021 groovy Apache Groovy (JVM language) - the simplest 'for each' solution Apr 12, 2021 haskell A lens-based Haskell solution with the partsOf combinator. Apr 12, 2021 hoon Add Hoon solution Apr 12, 2021 java Rename to ForEachImmutable.java Apr 12, 2021 javascript Add JavaScript forEach solution Apr 12, 2021 jq jq implementation with reduce. Apr 9, 2021 julia Add Julia solution using foldl Apr 7, 2021 kotlin Fix small issues in Kotlin for-loop solution Apr 12, 2021 lua/reduce Move Lua reduce file into reduce/ directory Apr 7, 2021 nim Small refactor to make it more Nim Apr 7, 2021 ocaml Implement an ocaml variant based on tagless final Apr 8, 2021 octave Add an Octave solution Apr 12, 2021 perl add perl Apr 7, 2021 php PHP: Recursive with for loop Apr 8, 2021 powershell Added PowerShell version of code Apr 12, 2021 prolog/swi-prolog Prolog: adds basic SWI-Prolog solution (recursive approach) Apr 12, 2021 purescript Rename Main.purs to fold.purs Apr 8, 2021 python Merge pull request #47 from shaib/master Apr 7, 2021 q Adding kdb+ Q implementation Apr 8, 2021 raku Add a for loop solution for Raku Apr 12, 2021 ruby Add another Ruby solution Apr 9, 2021 rust Rename minimal.rs to for_in_anonymous_tuples.rs Apr 12, 2021 scala alternate scala implementation Apr 8, 2021 scheme Add a scheme solution Apr 8, 2021 sml make SML more readable Apr 12, 2021 spark Update pyspark-dataframe.py Apr 12, 2021 sql/postgres SQL solution, Postgres dialect, similar to gaps-and-islands principle Apr 9, 2021 swift Swift for in solution Apr 7, 2021 tcl Tcl: replace [dict getdef] with simple [dict get] Apr 12, 2021 typescript Simple solution with map Apr 8, 2021 v Rename of file per review suggestion Apr 12, 2021 vbscript changed lesson array names Apr 8, 2021 vimscript Create for-loops.vim Apr 12, 2021 wenyan feat: wenyan lang implementation using recursive solution Apr 8, 2021 zig Add zig implementation using std.json.Value Apr 8, 2021 README.md Link to Nick's Twitter account Apr 8, 2021 View code Traversing nested data-structures The problem Sample solution Contribute License README.md Traversing nested data-structures The goal of this repository is to share solutions to a common problem of traversing and annotating data-structures across a variety of programming languages. The problem The algorithm should receive a list of sections. A section is a key-value data structure, with a "title", a "reset_lesson_position" boolean, and a list of "lessons". A lesson is a key-value data structure with the "name" field. Your job is to traverse the list of sections, adding a position (starting from 1) to each section, and traverse the list of lessons adding a position (starting from 1) to each lesson. Note, however, the lessons position is shared across sections. The lesson position should also be reset if "reset_lesson_position" is true. Here is an example input (formatted in JSON for convenience): [ { "title": "Getting started", "reset_lesson_position": false, "lessons": [ {"name": "Welcome"}, {"name": "Installation"} ] }, { "title": "Basic operator", "reset_lesson_position": false, "lessons": [ {"name": "Addition / Subtraction"}, {"name": "Multiplication / Division"} ] }, { "title": "Advanced topics", "reset_lesson_position": true, "lessons": [ {"name": "Mutability"}, {"name": "Immutability"} ] } ] The output should be (formatted in JSON for convenience): [ { "title": "Getting started", "reset_lesson_position": false, "position": 1, "lessons": [ {"name": "Welcome", "position": 1}, {"name": "Installation", "position": 2}, ] }, { "title": "Basic operator", "reset_lesson_position": false, "position": 2, "lessons": [ {"name": "Addition / Subtraction", "position": 3}, {"name": "Multiplication / Division", "position": 4} ] }, { "title": "Advanced topics", "reset_lesson_position": true, "position": 3, "lessons": [ {"name": "Mutability", "position": 1}, {"name": "Immutability", "position": 2} ] } ] Sample solution Here is one way to solve it in Python: sections = ... # the data from above as Python data structure ellided for convenience section_counter = 1 lesson_counter = 1 for section in sections: if section['reset_lesson_position']: lesson_counter = 1 section['position'] = section_counter section_counter += 1 for lesson in section['lessons']: lesson['position'] = lesson_counter lesson_counter += 1 print(sections) Thanks to @nickjanetakis for the description of the problem and for contributing this Python solution. Contribute New solutions to the problem are welcome. In order to contribute: * Make sure there are no entries for your programming language of choice * If there are existing entries, make sure your proposed solution is considerably distinct For example, avoid new entries that are small variations of existing solutions. Solutions that use different approaches, such as mutability vs immutability, single-pass vs chunking, etc are all welcome though. Once your solution is ready, please send a pull request. The solution should inside a directory named after the programming language and be a single file named after the approach taken. For example, the Python solution above is placed at: python/for-in.py If your solution requires more than 1 file, then you can include all of them inside a directory such as python/for-in/example.py. However, there is no need to include project setup files. It is important the solutions are considered readable and idiomatic. The goal is to focus on readability rather than performance, code golfing, etc. All code in this repository should be placed in the public domain. Thank you for the time and for sharing a solution! License All code in this repository is in the public domain. About No description, website, or topics provided. Resources Readme Releases No releases published Packages 0 No packages published Contributors 96 * @nickjj * @josevalim * @rarous * @meox * @sthagen * @spacejam * @IceDragon200 * @Janiczek * @formigarafa * @ianprogrammer * @jfrolich + 85 contributors Languages * Clojure 9.5% * Elixir 8.3% * Haskell 7.8% * Ruby 6.6% * Java 6.1% * OCaml 5.2% * Other 56.5% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.