Skip to content

Commit 7793969

Browse files
committed
Fix for SourceT Skip and Take issues #1503
#1489
1 parent cd6d051 commit 7793969

3 files changed

Lines changed: 69 additions & 4 deletions

File tree

LanguageExt.Streaming/SourceT/DSL/TransformSourceT.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ namespace LanguageExt;
55
record TransformSourceT<M, A, B>(SourceT<M, A> Source, TransducerM<M, A, B> Transducer) : SourceT<M, B>
66
where M : MonadIO<M>, Alternative<M>
77
{
8-
public override K<M, S> ReduceM<S>(S state, ReducerM<M, K<M, B>, S> reducer) =>
9-
Source.ReduceM(state,
10-
(s, ma) => ma.Bind(a => Transducer.Reduce<S>((s1, b) => reducer(s1, M.Pure(b)))(s, a)));
8+
public override K<M, S> ReduceM<S>(S state, ReducerM<M, K<M, B>, S> reducer)
9+
{
10+
var t = Transducer.Reduce<S>((s1, b) => reducer(s1, M.Pure(b)));
11+
return Source.ReduceM(state, (s, ma) => ma.Bind(a => t(s, a)));
12+
}
1113
}

Samples/TestBed/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ static void Main(string[] args)
5252
// //
5353
///////////////////////////////////////////v////////////////////////////////////////////////////////////
5454

55-
RecurTests.Run();
55+
SourceTTests.Run();
56+
//RecurTests.Run();
5657
//BracketTest.Run();
5758
//AwaitAnyTest.Run();
5859
//TestBed.StateStuff.StateForkIO.forkTest.Run(4).Run().Ignore();

Samples/TestBed/SourceTTests.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
global using LanguageExt;
2+
global using static LanguageExt.Prelude;
3+
global using LanguageExt.Pipes;
4+
global using LanguageExt.Traits;
5+
using System;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace TestBed;
10+
11+
public static class SourceExt
12+
{
13+
14+
public static SourceT<M, A> Log<M, A>(this SourceT<M, A> src, Func<A, string> log)
15+
where M: MonadIO<M>, Alternative<M> =>
16+
src.Map(x => { Console.WriteLine(log(x)); return x; });
17+
18+
public static Source<A> Log<A>(this Source<A> src, Func<A, string> log) =>
19+
src.Map(x => { Console.WriteLine(log(x)); return x; });
20+
}
21+
22+
public class SourceTTests
23+
{
24+
public record MySkipTransducerM<M, A>(int Amount) : TransducerM<M, A, A>
25+
where M : Applicative<M>
26+
{
27+
public int amount = Amount;
28+
public override ReducerM<M, A, S> Reduce<S>(ReducerM<M, A, S> reducer)
29+
{
30+
return (s, x) =>
31+
{
32+
if (amount > 0)
33+
{
34+
amount--;
35+
return M.Pure(s);
36+
}
37+
else
38+
{
39+
return reducer(s, x);
40+
}
41+
};
42+
}
43+
}
44+
45+
public static SourceT<IO, int> getSourceT() =>
46+
SourceT.lift<IO, int>(Enumerable.Range(1, 5)).Log(x => $"Producing {x}");
47+
48+
public static Source<int> getSource() =>
49+
Source.lift(Enumerable.Range(1, 5)).Log(x => $"Producing {x}");
50+
51+
public static void Run()
52+
{
53+
Console.WriteLine("\nCurrent Source Skip\n");
54+
getSource().Skip(1).Log(x => $"CurrentSkip: Got {x}").Iter().Run();
55+
56+
Console.WriteLine("\nCurrent SourceT Skip\n");
57+
getSourceT().Skip(1).Log(x => $"CurrentSkipT: Got {x}").Iter().Run();
58+
59+
//Console.WriteLine("\nPatched SourceT Skip\n");
60+
//getSourceT().Transform(new MySkipTransducerM<IO, int>(1)).Log(x => $"PatchedSkip: Got {x}").Iter().Run();
61+
}
62+
}

0 commit comments

Comments
 (0)