`repeat` capturing free variables
See original GitHub issueDiscussed in https://github.com/louthy/language-ext/discussions/1064
<div type='discussions-op-text'>Originally posted by mozesa June 23, 2022
using System.Text;
using LanguageExt;
using LanguageExt.Common;
using LanguageExt.Effects.Traits;
using LanguageExt.Sys;
using LanguageExt.Sys.Traits;
using static LanguageExt.Prelude;
namespace ConsoleApp;
public class QueueExample<RT>
where RT : struct,
HasCancel<RT>,
HasConsole<RT>,
HasDirectory<RT>,
HasFile<RT>
{
public static Aff<RT, Unit> Main()
{
var content = Encoding.ASCII.GetBytes("test\0test\0test\0");
var memStream = new MemoryStream(100);
memStream.Write(content, 0, content.Length);
memStream.Seek(0, SeekOrigin.Begin);
return repeat(
// from _51 in SuccessEff(unit)
from ln in (
from data in Eff(memStream.ReadByte)
from _ in guard(data != -1, Errors.Cancelled)
select data).FoldUntil(string.Empty, (s, ch) => s + (char)ch, ch => ch == '\0')
from _52 in Console<RT>.writeLine(ln)
select unit)
| @catch(exception => Console<RT>.writeLine(exception.Message));
}
}
Result when this line // from _51 in SuccessEff(unit) left commented out.
test
test test
test test test
cancelled
Result when this line from _51 in SuccessEff(unit) is enabled.
test
test
test
cancelled
</div>Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
How to capture multiple repeated groups? - regex
With one group in the pattern, you can only get one exact result in that group. If your capture group gets repeated by...
Read more >A question on free variable capture.
The simplest solution is to view free variables as a temporary concept useful only to bootstrap things, and to always use closed expressions, ......
Read more >Free variables and bound variables
In computer programming, the term free variable refers to variables used in a function that are neither local variables nor parameters of that...
Read more >Go internals: capturing loop variables in closures - Eli Bendersky
Variables declared by the init statement are re-used in each iteration. This means that when the program is running, there's just a single ......
Read more >Repeat records based on variable value
What I want is a repeater, that repeats the records inside the table ... variable End is used to put the value at...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

ToAff()can’t re-callreader.ReadLineAsync(), it can only convert the result of callingreader.ReadLineAsync()(once) to anAff. And so you are baking in the result-value for good. Usually you’d only useToAff()with a pure value, or something that you want to effectively become a constant. The only way to create something that re-runs each time the expression is evaluated is to lift a lambda, which is what you do withAff(() => ...). This is just how C# works, so there’s no bug here, just different use-cases. Remember also that LINQ is a nested set of lambdas, and so after the firstfrom ...everything below it is within a lamdba.Even then you need to be careful of what you lift into the lambda,
readerin this case is instanced outside of therepeat. So, you need to consider what it is you want to be repeated.btw, what you’re attempting already exists (that is the yielding of strings from a stream). So, you only need to provide the behaviour of opening the stream. Here’s an example (I’ve not tested it, but it should work! It also cleans up the disposables afterwards)
Producers and Pipes obviously have to be composed with Consumers to produce an
Effect<RT, A>. Effects are entirely self-enclosed systems. They will continue to run until they have either:cancel<RT>()effectFailEff/FailAffor an exception has been thrownguard,guardnot, orwhen.For example, here’s an effect that writes the lines to the screen, until a line is
"exit". It uses guards to test and then raise the error if the predicate is trueIf you need to cancel the
Effectfrom the outside, i.e. not from within theEffectstream itself. Then first you need to see what you get when you callRunEffect():That returned
Aff<RT, Unit>is the whole effect encapsulated in a re-runnableAff. So, callingeffect.Run(runtime)means that theruntimeargument must have aCancellationTokenwhich you can then cancel in the normal .NET way.The other way is that you may then include
effectin a largerAffexpression:As soon as the
from y ...starts, theEffectand all of its resource will have automatically been cleaned up.One final method is to
forkthe effect, so that it runs independently of its parent expression:This will allow the
effectto run in its own thread. The parent thread will wait for any key to be pressed in the console, and will then run thecanceleffect, which will shutdown the forked effect (and in the process clean up all of the resources).