{"id":21042918,"url":"https://github.com/aldanor/reactive-rs","last_synced_at":"2025-05-15T17:31:14.575Z","repository":{"id":62443752,"uuid":"155460236","full_name":"aldanor/reactive-rs","owner":"aldanor","description":"Streams and broadcasts: functional reactive programming in Rust.","archived":false,"fork":false,"pushed_at":"2018-11-12T00:57:33.000Z","size":8779,"stargazers_count":37,"open_issues_count":2,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-18T14:36:00.929Z","etag":null,"topics":["frp","reactive","rust","stream"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/aldanor.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":"2018-10-30T21:44:58.000Z","updated_at":"2025-01-02T23:01:01.000Z","dependencies_parsed_at":"2022-11-01T22:16:32.162Z","dependency_job_id":null,"html_url":"https://github.com/aldanor/reactive-rs","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldanor%2Freactive-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldanor%2Freactive-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldanor%2Freactive-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldanor%2Freactive-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aldanor","download_url":"https://codeload.github.com/aldanor/reactive-rs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254388071,"owners_count":22062982,"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":["frp","reactive","rust","stream"],"created_at":"2024-11-19T14:09:31.907Z","updated_at":"2025-05-15T17:31:14.252Z","avatar_url":"https://github.com/aldanor.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"reactive-rs\n===========\n\n[![Build](https://api.travis-ci.org/aldanor/reactive-rs.svg)](https://travis-ci.org/aldanor/reactive-rs)\n[![Crate](http://meritbadge.herokuapp.com/reactive-rs)](https://crates.io/crates/reactive-rs)\n[![Docs](https://docs.rs/reactive-rs/badge.svg)](https://docs.rs/reactive-rs)\n\nThis crate provides the building blocks for functional reactive programming (FRP)\nin Rust. It is inspired by\n[carboxyl](https://crates.io/crates/carboxyl),\n[frappe](https://crates.io/crates/frappe)\nand [bidule](https://crates.io/crates/bidule) crates, and\nvarious [ReactiveX](http://reactivex.io/) implementations.\n\n### Documentation\n\n[docs.rs/reactive-rs](https://docs.rs/reactive-rs)\n\n### Purpose\n\nThe main use case of this library is to simplify creating efficient\ncomputational DAGs (or computational trees, to be precise) that operate\non streams of values. It does not aim to replicate the entire galaxy of\nReactiveX operators, nor does it attempt to delve into\nfutures/concurrency territory.\n\nWhat is a computational tree? First, there's the root at the top, that's\nwhere the input values get fed into continuously. Then, we perform\ncomputations on these values – each of which may yield zero,\none or more values that are sent further down. Some downstream\nnodes may share their parents – for instance, `g(f(x))` and `h(f(x))`, where `x` is\nthe input and `f` is the intermediate transformation; in this case, we want\nto make sure we don't have to recompute `f(x)` twice. Moreover, this\nbeing Rust, we'd like to ensure we're not copying and cloning any values\nneedlessly, and we generally prefer things to be zero-cost/inlineable\nwhen possible. Finally, there are leaves – these are observers, functions\nthat receive transform values and do something with them, likely recording\nthem somewhere or mutating the environment in some other way.\n\n### Context\n\nStreams, broadcasts and observers in this crate operate on pairs of\nvalues: the *context* and the *element*. Context can be viewed as\noptional metadata attached to the original value. Closures required in\nmethods like `.map()` only take one argument (the element) and are\nexpected to return a single value; this way, the element can be changed\nwithout touching the context. This can be extremely convenient if you\nneed to access the original input value (or any \"upstream\" value) way\ndown the computation chain – this way you don't have to propagate\nit explicitly.\n\nMost stream/broadcast methods have an alternative \"full\" version that\noperates on both context/element, with `_ctx` suffix.\n\n### Usage example\n\nConsider the following problem: we have an incoming stream of\nbuy/sell price pairs, and for each incoming event we would like to\ncompute how the current mid-price (the average between the two)\ncompares relatively to the minimum buy price and the maximum sell\nprice over the last three observations. Moreover, we would like to\nskip the first few events in order to allow the buffer to fill up.\n\nHere's one way we could do it (not the most ultimately efficient\nway of solving this particular problem, but it serves quite well\nto demonstrate the basic functionality of the crate):\n\n```rust\nuse std::cell::Cell;\nuse std::f64;\nuse reactive_rs::*;\n\nlet min_rel = Cell::new(0.);\nlet max_rel = Cell::new(0.);\n\n// create a broadcast of (buy, sell) pairs\nlet quotes = SimpleBroadcast::new();\n\n// clone the broadcast so we can feed values to it later\nlet last = quotes.clone()\n    // save the mid-price for later use\n    .with_ctx_map(|_, \u0026(buy, sell)| (buy + sell) / 2.)\n    // cache the last three observations\n    .last_n(3)\n    // wait until the queue fills up\n    .filter(|quotes| quotes.len() \u003e 2)\n    // share the output (slices of values)\n    .broadcast();\n\n// subscribe to the stream of slices\nlet min = last.clone()\n    // compute min buy price\n    .map(|p| p.iter().map(|q| q.0).fold(1./0., f64::min));\n// subscribe to the stream of slices\nlet max = last.clone()\n    // compute max sell price\n    .map(|p| p.iter().map(|q| q.1).fold(-1./0., f64::max));\n\n// finally, attach observers\nmin.subscribe_ctx(|p, min| min_rel.set(min / p));\nmax.subscribe_ctx(|p, max| max_rel.set(max / p));\n\nquotes.send((100., 102.));\nquotes.send((101., 103.));\nassert_eq!((min_rel.get(), max_rel.get()), (0., 0.));\nquotes.send((99., 101.));\nassert_eq!((min_rel.get(), max_rel.get()), (0.99, 1.03));\nquotes.send((97., 103.));\nassert_eq!((min_rel.get(), max_rel.get()), (0.97, 1.03));\n```\n\n### License\n\nThe MIT License (MIT)\n\nCopyright (c) 2018 Ivan Smirnov\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faldanor%2Freactive-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faldanor%2Freactive-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faldanor%2Freactive-rs/lists"}