async def main(): in_ch = c.to_chan(range(4)) with self.assertRaises(TypeError): c.transduce( xf.filter(lambda x: x % 2 == 0), xf.multi_arity(None, xf.identity, lambda x, y: x + y), in_ch)
async def main(): in_ch = chan() in_ch.close() result_ch = c.reduce( xf.multi_arity(lambda: 100, xf.identity, lambda x, y: x + y), in_ch) self.assertEqual(await result_ch.get(), 100)
async def main(): in_ch = c.to_chan(range(4)) result_ch = c.transduce( xf.filter(lambda x: x % 2 == 0), xf.multi_arity(lambda: 100, xf.identity, lambda x, y: x + y), in_ch) self.assertEqual(await result_ch.get(), 102)
def test_custom_cf(self): rf = xf.completing(xf.multi_arity(lambda: 0, None, lambda x, y: x + y), str) self.assertEqual(rf(), 0) self.assertEqual(rf(1, 2), 3) self.assertEqual(rf(100), '100')
def test_default_cf(self): rf = xf.completing(xf.multi_arity(lambda: 0, None, lambda x, y: x + y)) self.assertEqual(rf(), 0) self.assertEqual(rf(1, 2), 3) self.assertEqual(rf('success'), 'success')
def test_reductions_no_init(self): xform = xf.reductions(xf.multi_arity(lambda: 100, None, sum_rf)) self.assertEqual(list(xf.xiter(xform, [1, 2])), [100, 101, 103])
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest from chanpy import transducers as xf sum_rf = xf.multi_arity(lambda: 0, xf.identity, lambda x, y: x + y) class TestPartitionAll(unittest.TestCase): def test_partition_every(self): xform = xf.partition_all(1) self.assertEqual(list(xf.xiter(xform, range(3))), [(0,), (1,), (2,)]) def test_partition_pos(self): xform = xf.partition_all(3) self.assertEqual(list(xf.xiter(xform, range(6))), [(0, 1, 2), (3, 4, 5)]) def test_partition_empty(self): xform = xf.partition_all(1) self.assertEqual(list(xf.xiter(xform, [])), [])