Skip to content

Commit

Permalink
General purpose tail recursion for K<M, A>
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Dec 19, 2024
1 parent 2f2a49f commit 967e976
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions LanguageExt.Core/Effects/IO/IO.Prelude.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public static partial class Prelude
/// <returns></returns>
public static IO<A> tail<A>(IO<A> tailIO) =>
new IOTail<A>(tailIO);

/// <summary>
/// Tail call
/// </summary>
/// <param name="ma"></param>
/// <typeparam name="A"></typeparam>
/// <returns></returns>
public static K<M, A> tailIO<M, A>(K<M, A> ma)
where M : Monad<M> =>
ma.MapIO(tail);

/// <summary>
/// Make this IO computation run on the `SynchronizationContext` that was captured at the start
Expand Down
6 changes: 6 additions & 0 deletions Samples/IOExmples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ static IO<Unit> infiniteLoop(int value) =>
: Pure(unit)
from r in tail(infiniteLoop(value + 1))
select unit;

static IO<Unit> infiniteLoop1(int value) =>
(value % 10000 == 0
? writeLine($"{value}")
: Pure(unit))
.Bind(_ => infiniteLoop1(value + 1));

static IO<int> recursiveAskForNumber =>
from n in askForNumber(1)
Expand Down

0 comments on commit 967e976

Please sign in to comment.