corecursion

How to encode corecursion/codata in a strictly evaluated setting?

限于喜欢 提交于 2020-11-29 03:07:06
问题 Corecursion means calling oneself on data at each iteration that is greater than or equal to what one had before. Corecursion works on codata, which are recursively defined values. Unfortunately, value recursion is not possible in strictly evaluated languages. We can work with explicit thunks though: const Defer = thunk => ({get runDefer() {return thunk()}}) const app = f => x => f(x); const fibs = app(x_ => y_ => { const go = x => y => Defer(() => [x, go(y) (x + y)]); return go(x_) (y_)

How to encode corecursion/codata in a strictly evaluated setting?

无人久伴 提交于 2020-11-29 03:07:05
问题 Corecursion means calling oneself on data at each iteration that is greater than or equal to what one had before. Corecursion works on codata, which are recursively defined values. Unfortunately, value recursion is not possible in strictly evaluated languages. We can work with explicit thunks though: const Defer = thunk => ({get runDefer() {return thunk()}}) const app = f => x => f(x); const fibs = app(x_ => y_ => { const go = x => y => Defer(() => [x, go(y) (x + y)]); return go(x_) (y_)

Observable from chained Tasks

空扰寡人 提交于 2019-12-18 04:24:14
问题 I'm trying to create an Observable where each item is produced via an asynchronous task. The next item should be produced via an async call on the result of the previous item (co-recursion). In "Generate" parlance this would look something like this - except that Generate does not support async (nor does it support the delegate on the initial state. var ob = Observable.Generate( async () => await ProduceFirst(), // Task<T> ProduceFirst() prev => Continue(prev) // bool Continue(T); async prev

Observable.Generate in RxJava?

一个人想着一个人 提交于 2019-12-11 00:08:23
问题 .NET Reactive Extensions has a neat method to generate sequences using corecursion which is called Observable.Generate. Is there analogues method in RxJava that allows data generation via corecursion? If not, could it be implemented based on existing methods? 回答1: It's not an exact match but we have SyncOnSubscribe (and AsyncOnSubscriber ) that can generate values, for example: @Test public void testRange() { final int start = 1; final int count = 4000; OnSubscribe<Integer> os =

List filter using an anamorphism

人走茶凉 提交于 2019-12-08 15:32:10
问题 I implemented a broken filter function using an anamorphism from recursion-schemes Hackage library: import Data.Functor.Foldable xfilter :: (a -> Bool) -> [a] -> [a] xfilter f = ana $ project . phi f phi :: (a -> Bool) -> [a] -> [a] phi f (h : t) | not (f h) = t phi f l = l The function is not a faithful implementation of filter : xfilter odd [1..5] works, but xfilter odd [0,0] doesn't. I tried to implement "retries" by using explicit recursion in phi and then reimplemented that with a

Observable from chained Tasks

别来无恙 提交于 2019-11-29 05:08:36
I'm trying to create an Observable where each item is produced via an asynchronous task. The next item should be produced via an async call on the result of the previous item (co-recursion). In "Generate" parlance this would look something like this - except that Generate does not support async (nor does it support the delegate on the initial state. var ob = Observable.Generate( async () => await ProduceFirst(), // Task<T> ProduceFirst() prev => Continue(prev) // bool Continue(T); async prev => await ProduceNext(prev) // Task<T> ProduceNext(T) item => item ); As a more concrete example, to