Latest v5 alpha IEnumerable<T> ToSeq() extension is missing #1329
Replies: 4 comments
-
It is intentional. public static Seq<A> ToSeq<T, A>(this K<T, A> ta)
where T : Foldable<T> =>
T.ToSeq(ta); This allows any foldable to be converted to a The original extension that worked with public static Seq<A> ToSeq<A>(this IEnumerable<A> items) =>
... The one type this is a major problem for is
I am also considering re-adding some extension method (with an alternative name), because I too have found the lack of Another approach is to change enumerable.As().ToSeq() My only issue with that is that I'd prefer to not overdo the Other thoughts are that the trait members could have an alternative name, like Naming things is hard! Anyway, it is on my mind, I'm just not sure what my solution will be yet. |
Beta Was this translation helpful? Give feedback.
-
On thing I forgot to add. If the type you're calling Below is a simple binary tree implementation: public abstract record Tree<A> : IEnumerable<A>
{
/// <summary>
/// Empty binary tree
/// </summary>
public static readonly Tree<A> Empty = new Empty<A>();
/// <summary>
/// Add an item to the tree
/// </summary>
public abstract Tree<A> Add(A value);
/// <summary>
/// Traverse the tree left-node-right
/// </summary>
internal abstract IEnumerable<A> LeftNodeRight();
/// <summary>
/// Traverse the tree right-node-left
/// </summary>
internal abstract IEnumerable<A> RightNodeLeft();
/// <summary>
/// Standard enumerator - left/node/right
/// </summary>
public IEnumerator<A> GetEnumerator() =>
LeftNodeRight().GetEnumerator();
/// <summary>
/// Standard enumerator - left/node/right
/// </summary>
IEnumerator IEnumerable.GetEnumerator() =>
GetEnumerator();
}
/// <summary>
/// Tree node
/// </summary>
/// <param name="Value">Value stored</param>
/// <param name="Left">Left branch</param>
/// <param name="Right">Right branch</param>
public record Node<A>(A Value, Tree<A> Left, Tree<A> Right) : Tree<A>
{
public override Tree<A> Add(A value) =>
Comparer<A>.Default.Compare(value, Value) switch
{
0 => throw new Exception("Value already added"),
< 0 => new Node<A>(Value, Left.Add(value), Right),
> 0 => new Node<A>(Value, Left, Right.Add(value))
};
internal override IEnumerable<A> LeftNodeRight()
{
foreach (var item in Left.LeftNodeRight()) yield return item;
yield return Value;
foreach (var item in Right.LeftNodeRight()) yield return item;
}
internal override IEnumerable<A> RightNodeLeft()
{
foreach (var item in Right.RightNodeLeft()) yield return item;
yield return Value;
foreach (var item in Left.RightNodeLeft()) yield return item;
}
}
/// <summary>
/// Empty tree node
/// </summary>
/// <typeparam name="A"></typeparam>
public record Empty<A> : Tree<A>
{
public override Tree<A> Add(A value) =>
new Node<A>(value, Empty, Empty);
internal override IEnumerable<A> LeftNodeRight() =>
[];
internal override IEnumerable<A> RightNodeLeft() =>
[];
} We can make that into something that can be converted to a public class Tree : Foldable<Tree>
{
public static S FoldWhile<A, S>(
Func<A, Func<S, S>> f,
Func<(S State, A Value), bool> predicate,
S state,
K<Tree, A> ta)
{
foreach (var item in ta.As().LeftNodeRight())
{
if (!predicate((state, item))) return state;
state = f(item)(state);
}
return state;
}
public static S FoldBackWhile<A, S>(
Func<S, Func<A, S>> f,
Func<(S State, A Value), bool> predicate,
S state,
K<Tree, A> ta)
{
foreach (var item in ta.As().RightNodeLeft())
{
if (!predicate((state, item))) return state;
state = f(state)(item);
}
return state;
}
} And make public abstract record Tree<A> : IEnumerable<A>, K<Tree, A> Now So, if you have |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply! Maybe the whole |
Beta Was this translation helpful? Give feedback.
-
If I were to go that route, I'd call it The other example that exists so far is
I'll move this to the discussions section. Anybody else has an opinion on this I am definitely open to your thoughts and ideas. Whatever is chosen just needs to be as elegant as it can be! |
Beta Was this translation helpful? Give feedback.
-
Hi!
A quick question - is extension method
ToSeq()
onIEnumerable<T>
gone? I can't seem to find it. I can doAsEnumerableM()
and theToSeq()
on that, but can't find the original extension method.Is this intentional?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions