diff --git a/LanguageExt.Core/CollectionBuilderAttribute.cs b/LanguageExt.Core/CollectionBuilderAttribute.cs new file mode 100644 index 000000000..7863e089b --- /dev/null +++ b/LanguageExt.Core/CollectionBuilderAttribute.cs @@ -0,0 +1,20 @@ +namespace System.Runtime.CompilerServices +{ + [AttributeUsage( + AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface, + Inherited = false, + AllowMultiple = false)] + public sealed class CollectionBuilderAttribute : Attribute + { + public CollectionBuilderAttribute( + Type builderType, + string methodName) + { + BuilderType = builderType; + MethodName = methodName; + } + + public Type BuilderType { get; } + public string MethodName { get; } + } +} diff --git a/LanguageExt.Core/Immutable Collections/Arr/Arr.Module.cs b/LanguageExt.Core/Immutable Collections/Arr/Arr.Module.cs index ed2a8b9f9..cf9fde44a 100644 --- a/LanguageExt.Core/Immutable Collections/Arr/Arr.Module.cs +++ b/LanguageExt.Core/Immutable Collections/Arr/Arr.Module.cs @@ -34,6 +34,17 @@ public static partial class Arr [Pure] public static Arr create(params T[] items) => new Arr(items); + + /// + /// Create an array from a initial set of items + /// + /// Items + /// Lst T + [Pure] + public static Arr create( + ReadOnlySpan items) => + create( + items.ToArray()); /// /// Create an array from an initial set of items diff --git a/LanguageExt.Core/Immutable Collections/Arr/Arr.cs b/LanguageExt.Core/Immutable Collections/Arr/Arr.cs index d4018f0f5..5ee7acda5 100644 --- a/LanguageExt.Core/Immutable Collections/Arr/Arr.cs +++ b/LanguageExt.Core/Immutable Collections/Arr/Arr.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Collections; using System.Collections.Generic; +using System.ComponentModel; using System.Diagnostics.Contracts; using static LanguageExt.Prelude; using LanguageExt.TypeClasses; @@ -17,6 +18,9 @@ namespace LanguageExt /// for large arrays. /// /// Value type + [CollectionBuilderAttribute( + typeof(Arr), + nameof(Arr.create))] [Serializable] public struct Arr : IEnumerable, @@ -721,6 +725,5 @@ public Arr Bind(Func> f) [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Arr(SeqEmpty _) => Empty; - } } diff --git a/LanguageExt.Core/Immutable Collections/List/Lst.Module.cs b/LanguageExt.Core/Immutable Collections/List/Lst.Module.cs index 79137ecbd..62663bd7d 100644 --- a/LanguageExt.Core/Immutable Collections/List/Lst.Module.cs +++ b/LanguageExt.Core/Immutable Collections/List/Lst.Module.cs @@ -50,6 +50,17 @@ public static partial class List public static Lst create(params T[] items) => new Lst(items); + /// + /// Create an array from a initial set of items + /// + /// Items + /// Lst T + [Pure] + public static Lst create( + ReadOnlySpan items) => + create( + items.ToArray()); + /// /// Create a list from an initial set of items /// diff --git a/LanguageExt.Core/Immutable Collections/List/Lst.cs b/LanguageExt.Core/Immutable Collections/List/Lst.cs index b954ad7d1..064c29ee8 100644 --- a/LanguageExt.Core/Immutable Collections/List/Lst.cs +++ b/LanguageExt.Core/Immutable Collections/List/Lst.cs @@ -15,6 +15,9 @@ namespace LanguageExt /// Immutable list /// /// Value type + [CollectionBuilder( + typeof(List), + nameof(List.create))] [Serializable] public readonly struct Lst : IComparable>, diff --git a/LanguageExt.Core/Immutable Collections/Seq/Seq.Module.cs b/LanguageExt.Core/Immutable Collections/Seq/Seq.Module.cs index 4b27e03b8..6b822dde4 100644 --- a/LanguageExt.Core/Immutable Collections/Seq/Seq.Module.cs +++ b/LanguageExt.Core/Immutable Collections/Seq/Seq.Module.cs @@ -57,6 +57,17 @@ public static Seq create(params A[] items) System.Array.Copy(items, nitems, items.Length); return FromArray(items); } + + /// + /// Create an array from a initial set of items + /// + /// Items + /// Lst T + [Pure] + public static Seq create( + ReadOnlySpan items) => + create( + items.ToArray()); /// /// Create a sequence from an initial set of items diff --git a/LanguageExt.Core/Immutable Collections/Seq/Seq.cs b/LanguageExt.Core/Immutable Collections/Seq/Seq.cs index 7e855745a..3d6690db6 100644 --- a/LanguageExt.Core/Immutable Collections/Seq/Seq.cs +++ b/LanguageExt.Core/Immutable Collections/Seq/Seq.cs @@ -17,6 +17,9 @@ namespace LanguageExt /// /// Type of the values in the sequence + [CollectionBuilder( + typeof(Seq), + nameof(Seq.create))] public readonly struct Seq : #pragma warning disable CS0618 // Remove ISeq complaint ISeq, diff --git a/LanguageExt.Core/Immutable Collections/Set/Set.Module.cs b/LanguageExt.Core/Immutable Collections/Set/Set.Module.cs index cfad3a3ef..e7aaa25b7 100644 --- a/LanguageExt.Core/Immutable Collections/Set/Set.Module.cs +++ b/LanguageExt.Core/Immutable Collections/Set/Set.Module.cs @@ -32,6 +32,16 @@ public static partial class Set public static Set create() => Set.Empty; + /// + /// Create an array from a initial set of items + /// + /// Items + /// Lst T + [Pure] + public static Set create( + ReadOnlySpan items) => + new Set(items.ToArray()); + /// /// Create a new set pre-populated with the items in range /// diff --git a/LanguageExt.Core/Immutable Collections/Set/Set.cs b/LanguageExt.Core/Immutable Collections/Set/Set.cs index b9a8a2a99..5cf0de0fb 100644 --- a/LanguageExt.Core/Immutable Collections/Set/Set.cs +++ b/LanguageExt.Core/Immutable Collections/Set/Set.cs @@ -19,6 +19,9 @@ namespace LanguageExt /// [wikipedia.org/wiki/AVL_tree](http://en.wikipedia.org/wiki/AVL_tree) /// /// Set item type + [CollectionBuilder( + typeof(Set), + nameof(Set.create))] [Serializable] public readonly struct Set : IEnumerable,