├── .gitignore ├── .travis.yml ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md └── src ├── applications.rs ├── lib.rs └── transducers.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | 3 | rust: 4 | - stable 5 | - beta 6 | - nightly -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rs_transducers" 3 | version = "0.0.4" 4 | authors = ["Ben Ashford"] 5 | license = "MIT/Apache-2.0" 6 | repository = "https://github.com/benashford/rs-transducers" 7 | description = "Implementation of transducers for Rust" 8 | readme = "README.md" 9 | 10 | [dependencies] 11 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright 2016 rust-lazysort developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `rs-transducers` 2 | 3 | [![Build Status](https://travis-ci.org/benashford/rs-transducers.svg?branch=master)](https://travis-ci.org/benashford/rs-transducers) 4 | [![](http://meritbadge.herokuapp.com/rs_transducers)](https://crates.io/crates/rs_transducers) 5 | 6 | An attempt at implementing Clojure style transducers in Rust. 7 | 8 | ## What is a transducer? 9 | 10 | When first introduced into Clojure, the concept of transducers caused a [lot of confusion](https://news.ycombinator.com/item?id=8143905). The best overview is part of the [Clojure reference](http://clojure.org/reference/transducers). 11 | 12 | Essentially a transducer separates the application of functions on data from the structure of the data. For example the higher-order functions like `map` can be expressed in such a way that could be applied to a vector, but also an iterator, but also a channel containing data passed between threads. 13 | 14 | This library contains two parts: 15 | 16 | 1. A collection of frequently occurring transducers. 17 | 2. Implementation of applications of those transducers. 18 | 19 | In both cases these collections can be extended. Custom transducers can be defined, and transducers can be applied to any custom data structure or stream. 20 | 21 | WARNING: as a result of the simplification, there is potentially some confused terminology. At this early stage of development, I'm happy to correct these even if it involves renaming significant parts of the library. 22 | 23 | ## Transducers 24 | 25 | An example of a transducer to filter odd numbers: 26 | 27 | ```rust 28 | extern crate rs_transducers; 29 | 30 | use rs_transducers::transducers; 31 | use rs_transducers::applications::vec::Into; 32 | 33 | let source = vec![1, 2, 3, 4, 5]; 34 | let transducer = transducers::filter(|x| x % 2 == 0); 35 | println!(source.transduce_into(transducer)); 36 | ``` 37 | 38 | This will print: `[2, 4]`. 39 | 40 | Transducers can be composed, so complex map/filter/etc. operations can be expressed simply. 41 | 42 | ```rust 43 | let transducer = rs_transducers::compose(transducers::drop(5), 44 | transducers::filter(|x| x % 2 == 0)); 45 | ``` 46 | 47 | ### Provided transducers 48 | 49 | `map` - takes a function of type `Fn(I) -> O` and returns a `MapTransducer` that implements `Transducer`. Also `map_indexed` which takes a function of type `Fn(usize, I) -> O`. 50 | 51 | `mapcat` - takes a function of type `Fn(I) -> OI` where `OI` implementes `IntoIterator` and returns a `MapcatTransducer` that implements `Transducer`. 52 | 53 | `filter` and `remove` - takes a function of type `Fn(I) -> bool` and returns a `FilterTransducer` that implements a `Transducer`. `filter` will retain those that match the condition, `remove` is the opposite. 54 | 55 | `keep` - takes a function of type `Fn(I) -> Option` and returns a `KeepTransducer` that produces all `O`. Also `keep_indexed` which takes a function of type `Fn(usize, I) -> Option`. 56 | 57 | `partition` and `partition_all` - takes a `usize` determining the size of each partition and returns a `PartitionTransducer` that implements `Transducer>`. The difference between the two is that `partition_all` will return the final partition incomplete, where `partition` will not. Also `partition_by` that groups data together as long as the provided function returns the same value. 58 | 59 | `take` and `drop` - takes a `usize` and return a transducer that implements `Transducer` that takes or drops the appropriate number of elements. 60 | 61 | `take_while` and `drop_while` - take or drop values while the predicate remains true. 62 | 63 | `replace` - takes a `HashMap` (where `T` must implement `Clone`) and returns a `ReplaceTransducer` which will replace each instance of a given key with a clone of the corresponding value. 64 | 65 | `interpose` - takes a cloneable value `T` and returns a transducer which, when applied, interposes that value with each value that goes through the reducing function. 66 | 67 | `dedupe` - removes consecutive duplicates. 68 | 69 | The only `clojure.core` transducer not implemented here is `random-sample`, this is due to me trying to avoid all dependencies from this package. It is trivial to implement such a transducer in any application however. 70 | 71 | ### Implementing transducers 72 | 73 | The initial version of this library attempted to simpify what a transducer was by trying to factor out the need for a "reducing function" (please see the Clojure documentation for definition of these terms). By not having such a function then we didn't really have a transducer, just something that could be used for similar ends. But it soon became apparent that both reducing functions and transducers will be needed; the reason for this is it is the only way certain transducers (e.g. `mapcat`) can be applied to certain things (e.g. channels). 74 | 75 | Implementing a transducer, therefore requires implementing two traits `Transducer` for the transducer itself and `Reducing` for the reducing function returned by the transducer when applied to the previous reducing function. 76 | 77 | #### `Transducer` 78 | 79 | A `Transducer` implemented one function `new` which takes one `Reducing` function and returns another one. 80 | 81 | #### `Reducing` 82 | 83 | The `Reducing` trait requires three functions to be implemented: 84 | 85 | `init` (this may be removed in future versions as it currently is not needed) - any underlying `Reducing` function must have its `init` function called. 86 | 87 | `step` - this is called for each value passing through. Each call can call the underlying `step` function zero, one or many times. The result is `Result`, implementations must ensure the result of any underlying `step` is propagated appropriately. `StepResult` is an enum with two options `Continue` and `Stop` which can be used to terminate the reduction process early (for example see `take`). 88 | 89 | `complete` - a transducer can be stateful (e.g. `partition_all`), calling this function ensures that any such state is flushed at the end of the process. Implementations can call `step` on the underlying `Reducing` function as often as required, and must complete by calling `complete` on the underlying `Reducing`. 90 | 91 | ## Applications 92 | 93 | Transducers need to be applied to a source of data to have an effect. The initial example used the `Into` trait to add `transduce_into` to vectors; as the name suggests, this is analogous to `into_iter()` in that it consumes the original data, applies the transducer and returns a new vector. 94 | 95 | ### Provided applications 96 | 97 | Implemented so far are transducer applications for: 98 | 99 | #### `Vec` 100 | 101 | This comes in two forms `Into` that adds a `transduce_into` to vectors, this consumes the original vector; and the `Ref` trait that adds `transduce_ref` to vectors, this leaves the original vector unchanged and returns a new one based on feeding references to the source data through the transducer. 102 | 103 | #### `Iterator` 104 | 105 | The trait `TransduceIter` adds a `transduce` to iterators which returns a new iterator. 106 | 107 | #### Channels 108 | 109 | Unlike operations solely defined on iterators, transducers can be applied to any sequence of data, including streams of data through channels between threads. 110 | 111 | One compromise is necessary since Rust's channels are concrete `Sender` and `Receiver` types, not implementing any traits, we cannot implement one of these channels (not without creating two pairs of channels, but that would need an additional thread to pipe messages between them). Instead we wrap the `Sender` type with a new `TransducingSender`. 112 | 113 | For example (from the tests): 114 | 115 | ```rust 116 | let transducer = super::compose(transducers::partition_all(6), 117 | transducers::filter(|x| x % 2 == 0)); 118 | let (mut tx, rx) = transducing_channel(transducer); 119 | thread::spawn(move|| { 120 | for i in 0..10 { 121 | tx.send(i).unwrap(); 122 | } 123 | tx.close().unwrap(); 124 | }); 125 | assert_eq!(vec![0, 2, 4, 6, 8], rx.recv().unwrap()); 126 | ``` 127 | 128 | ### Implementing applications 129 | 130 | Any custom data-structure/channel/sequence/etc. can apply a transducer. 131 | 132 | In order to do this an implementation of `Reducing` needs to be provided, to build the required data-structure. Then: 133 | 134 | 1. By passing this to the `new` function of a transducer a new reducing function is returned. 135 | 2. Call `init` on the reducing function. 136 | 3. For each piece of data call `step`. Taking into account the result, stopping early if `StepResult::Stop` is returned. 137 | 4. Finally call `complete`. 138 | 139 | It is the responsibility of the implementation to retain access to the constructed data structure. 140 | 141 | ## Differences between `rs-transducers` and Clojure's transducers 142 | 143 | Two of the biggest differences are: 144 | 145 | 1. Transducers are not reusable, they're fully consumed when producing the final `Reducing` function. This was done to make lifecycles simpler. 146 | 147 | 2. A limited set of application functions. 148 | 149 | ## License 150 | 151 | Licensed under either of 152 | 153 | * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 154 | * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 155 | 156 | at your option. 157 | 158 | ### Contribution 159 | 160 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. 161 | -------------------------------------------------------------------------------- /src/applications.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 rs-transducers developers 3 | * 4 | * Licensed under the Apache License, Version 2.0 or the MIT license 6 | * , at your 7 | * option. This file may not be copied, modified, or distributed 8 | * except according to those terms. 9 | */ 10 | 11 | pub mod vec { 12 | use std::cell::RefCell; 13 | use std::rc::Rc; 14 | 15 | use ::{Transducer, Reducing, StepResult}; 16 | 17 | pub trait Ref { 18 | type Input; 19 | 20 | fn transduce_ref<'a, T, O, RO, E>(&'a self, transducer: T) -> Result, E> 21 | where RO: Reducing<&'a Self::Input, Vec, E>, 22 | T: Transducer, RO=RO>; 23 | } 24 | 25 | pub trait Into { 26 | type Input; 27 | 28 | fn transduce_into(self, transducer: T) -> Result, E> 29 | where RO: Reducing, E>, 30 | T: Transducer, RO=RO>; 31 | } 32 | 33 | pub struct VecReducer(Rc>>); 34 | 35 | impl<'a, O> Reducing, ()> for VecReducer { 36 | type Item = O; 37 | 38 | #[inline] 39 | fn step(&mut self, value: O) -> Result { 40 | self.0.borrow_mut().push(value); 41 | Ok(StepResult::Continue) 42 | } 43 | 44 | fn complete(&mut self) -> Result<(), ()> { 45 | Ok(()) 46 | } 47 | } 48 | 49 | impl Ref for Vec { 50 | type Input = X; 51 | 52 | fn transduce_ref<'a, T, O, RO, E>(&'a self, transducer: T) -> Result, E> 53 | where RO: Reducing<&'a Self::Input, Vec, E>, 54 | T: Transducer, RO=RO> { 55 | let res = Rc::new(RefCell::new(Vec::with_capacity(self.len()))); 56 | { 57 | let rr = VecReducer(res.clone()); 58 | let mut reducing = transducer.new(rr); 59 | reducing.init(); 60 | for val in self.iter() { 61 | match reducing.step(val) { 62 | Ok(StepResult::Continue) => (), 63 | Ok(StepResult::Stop) => break, 64 | Err(e) => return Err(e) 65 | } 66 | } 67 | try!(reducing.complete()) 68 | } 69 | Ok(match Rc::try_unwrap(res) { 70 | Ok(res) => res.into_inner(), 71 | Err(_) => panic!("Other refs") 72 | }) 73 | } 74 | } 75 | 76 | impl Into for Vec { 77 | type Input = X; 78 | 79 | fn transduce_into(self, transducer: T) -> Result, E> 80 | where RO: Reducing, E>, 81 | T: Transducer, RO=RO> { 82 | let res = Rc::new(RefCell::new(Vec::with_capacity(self.len()))); 83 | { 84 | let rr = VecReducer(res.clone()); 85 | let mut reducing = transducer.new(rr); 86 | reducing.init(); 87 | for val in self.into_iter() { 88 | match reducing.step(val) { 89 | Ok(StepResult::Continue) => (), 90 | Ok(StepResult::Stop) => break, 91 | Err(e) => return Err(e) 92 | } 93 | } 94 | try!(reducing.complete()) 95 | } 96 | Ok(match Rc::try_unwrap(res) { 97 | Ok(res) => res.into_inner(), 98 | Err(_) => panic!("Other refs") 99 | }) 100 | } 101 | } 102 | } 103 | 104 | pub mod iter { 105 | use std::cell::RefCell; 106 | use std::collections::VecDeque; 107 | use std::rc::Rc; 108 | 109 | use ::{Transducer, Reducing, StepResult}; 110 | 111 | pub trait TransduceIter { 112 | type UnderlyingIterator; 113 | type Item; 114 | 115 | fn transduce(self, transducer: T) -> TransduceIterator 116 | where RO: Reducing, 117 | T: Transducer, RO=RO>; 118 | } 119 | 120 | impl TransduceIter for I 121 | where I: Iterator { 122 | 123 | type UnderlyingIterator = I; 124 | type Item = T; 125 | 126 | fn transduce(self, transducer: TR) -> TransduceIterator 127 | where RO: Reducing, 128 | TR: Transducer, RO=RO> { 129 | let buffer = Rc::new(RefCell::new(VecDeque::new())); 130 | 131 | TransduceIterator { 132 | underlying: self, 133 | buffer: buffer.clone(), 134 | rf: transducer.new(IterReducer(buffer.clone())), 135 | runoff: false 136 | } 137 | } 138 | } 139 | 140 | pub struct IterReducer(Rc>>); 141 | 142 | impl Reducing for IterReducer { 143 | type Item = T; 144 | 145 | #[inline] 146 | fn step(&mut self, value: T) -> Result { 147 | self.0.borrow_mut().push_back(value); 148 | Ok(StepResult::Continue) 149 | } 150 | 151 | fn complete(&mut self) -> Result<(), ()> { 152 | Ok(()) 153 | } 154 | } 155 | 156 | pub struct TransduceIterator { 157 | underlying: I, 158 | buffer: Rc>>, 159 | rf: RF, 160 | runoff: bool 161 | } 162 | 163 | impl Iterator for TransduceIterator 164 | where I: Iterator, 165 | RF: Reducing { 166 | 167 | type Item = O; 168 | 169 | #[inline] 170 | fn next(&mut self) -> Option { 171 | loop { 172 | if !self.runoff && self.buffer.borrow().is_empty() { 173 | match self.underlying.next() { 174 | None => { 175 | self.runoff = true; 176 | self.rf.complete().unwrap(); 177 | }, 178 | Some(value) => { 179 | match self.rf.step(value) { 180 | Ok(StepResult::Continue) => (), 181 | Ok(StepResult::Stop) => { 182 | self.runoff = true; 183 | self.rf.complete().unwrap(); 184 | }, 185 | Err(_) => unreachable!() 186 | } 187 | } 188 | } 189 | } 190 | if self.runoff && self.buffer.borrow().is_empty() { 191 | return None 192 | } 193 | match self.buffer.borrow_mut().pop_front() { 194 | None => (), 195 | Some(value) => return Some(value) 196 | } 197 | } 198 | } 199 | } 200 | } 201 | 202 | pub mod channels { 203 | use std::marker::PhantomData; 204 | use std::sync::mpsc::{Receiver, Sender, SendError, channel}; 205 | 206 | use ::{Transducer, Reducing, StepResult}; 207 | 208 | pub struct TransducingSender 209 | where SR: Reducing> { 210 | 211 | rf: SR, 212 | o_type: PhantomData 213 | } 214 | 215 | pub struct SenderReducer(Sender); 216 | 217 | impl Reducing> for SenderReducer { 218 | type Item = O; 219 | 220 | #[inline] 221 | fn step(&mut self, value: O) -> Result> { 222 | match self.0.send(value) { 223 | Ok(_) => Ok(StepResult::Continue), 224 | Err(e) => Err(e) 225 | } 226 | } 227 | 228 | fn complete(&mut self) -> Result<(), SendError> { 229 | Ok(()) 230 | } 231 | } 232 | 233 | impl TransducingSender 234 | where SR: Reducing> { 235 | 236 | pub fn send(&mut self, f: O) -> Result> { 237 | match self.rf.step(f) { 238 | Ok(StepResult::Continue) => Ok(true), 239 | Ok(StepResult::Stop) => Ok(false), 240 | Err(e) => Err(e) 241 | } 242 | } 243 | 244 | pub fn close(&mut self) -> Result<(), SendError> { 245 | self.rf.complete() 246 | } 247 | } 248 | 249 | pub fn transducing_channel(transducer: T) -> (TransducingSender, 250 | Receiver) 251 | where RO: Reducing, Item=O>, 252 | T: Transducer, RO=RO> { 253 | let (tx, rx) = channel(); 254 | let sender = TransducingSender { 255 | rf: transducer.new(SenderReducer(tx)), 256 | o_type: PhantomData 257 | }; 258 | (sender, rx) 259 | } 260 | } 261 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 rs-transducers developers 3 | * 4 | * Licensed under the Apache License, Version 2.0 or the MIT license 6 | * , at your 7 | * option. This file may not be copied, modified, or distributed 8 | * except according to those terms. 9 | */ 10 | pub mod transducers; 11 | pub mod applications; 12 | 13 | pub enum StepResult { 14 | Continue, 15 | Stop 16 | } 17 | 18 | /// Defines a reducing function from I to O with step errors of E 19 | pub trait Reducing { 20 | /// The type of each value after the reducing function 21 | type Item; 22 | 23 | /// Transducers must call the underlying `init` 24 | /// TODO: may not be required at all. Not currently used by any implementation 25 | fn init(&mut self) {} 26 | 27 | /// Each step, may fail 28 | /// TODO: the return type to contain an indicator of early termination 29 | fn step(&mut self, value: I) -> Result; 30 | 31 | /// Transducers must call the underlying `complete` 32 | fn complete(&mut self) -> Result<(), E>; 33 | } 34 | 35 | /// Defines a transducer that transforms a reducing function RI into 36 | /// a reducing function RO 37 | pub trait Transducer { 38 | type RO; 39 | fn new(self, reducing_fn: RI) -> Self::RO; 40 | } 41 | 42 | /// Composed transducers 43 | pub struct ComposedTransducer { 44 | a: AT, 45 | b: BT 46 | } 47 | 48 | impl Transducer for ComposedTransducer 49 | where AT: Transducer, 50 | BT: Transducer { 51 | type RO = RO; 52 | 53 | fn new(self, reducing_fn: RI) -> Self::RO { 54 | self.b.new(self.a.new(reducing_fn)) 55 | } 56 | } 57 | 58 | pub fn compose(a: AT, b: BT) -> ComposedTransducer { 59 | ComposedTransducer { 60 | a: a, 61 | b: b 62 | } 63 | } 64 | 65 | #[cfg(test)] 66 | mod test { 67 | use std::collections::HashMap; 68 | use std::thread; 69 | 70 | use super::transducers; 71 | use super::applications::vec::{Into, Ref}; 72 | use super::applications::iter::TransduceIter; 73 | use super::applications::channels::transducing_channel; 74 | 75 | #[test] 76 | fn test_vec_ref() { 77 | let source = vec![1, 2, 3]; 78 | let transducer = transducers::map(|x| x + 1); 79 | let result = source.transduce_ref(transducer).unwrap(); 80 | assert_eq!(vec![2, 3, 4], result); 81 | } 82 | 83 | /// A trivial function to test function references 84 | fn duplicator(i: isize) -> Vec { 85 | vec![i, i] 86 | } 87 | 88 | #[test] 89 | fn test_compose() { 90 | let source = vec![1, 2, 3]; 91 | let ta = transducers::mapcat(duplicator); 92 | let tb = transducers::map(|x| x * 2); 93 | let transducer = super::compose(tb, ta); 94 | let result = source.transduce_into(transducer).unwrap(); 95 | assert_eq!(vec![2, 2, 4, 4, 6, 6], result); 96 | } 97 | 98 | #[test] 99 | fn test_iterator() { 100 | let source = vec![1, 2, 3]; 101 | let transducer = transducers::mapcat(duplicator); 102 | let result:Vec = source.into_iter().transduce(transducer).collect(); 103 | assert_eq!(vec![1, 1, 2, 2, 3, 3], result); 104 | } 105 | 106 | #[test] 107 | fn test_filter() { 108 | { 109 | let source = vec![1, 2, 3, 4, 5]; 110 | let transducer = transducers::filter(|x| x % 2 == 0); 111 | let result = source.transduce_into(transducer).unwrap(); 112 | assert_eq!(vec![2, 4], result); 113 | } 114 | { 115 | let source = vec![1, 2, 3, 4, 5]; 116 | let transducer = transducers::remove(|x| x % 2 == 0); 117 | let result = source.transduce_into(transducer).unwrap(); 118 | assert_eq!(vec![1, 3, 5], result); 119 | } 120 | } 121 | 122 | #[test] 123 | fn test_partition() { 124 | { 125 | let source = vec![1, 2, 3, 4, 5, 6, 7]; 126 | let transducer = transducers::partition(2); 127 | let result = source.transduce_into(transducer).unwrap(); 128 | let expected_result:Vec> = vec![vec![1, 2], vec![3, 4], vec![5, 6]]; 129 | assert_eq!(expected_result, result); 130 | } 131 | { 132 | let source = vec![1, 2, 3, 4, 5, 6, 7]; 133 | let transducer = transducers::partition_all(2); 134 | let result = source.transduce_into(transducer).unwrap(); 135 | let expected_result:Vec> = vec![vec![1, 2], vec![3, 4], vec![5, 6], vec![7]]; 136 | assert_eq!(expected_result, result); 137 | } 138 | } 139 | 140 | #[test] 141 | fn test_take() { 142 | let source = vec![1, 2, 3, 4, 5, 6, 7]; 143 | let transducer = transducers::take(5); 144 | let result = source.transduce_into(transducer).unwrap(); 145 | assert_eq!(vec![1, 2, 3, 4, 5], result); 146 | 147 | let source2 = vec![1, 2, 3, 4, 5, 6, 7]; 148 | let transducer2 = super::compose(transducers::take(2), 149 | transducers::filter(|x| x % 2 == 0)); 150 | let result = source2.transduce_into(transducer2).unwrap(); 151 | assert_eq!(vec![2, 4], result); 152 | } 153 | 154 | #[test] 155 | fn test_drop() { 156 | let source = vec![1, 2, 3, 4, 5, 6, 7]; 157 | let transducer = transducers::drop(2); 158 | let result = source.transduce_into(transducer).unwrap(); 159 | assert_eq!(vec![3, 4, 5, 6, 7], result); 160 | } 161 | 162 | #[test] 163 | fn test_take_while() { 164 | let source = vec![1, 2, 3, 4, 5, 6, 7]; 165 | let transducer = transducers::take_while(|x| *x <= 5); 166 | let result = source.transduce_into(transducer).unwrap(); 167 | assert_eq!(vec![1, 2, 3, 4, 5], result); 168 | } 169 | 170 | #[test] 171 | fn test_drop_while() { 172 | let source = vec![1, 2, 3, 4, 5, 6, 7]; 173 | let transducer = transducers::drop_while(|x| *x <= 5); 174 | let result = source.transduce_into(transducer).unwrap(); 175 | assert_eq!(vec![6, 7], result); 176 | } 177 | 178 | #[test] 179 | fn test_replace() { 180 | let source = vec![1, 2, 3, 1, 2, 3, 1, 2, 3]; 181 | let mut replacements = HashMap::new(); 182 | replacements.insert(1, 4); 183 | replacements.insert(3, 7); 184 | let transducer = transducers::replace(replacements); 185 | let result = source.transduce_into(transducer).unwrap(); 186 | assert_eq!(vec![4, 2, 7, 4, 2, 7, 4, 2, 7], result); 187 | } 188 | 189 | #[test] 190 | fn test_partition_by() { 191 | let source = vec![1, 2, 3, 4, 5, 6]; 192 | let transducer = transducers::partition_by(|&x| x > 3); 193 | let result = source.transduce_into(transducer).unwrap(); 194 | let expected_result:Vec> = vec![vec![1, 2, 3], vec![4, 5, 6]]; 195 | assert_eq!(expected_result, result); 196 | } 197 | 198 | #[test] 199 | fn test_keep() { 200 | let source = vec![1, 2, 3, 4, 5, 6]; 201 | let transducer = transducers::keep(|x| { 202 | if x % 2 == 0 { 203 | Some(x) 204 | } else { 205 | None 206 | } 207 | }); 208 | let result = source.transduce_into(transducer).unwrap(); 209 | let expected_result = vec![2, 4, 6]; 210 | assert_eq!(expected_result, result); 211 | } 212 | 213 | #[test] 214 | fn test_keep_indexed() { 215 | let source = vec![6, 5, 4, 3, 2, 1]; 216 | let transducer = transducers::keep_indexed(|idx, x| { 217 | if x > idx { 218 | Some(x) 219 | } else { 220 | None 221 | } 222 | }); 223 | let result = source.transduce_into(transducer).unwrap(); 224 | let expected_result = vec![6, 5, 4]; 225 | assert_eq!(expected_result, result); 226 | } 227 | 228 | #[test] 229 | fn test_map_indexed() { 230 | let source = vec![1, 2, 3, 4, 5, 6]; 231 | let transducer = transducers::map_indexed(|idx, x| x + idx); 232 | let result = source.transduce_into(transducer).unwrap(); 233 | let expected_result = vec![1, 3, 5, 7, 9, 11]; 234 | assert_eq!(expected_result, result); 235 | } 236 | 237 | #[test] 238 | fn test_interpose() { 239 | let source = vec![1, 2, 3, 4]; 240 | let transducer = transducers::interpose(0); 241 | let result = source.transduce_into(transducer).unwrap(); 242 | let expected_result = vec![1, 0, 2, 0, 3, 0, 4]; 243 | assert_eq!(expected_result, result); 244 | } 245 | 246 | #[test] 247 | fn test_dedupe() { 248 | let source = vec![1, 2, 2, 2, 3, 3, 2, 3, 4]; 249 | let transducer = transducers::dedupe(); 250 | let result = source.transduce_into(transducer).unwrap(); 251 | let expected_result = vec![1, 2, 3, 2, 3, 4]; 252 | assert_eq!(expected_result, result); 253 | } 254 | 255 | #[test] 256 | fn test_channels() { 257 | let transducer = transducers::map(|x| x + 1); 258 | let (mut tx, rx) = transducing_channel(transducer); 259 | thread::spawn(move|| { 260 | for i in 0..3 { 261 | tx.send(i).unwrap(); 262 | } 263 | tx.close().unwrap(); 264 | }); 265 | assert_eq!(1, rx.recv().unwrap()); 266 | assert_eq!(2, rx.recv().unwrap()); 267 | assert_eq!(3, rx.recv().unwrap()); 268 | } 269 | } 270 | -------------------------------------------------------------------------------- /src/transducers.rs: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 rs-transducers developers 3 | * 4 | * Licensed under the Apache License, Version 2.0 or the MIT license 6 | * , at your 7 | * option. This file may not be copied, modified, or distributed 8 | * except according to those terms. 9 | */ 10 | use std::collections::HashMap; 11 | use std::hash::Hash; 12 | use std::marker::PhantomData; 13 | use std::mem; 14 | 15 | use super::{Transducer, Reducing, StepResult}; 16 | 17 | pub struct MapTransducer { 18 | f: F 19 | } 20 | 21 | pub struct MapReducer { 22 | rf: R, 23 | t: MapTransducer 24 | } 25 | 26 | impl Transducer for MapTransducer { 27 | type RO = MapReducer; 28 | 29 | fn new(self, reducing_fn: RI) -> Self::RO { 30 | MapReducer { 31 | rf: reducing_fn, 32 | t: self 33 | } 34 | } 35 | } 36 | 37 | impl Reducing for MapReducer 38 | where F: Fn(I) -> O, 39 | R: Reducing { 40 | 41 | type Item = O; 42 | 43 | fn init(&mut self) { 44 | self.rf.init(); 45 | } 46 | 47 | #[inline] 48 | fn step(&mut self, value: I) -> Result { 49 | self.rf.step((self.t.f)(value)) 50 | } 51 | 52 | fn complete(&mut self) -> Result<(), E> { 53 | self.rf.complete() 54 | } 55 | } 56 | 57 | pub fn map(f: F) -> MapTransducer 58 | where F: Fn(I) -> O { 59 | 60 | MapTransducer { 61 | f: f 62 | } 63 | } 64 | 65 | pub struct MapIndexedTransducer { 66 | f: F 67 | } 68 | 69 | pub struct MapIndexedReducer { 70 | rf: R, 71 | t: MapIndexedTransducer, 72 | count: usize 73 | } 74 | 75 | impl Transducer for MapIndexedTransducer { 76 | type RO = MapIndexedReducer; 77 | 78 | fn new(self, reducing_fn: RI) -> Self::RO { 79 | MapIndexedReducer { 80 | rf: reducing_fn, 81 | t: self, 82 | count: 0 83 | } 84 | } 85 | } 86 | 87 | impl Reducing for MapIndexedReducer 88 | where F: Fn(usize, I) -> O, 89 | R: Reducing { 90 | 91 | type Item = O; 92 | 93 | fn init(&mut self) { 94 | self.rf.init(); 95 | } 96 | 97 | #[inline] 98 | fn step(&mut self, value: I) -> Result { 99 | let idx = self.count; 100 | self.count += 1; 101 | self.rf.step((self.t.f)(idx, value)) 102 | } 103 | 104 | fn complete(&mut self) -> Result<(), E> { 105 | self.rf.complete() 106 | } 107 | } 108 | 109 | pub fn map_indexed(f: F) -> MapIndexedTransducer 110 | where F: Fn(usize, I) -> O { 111 | 112 | MapIndexedTransducer { 113 | f: f 114 | } 115 | } 116 | 117 | pub struct MapcatTransducer { 118 | f: F 119 | } 120 | 121 | pub struct MapcatReducer { 122 | rf: R, 123 | t: MapcatTransducer 124 | } 125 | 126 | impl Transducer for MapcatTransducer { 127 | type RO = MapcatReducer; 128 | 129 | fn new(self, reducing_fn: RI) -> Self::RO { 130 | MapcatReducer { 131 | rf: reducing_fn, 132 | t: self 133 | } 134 | } 135 | } 136 | 137 | impl Reducing for MapcatReducer 138 | where IO: IntoIterator, 139 | F: Fn(I) -> IO, 140 | R: Reducing { 141 | 142 | type Item = O; 143 | 144 | fn init(&mut self) { 145 | self.rf.init(); 146 | } 147 | 148 | #[inline] 149 | fn step(&mut self, value: I) -> Result { 150 | for o in (self.t.f)(value) { 151 | match self.rf.step(o) { 152 | Ok(StepResult::Continue) => (), 153 | Ok(StepResult::Stop) => return Ok(StepResult::Stop), 154 | Err(e) => return Err(e) 155 | } 156 | } 157 | Ok(StepResult::Continue) 158 | } 159 | 160 | fn complete(&mut self) -> Result<(), E> { 161 | self.rf.complete() 162 | } 163 | } 164 | 165 | pub fn mapcat(f: F) -> MapcatTransducer 166 | where IO: IntoIterator, 167 | F: Fn(I) -> IO { 168 | 169 | MapcatTransducer { 170 | f: f 171 | } 172 | } 173 | 174 | pub struct FilterTransducer { 175 | f: F, 176 | inclusive: bool 177 | } 178 | 179 | pub struct FilterReducer { 180 | rf: R, 181 | t: FilterTransducer 182 | } 183 | 184 | impl Transducer for FilterTransducer { 185 | type RO = FilterReducer; 186 | 187 | fn new(self, reducing_fn: RI) -> Self::RO { 188 | FilterReducer { 189 | rf: reducing_fn, 190 | t: self 191 | } 192 | } 193 | } 194 | 195 | impl Reducing for FilterReducer 196 | where F: Fn(&I) -> bool, 197 | R: Reducing { 198 | type Item = I; 199 | 200 | fn init(&mut self) { 201 | self.rf.init(); 202 | } 203 | 204 | #[inline] 205 | fn step(&mut self, value: I) -> Result { 206 | let mut include = (self.t.f)(&value); 207 | if !self.t.inclusive { 208 | include = !include; 209 | } 210 | if include { 211 | self.rf.step(value) 212 | } else { 213 | Ok(StepResult::Continue) 214 | } 215 | } 216 | 217 | fn complete(&mut self) -> Result<(), E> { 218 | self.rf.complete() 219 | } 220 | } 221 | 222 | pub fn filter(f: F) -> FilterTransducer 223 | where F: Fn(&T) -> bool { 224 | 225 | FilterTransducer { 226 | f: f, 227 | inclusive: true 228 | } 229 | } 230 | 231 | pub fn remove(f: F) -> FilterTransducer 232 | where F: Fn(&T) -> bool { 233 | 234 | FilterTransducer { 235 | f: f, 236 | inclusive: false 237 | } 238 | } 239 | 240 | pub struct KeepTransducer(F); 241 | 242 | pub struct KeepReducer { 243 | rf: R, 244 | t: KeepTransducer 245 | } 246 | 247 | impl Transducer for KeepTransducer { 248 | type RO = KeepReducer; 249 | 250 | fn new(self, reducing_fn: RI) -> Self::RO { 251 | KeepReducer { 252 | rf: reducing_fn, 253 | t: self 254 | } 255 | } 256 | } 257 | 258 | impl Reducing for KeepReducer 259 | where F: Fn(I) -> Option, 260 | R: Reducing { 261 | 262 | type Item = O; 263 | 264 | fn init(&mut self) { 265 | self.rf.init(); 266 | } 267 | 268 | #[inline] 269 | fn step(&mut self, value: I) -> Result { 270 | match (self.t.0)(value) { 271 | Some(o) => self.rf.step(o), 272 | None => Ok(StepResult::Continue) 273 | } 274 | } 275 | 276 | fn complete(&mut self) -> Result<(), E> { 277 | self.rf.complete() 278 | } 279 | } 280 | 281 | pub fn keep(f: F) -> KeepTransducer 282 | where F: Fn(I) -> Option { 283 | 284 | KeepTransducer(f) 285 | } 286 | 287 | pub struct KeepIndexedTransducer(F); 288 | 289 | pub struct KeepIndexedReducer { 290 | rf: R, 291 | t: KeepIndexedTransducer, 292 | count: usize 293 | } 294 | 295 | impl Transducer for KeepIndexedTransducer { 296 | type RO = KeepIndexedReducer; 297 | 298 | fn new(self, reducing_fn: RI) -> Self::RO { 299 | KeepIndexedReducer { 300 | rf: reducing_fn, 301 | t: self, 302 | count: 0 303 | } 304 | } 305 | } 306 | 307 | impl Reducing for KeepIndexedReducer 308 | where F: Fn(usize, I) -> Option, 309 | R: Reducing { 310 | 311 | type Item = O; 312 | 313 | fn init(&mut self) { 314 | self.rf.init(); 315 | } 316 | 317 | #[inline] 318 | fn step(&mut self, value: I) -> Result { 319 | let idx = self.count; 320 | self.count += 1; 321 | 322 | match (self.t.0)(idx, value) { 323 | Some(o) => self.rf.step(o), 324 | None => Ok(StepResult::Continue) 325 | } 326 | } 327 | 328 | fn complete(&mut self) -> Result<(), E> { 329 | self.rf.complete() 330 | } 331 | } 332 | 333 | pub fn keep_indexed(f: F) -> KeepIndexedTransducer 334 | where F: Fn(usize, I) -> Option { 335 | 336 | KeepIndexedTransducer(f) 337 | } 338 | 339 | pub struct PartitionTransducer { 340 | size: usize, 341 | all: bool, 342 | t: PhantomData 343 | } 344 | 345 | pub struct PartitionReducer { 346 | t: PartitionTransducer, 347 | rf: RF, 348 | holder: Vec 349 | } 350 | 351 | impl Transducer for PartitionTransducer { 352 | type RO = PartitionReducer; 353 | 354 | fn new(self, reducing_fn: RI) -> Self::RO { 355 | let size = self.size; 356 | PartitionReducer { 357 | t: self, 358 | rf: reducing_fn, 359 | holder: Vec::with_capacity(size) 360 | } 361 | } 362 | } 363 | 364 | impl Reducing for PartitionReducer 365 | where R: Reducing, OF, E> { 366 | 367 | type Item = Vec; 368 | 369 | fn init(&mut self) { 370 | self.rf.init(); 371 | } 372 | 373 | #[inline] 374 | fn step(&mut self, value: I) -> Result { 375 | self.holder.push(value); 376 | if self.holder.len() == self.t.size { 377 | let mut other_holder = Vec::with_capacity(self.t.size); 378 | mem::swap(&mut other_holder, &mut self.holder); 379 | self.rf.step(other_holder) 380 | } else { 381 | Ok(StepResult::Continue) 382 | } 383 | } 384 | 385 | fn complete(&mut self) -> Result<(), E> { 386 | if self.t.all { 387 | let mut other_holder = Vec::new(); 388 | mem::swap(&mut other_holder, &mut self.holder); 389 | try!(self.rf.step(other_holder)); 390 | } 391 | self.rf.complete() 392 | } 393 | } 394 | 395 | pub fn partition(num: usize) -> PartitionTransducer { 396 | PartitionTransducer { 397 | size: num, 398 | all: false, 399 | t: PhantomData 400 | } 401 | } 402 | 403 | pub fn partition_all(num: usize) -> PartitionTransducer { 404 | PartitionTransducer { 405 | size: num, 406 | all: true, 407 | t: PhantomData 408 | } 409 | } 410 | 411 | pub struct TakeTransducer(usize); 412 | 413 | pub struct TakeReducer { 414 | rf: RF, 415 | taken: usize, 416 | t: TakeTransducer 417 | } 418 | 419 | impl Transducer for TakeTransducer { 420 | type RO = TakeReducer; 421 | 422 | fn new(self, reducing_fn: RI) -> Self::RO { 423 | TakeReducer { 424 | rf: reducing_fn, 425 | taken: 0, 426 | t: self 427 | } 428 | } 429 | } 430 | 431 | impl Reducing for TakeReducer 432 | where R: Reducing { 433 | 434 | type Item = I; 435 | 436 | fn init(&mut self) { 437 | self.rf.init(); 438 | } 439 | 440 | #[inline] 441 | fn step(&mut self, value: I) -> Result { 442 | if self.taken < self.t.0 { 443 | self.taken += 1; 444 | match self.rf.step(value) { 445 | Ok(StepResult::Continue) => if self.taken < self.t.0 { 446 | Ok(StepResult::Continue) 447 | } else { 448 | Ok(StepResult::Stop) 449 | }, 450 | Ok(StepResult::Stop) => Ok(StepResult::Stop), 451 | Err(e) => Err(e) 452 | } 453 | } else { 454 | Ok(StepResult::Stop) 455 | } 456 | } 457 | 458 | fn complete(&mut self) -> Result<(), E> { 459 | self.rf.complete() 460 | } 461 | } 462 | 463 | pub fn take(num: usize) -> TakeTransducer { 464 | TakeTransducer(num) 465 | } 466 | 467 | pub struct TakeWhileTransducer(F); 468 | 469 | pub struct TakeWhileReducer { 470 | rf: RF, 471 | t: TakeWhileTransducer 472 | } 473 | 474 | impl Transducer for TakeWhileTransducer { 475 | type RO = TakeWhileReducer; 476 | 477 | fn new(self, reducing_fn: RI) -> Self::RO { 478 | TakeWhileReducer { 479 | rf: reducing_fn, 480 | t: self 481 | } 482 | } 483 | } 484 | 485 | impl Reducing for TakeWhileReducer 486 | where R: Reducing, 487 | F: Fn(&I) -> bool { 488 | 489 | type Item = I; 490 | 491 | fn init(&mut self) { 492 | self.rf.init(); 493 | } 494 | 495 | #[inline] 496 | fn step(&mut self, value: I) -> Result { 497 | if (self.t.0)(&value) { 498 | self.rf.step(value) 499 | } else { 500 | Ok(StepResult::Stop) 501 | } 502 | } 503 | 504 | fn complete(&mut self) -> Result<(), E> { 505 | self.rf.complete() 506 | } 507 | } 508 | 509 | pub fn take_while(pred: F) -> TakeWhileTransducer 510 | where F: Fn(&T) -> bool { 511 | 512 | TakeWhileTransducer(pred) 513 | } 514 | 515 | pub struct DropWhileTransducer(F); 516 | 517 | pub struct DropWhileReducer { 518 | rf: RF, 519 | t: DropWhileTransducer, 520 | done: bool 521 | } 522 | 523 | impl Transducer for DropWhileTransducer { 524 | type RO = DropWhileReducer; 525 | 526 | fn new(self, reducing_fn: RI) -> Self::RO { 527 | DropWhileReducer { 528 | rf: reducing_fn, 529 | t: self, 530 | done: false 531 | } 532 | } 533 | } 534 | 535 | impl Reducing for DropWhileReducer 536 | where R: Reducing, 537 | F: Fn(&I) -> bool { 538 | 539 | type Item = I; 540 | 541 | fn init(&mut self) { 542 | self.rf.init(); 543 | } 544 | 545 | #[inline] 546 | fn step(&mut self, value: I) -> Result { 547 | if self.done { 548 | self.rf.step(value) 549 | } else { 550 | if !(self.t.0)(&value) { 551 | self.done = true; 552 | self.rf.step(value) 553 | } else { 554 | Ok(StepResult::Continue) 555 | } 556 | } 557 | } 558 | 559 | fn complete(&mut self) -> Result<(), E> { 560 | self.rf.complete() 561 | } 562 | } 563 | 564 | pub fn drop_while(pred: F) -> DropWhileTransducer 565 | where F: Fn(&T) -> bool { 566 | 567 | DropWhileTransducer(pred) 568 | } 569 | 570 | pub struct DropTransducer(usize); 571 | 572 | pub struct DropReducer { 573 | rf: RF, 574 | dropped: usize, 575 | d: DropTransducer 576 | } 577 | 578 | impl Transducer for DropTransducer { 579 | type RO = DropReducer; 580 | 581 | fn new(self, reducing_fn: RI) -> Self::RO { 582 | DropReducer { 583 | rf: reducing_fn, 584 | dropped: 0, 585 | d: self 586 | } 587 | } 588 | } 589 | 590 | impl Reducing for DropReducer 591 | where R: Reducing { 592 | 593 | type Item = I; 594 | 595 | fn init(&mut self) { 596 | self.rf.init(); 597 | } 598 | 599 | #[inline] 600 | fn step(&mut self, value: I) -> Result { 601 | if self.dropped < self.d.0 { 602 | self.dropped += 1; 603 | Ok(StepResult::Continue) 604 | } else { 605 | self.rf.step(value) 606 | } 607 | } 608 | 609 | fn complete(&mut self) -> Result<(), E> { 610 | self.rf.complete() 611 | } 612 | } 613 | 614 | pub fn drop(size: usize) -> DropTransducer { 615 | DropTransducer(size) 616 | } 617 | 618 | pub struct ReplaceTransducer(HashMap); 619 | 620 | pub struct ReplaceReducer { 621 | rf: RF, 622 | t: ReplaceTransducer 623 | } 624 | 625 | impl <'a, RI, T> Transducer for ReplaceTransducer { 626 | type RO = ReplaceReducer; 627 | 628 | fn new(self, reducing_fn: RI) -> Self::RO { 629 | ReplaceReducer { 630 | rf: reducing_fn, 631 | t: self 632 | } 633 | } 634 | } 635 | 636 | impl<'a, R, I, OF, E> Reducing for ReplaceReducer 637 | where I: Eq + Hash + Clone, 638 | R: Reducing { 639 | 640 | type Item = I; 641 | 642 | fn init(&mut self) { 643 | self.rf.init(); 644 | } 645 | 646 | #[inline] 647 | fn step(&mut self, value: I) -> Result { 648 | let v:I = match self.t.0.get(&value) { 649 | Some(val) => val.clone(), 650 | None => value 651 | }; 652 | self.rf.step(v) 653 | } 654 | 655 | fn complete(&mut self) -> Result<(), E> { 656 | self.rf.complete() 657 | } 658 | } 659 | 660 | pub fn replace(replacements: HashMap) -> ReplaceTransducer { 661 | ReplaceTransducer(replacements) 662 | } 663 | 664 | pub struct PartitionByTransducer 665 | where F: Fn(&T) -> R { 666 | 667 | f: F, 668 | t: PhantomData 669 | } 670 | 671 | pub struct PartitionByReducer 672 | where F: Fn(&T) -> R { 673 | 674 | rf: RF, 675 | t: PartitionByTransducer, 676 | holder: Vec, 677 | last_res: Option 678 | } 679 | 680 | impl Transducer for PartitionByTransducer 681 | where F: Fn(&T) -> R { 682 | 683 | type RO = PartitionByReducer; 684 | 685 | fn new(self, reducing_fn: RI) -> Self::RO { 686 | PartitionByReducer { 687 | rf: reducing_fn, 688 | t: self, 689 | holder: Vec::new(), 690 | last_res: None 691 | } 692 | } 693 | } 694 | 695 | impl Reducing for PartitionByReducer 696 | where R: Reducing, OF, E>, 697 | F: Fn(&I) -> X, 698 | X: Eq { 699 | 700 | type Item = Vec; 701 | 702 | fn init(&mut self) { 703 | self.rf.init(); 704 | } 705 | 706 | #[inline] 707 | fn step(&mut self, value: I) -> Result { 708 | let last_res = self.last_res.take(); 709 | match last_res { 710 | None => { 711 | self.last_res = Some((self.t.f)(&value)); 712 | self.holder.push(value); 713 | Ok(StepResult::Continue) 714 | }, 715 | Some(ref res) => { 716 | let new_res = (self.t.f)(&value); 717 | if res == &new_res { 718 | self.holder.push(value); 719 | Ok(StepResult::Continue) 720 | } else { 721 | self.last_res = Some(new_res); 722 | let mut other_holder = Vec::new(); 723 | mem::swap(&mut other_holder, &mut self.holder); 724 | self.holder.push(value); 725 | self.rf.step(other_holder) 726 | } 727 | } 728 | } 729 | } 730 | 731 | fn complete(&mut self) -> Result<(), E> { 732 | if self.holder.len() > 0 { 733 | let mut other_holder = Vec::new(); 734 | mem::swap(&mut other_holder, &mut self.holder); 735 | try!(self.rf.step(other_holder)); 736 | } 737 | self.rf.complete() 738 | } 739 | } 740 | 741 | pub fn partition_by(partition_func: F) -> PartitionByTransducer 742 | where F: Fn(&T) -> R { 743 | 744 | PartitionByTransducer { 745 | f: partition_func, 746 | t: PhantomData 747 | } 748 | } 749 | 750 | pub struct InterposeTransducer(T); 751 | 752 | pub struct InterposeReducer { 753 | first: bool, 754 | rf: R, 755 | t: InterposeTransducer 756 | } 757 | 758 | impl Transducer for InterposeTransducer { 759 | type RO = InterposeReducer; 760 | 761 | fn new(self, reducing_fn: RI) -> Self::RO { 762 | InterposeReducer { 763 | first: true, 764 | rf: reducing_fn, 765 | t: self 766 | } 767 | } 768 | } 769 | 770 | impl Reducing for InterposeReducer 771 | where I: Clone, 772 | R: Reducing { 773 | 774 | type Item = I; 775 | 776 | fn init(&mut self) { 777 | self.rf.init(); 778 | } 779 | 780 | #[inline] 781 | fn step(&mut self, value: I) -> Result { 782 | if self.first { 783 | self.first = false; 784 | } else { 785 | match try!(self.rf.step(self.t.0.clone())) { 786 | StepResult::Continue => (), 787 | StepResult::Stop => return Ok(StepResult::Stop) 788 | } 789 | } 790 | self.rf.step(value) 791 | } 792 | 793 | fn complete(&mut self) -> Result<(), E> { 794 | self.rf.complete() 795 | } 796 | } 797 | 798 | pub fn interpose(separator: T) -> InterposeTransducer { 799 | InterposeTransducer(separator) 800 | } 801 | 802 | pub struct DedupeTransducer(PhantomData); 803 | 804 | pub struct DedupeReducer { 805 | last_val: Option, 806 | rf: R 807 | } 808 | 809 | impl Transducer for DedupeTransducer { 810 | type RO = DedupeReducer; 811 | 812 | fn new(self, reducing_fn: RI) -> Self::RO { 813 | DedupeReducer { 814 | last_val: None, 815 | rf: reducing_fn 816 | } 817 | } 818 | } 819 | 820 | impl Reducing for DedupeReducer 821 | where I: Eq + Clone, 822 | R: Reducing { 823 | 824 | type Item = I; 825 | 826 | fn init(&mut self) { 827 | self.rf.init(); 828 | } 829 | 830 | #[inline] 831 | fn step(&mut self, value: I) -> Result { 832 | if self.last_val.is_none() { 833 | self.last_val = Some(value.clone()); 834 | self.rf.step(value) 835 | } else if self.last_val.as_ref().unwrap() == &value { 836 | Ok(StepResult::Continue) 837 | } else { 838 | self.last_val = Some(value.clone()); 839 | self.rf.step(value) 840 | } 841 | } 842 | 843 | fn complete(&mut self) -> Result<(), E> { 844 | self.rf.complete() 845 | } 846 | } 847 | 848 | pub fn dedupe() -> DedupeTransducer { 849 | DedupeTransducer(PhantomData) 850 | } 851 | --------------------------------------------------------------------------------