|
| 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