{"id":19345932,"url":"https://github.com/tolitius/multim","last_synced_at":"2025-04-23T04:36:37.301Z","repository":{"id":62434932,"uuid":"47798755","full_name":"tolitius/multim","owner":"tolitius","description":"oh.. these tasty guava multi maps","archived":false,"fork":false,"pushed_at":"2022-02-22T17:08:17.000Z","size":12,"stargazers_count":25,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T08:23:15.249Z","etag":null,"topics":["clojure","guava","timeseries"],"latest_commit_sha":null,"homepage":null,"language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tolitius.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2015-12-11T01:55:27.000Z","updated_at":"2022-02-22T17:08:21.000Z","dependencies_parsed_at":"2022-11-01T21:02:40.233Z","dependency_job_id":null,"html_url":"https://github.com/tolitius/multim","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/tolitius%2Fmultim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tolitius%2Fmultim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tolitius%2Fmultim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tolitius%2Fmultim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tolitius","download_url":"https://codeload.github.com/tolitius/multim/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250372505,"owners_count":21419719,"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":["clojure","guava","timeseries"],"created_at":"2024-11-10T04:08:23.113Z","updated_at":"2025-04-23T04:36:36.933Z","avatar_url":"https://github.com/tolitius.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# multim\n\nwhere a lonely key meets multiple values\n\n[![Clojars Project](http://clojars.org/tolitius/multim/latest-version.svg)](http://clojars.org/tolitius/multim)\n\n- [Why](#why)\n- [Time Series](#time-series)\n- [Multi Mode](#multi-mode)\n  - [Time Slicing](#time-slicing)\n  - [View with a View](#view-with-a-view)\n- [License](#license)\n\n## Why\n\nThey come handy. Sometimes. And at those times it's good to have them.\n\n## Time Series\n\nLet's say we have events streaming in in a form of `{timestamp {ticker event-id}}`:\n\n```clojure\n(def events [\n  [1449088877203 {:ticker :GOOG :event-id 1}]\n  [1449088876590 {:ticker :AAPL :event-id 2}]\n  [1449088877601 {:ticker :MSFT :event-id 3}]\n  [1449088877203 {:ticker :TSLA :event-id 4}]\n  [1449088875914 {:ticker :NFLX :event-id 5}]\n  [1449088870005 {:ticker :FB   :event-id 6}] ])\n```\n\n* we'd like to keep them in a map.\n* we'd also like to keep them _sorted_ by time (i.e. timestamp)\n\n_notice_ that Tesla and Google have _the same timestamp_ (i.e. same key value).\n\n## Multi Mode\n\nAs events come in they can be added into something like a [TreeMultimap](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/TreeMultimap.html)\nwhich is both: sorted and multimap.\n\n```clojure\n;; syntax: (tree-multimap [key-comparator] [value-comparator])\n\nuser=\u003e (tree-multimap \u003c)\n\n#object[com.google.common.collect.TreeMultimap 0x1fabbda8 \"{}\"]\n```\n\na map with no data is interesting, but not as much as a map with the data:\n\n```clojure\nuser=\u003e (def mm (into-multi \n                 (tree-multimap \u003c) events))\n\n#object[com.google.common.collect.TreeMultimap 0x688a6108\n\"{1449088877601=[{:ticker :MSFT, :event-id 3}], \n  1449088877203=[{:ticker :GOOG, :event-id 1}, {:ticker :TSLA, :event-id 4}],\n  1449088876590=[{:ticker :AAPL, :event-id 2}],\n  1449088875914=[{:ticker :NFLX, :event-id 5}],\n  1449088870005=[{:ticker :FB, :event-id 6}]}\"]\n```\n\n_notice_ how it groupped values for the `1449088877203` timestamp.\n\n### Time Slicing\n\nSince the map is sorted, it should be quite simple to find all the entries before or after certain time. \n\n##### before\n```clojure\nuser=\u003e (to mm 1449088876592)\n\n{1449088870005 #{{:ticker :FB, :event-id 6}}, \n 1449088875914 #{{:ticker :NFLX, :event-id 5}}, \n 1449088876590 #{{:ticker :AAPL, :event-id 2}}}\n```\n\n##### after\n```clojure\nuser=\u003e (from mm 1449088876592)\n\n{1449088877203 #{{:ticker :GOOG, :event-id 1} {:ticker :TSLA, :event-id 4}}, \n 1449088877601 #{{:ticker :MSFT, :event-id 3}}}\n```\n\n### View with a View\n\nWhile `TreeMultimap` has all the chops, it is mutable, hence it is better to create a navigatable view based on the same `tree-multimap`:\n\n```clojure\nuser=\u003e (def view (into-view \n                   (tree-multimap \u003c) events))\n#'user/view\n\nuser=\u003e (type view)\ncom.google.common.collect.AbstractMapBasedMultimap$NavigableAsMap\n```\n\nit would of course be boring if this view type was not extended with a `Sliceable` protocol (as the `TreeMultimap` above):\n\n```clojure\n(defprotocol Sliceable \n  (from [this k])\n  (to [this k]))\n```\n\nso it does extend it as well:\n\n##### before\n```clojure\nuser=\u003e (to view 1449088876592)\n\n{1449088870005 #{{:ticker :FB, :event-id 6}}, \n 1449088875914 #{{:ticker :NFLX, :event-id 5}}, \n 1449088876590 #{{:ticker :AAPL, :event-id 2}}}\n```\n\n##### after\n```clojure\nuser=\u003e (from view 1449088876592)\n\n{1449088877203 #{{:ticker :GOOG, :event-id 1} {:ticker :TSLA, :event-id 4}}, \n 1449088877601 #{{:ticker :MSFT, :event-id 3}}}\n```\n\n## License\n\nCopyright © 2022 tolitius\n\nDistributed under the Eclipse Public License either version 1.0 or (at\nyour option) any later version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftolitius%2Fmultim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftolitius%2Fmultim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftolitius%2Fmultim/lists"}