Skip to content

Commit

Permalink
Somes for Seq<Options>
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Aug 6, 2017
1 parent 7cec5ac commit b0a546e
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -194,3 +194,4 @@ ModelManifest.xml
/SettingsParser.playlist
/Parsec Tests.playlist
/LanguageExt.Tests/LanguageExt.Tests.nuget.props
/.idea/.idea.language-ext
24 changes: 22 additions & 2 deletions LanguageExt.Core/DataTypes/Option/Option.Extensions.cs
Expand Up @@ -17,8 +17,8 @@
public static class OptionExtensions
{
/// <summary>
/// Extracts from a list of 'Option' all the 'Some' elements.
/// All the 'Some' elements are extracted in order.
/// Extracts from a list of `Option` all the `Some` elements.
/// All the `Some` elements are extracted in order.
/// </summary>
[Pure]
public static IEnumerable<A> Somes<A>(this IEnumerable<Option<A>> self)
Expand All @@ -32,6 +32,26 @@ public static IEnumerable<A> Somes<A>(this IEnumerable<Option<A>> self)
}
}

/// <summary>
/// Extracts from a list of `Option` all the `Some` elements.
/// All the `Some` elements are extracted in order.
/// </summary>
[Pure]
public static Seq<A> Somes<A>(this Seq<Option<A>> self)
{
IEnumerable<A> ToSequence(Seq<Option<A>> items)
{
foreach (var item in items)
{
if (item.IsSome)
{
yield return item.Value;
}
}
}
return Seq(ToSequence(self));
}

/// <summary>
/// Add the bound values of x and y, uses an Add type-class to provide the add
/// operation for type A. For example x.Add<TInteger,int>(y)
Expand Down
17 changes: 15 additions & 2 deletions LanguageExt.Core/DataTypes/OptionAsync/OptionAsync.Extensions.cs
Expand Up @@ -17,8 +17,8 @@
public static partial class OptionAsyncExtensions
{
/// <summary>
/// Extracts from a list of 'Option' all the 'Some' elements.
/// All the 'Some' elements are extracted in order.
/// Extracts from a list of `Option` all the `Some` elements.
/// All the `Some` elements are extracted in order.
/// </summary>
[Pure]
public static async Task<IEnumerable<A>> Somes<A>(this IEnumerable<OptionAsync<A>> self)
Expand All @@ -27,6 +27,19 @@ public static async Task<IEnumerable<A>> Somes<A>(this IEnumerable<OptionAsync<A
var res = await Task.WhenAll(tasks);
return res.Filter(x => x.IsSome).Map(x => x.Value);
}


/// <summary>
/// Extracts from a list of `OptionAsync` all the `Some` elements.
/// All the `Some` elements are extracted in order.
/// </summary>
[Pure]
public static async Task<Seq<A>> Somes<A>(this Seq<OptionAsync<A>> self)
{
var tasks = self.Map(x => x.ToOption()).ToArray();
var res = await Task.WhenAll(tasks);
return res.Filter(x => x.IsSome).Map(x => x.Value).ToSeq();
}

/// <summary>
/// Add the bound values of x and y, uses an Add type-class to provide the add
Expand Down
Expand Up @@ -71,7 +71,7 @@ public static partial class Prelude
/// </summary>
/// <typeparam name="T">T</typeparam>
/// <param name="value">Non-null value to be made OptionAsyncal</param>
/// <returns>OptionAsync<T> in a Some state or throws ValueIsNullException
/// <returns>`OptionAsync<T>` in a Some state or throws ValueIsNullException
/// if isnull(value).</returns>
[Pure]
public static OptionAsync<T> SomeAsync<T>(T value) =>
Expand Down
4 changes: 2 additions & 2 deletions LanguageExt.Core/DataTypes/OptionAsync/OptionAsync.cs
Expand Up @@ -37,10 +37,10 @@ public struct OptionAsync<A> :
public static readonly OptionAsync<A> None = new OptionAsync<A>(OptionDataAsync<A>.None);

/// <summary>
/// Construct an Option of A in a Some state
/// Construct an OptionAsync of A in a Some state
/// </summary>
/// <param name="value">Value to bind, must be non-null</param>
/// <returns>Option of A</returns>
/// <returns>OptionAsync of A</returns>
[Pure]
public static OptionAsync<A> Some(A value) =>
new OptionAsync<A>(new OptionDataAsync<A>(OptionState.Some, value, null));
Expand Down
26 changes: 23 additions & 3 deletions LanguageExt.Core/DataTypes/OptionUnsafe/OptionUnsafe.Extensions.cs
Expand Up @@ -17,8 +17,8 @@
public static class OptionUnsafeExtensions
{
/// <summary>
/// Extracts from a list of 'OptionUnsafe' all the 'Some' elements.
/// All the 'Some' elements are extracted in order.
/// Extracts from a list of `OptionUnsafe` all the `Some` elements.
/// All the `Some` elements are extracted in order.
/// </summary>
[Pure]
public static IEnumerable<A> Somes<A>(this IEnumerable<OptionUnsafe<A>> self)
Expand All @@ -31,7 +31,27 @@ public static IEnumerable<A> Somes<A>(this IEnumerable<OptionUnsafe<A>> self)
}
}
}


/// <summary>
/// Extracts from a list of `OptionUnsafe` all the `Some` elements.
/// All the `Some` elements are extracted in order.
/// </summary>
[Pure]
public static Seq<A> Somes<A>(this Seq<OptionUnsafe<A>> self)
{
IEnumerable<A> ToSequence(Seq<OptionUnsafe<A>> items)
{
foreach (var item in items)
{
if (item.IsSome)
{
yield return item.Value;
}
}
}
return Seq(ToSequence(self));
}

/// <summary>
/// Add the bound values of x and y, uses an Add type-class to provide the add
/// operation for type A. For example x.Add<TInteger,int>(y)
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Core/LanguageExt.Core.csproj
Expand Up @@ -9,7 +9,7 @@
<DefineConstants>COREFX</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<PackageVersion>2.1.11</PackageVersion>
<PackageVersion>2.1.13</PackageVersion>
<PackageId>LanguageExt.Core</PackageId>
<Title>LanguageExt.Core</Title>
<Authors>Paul Louth</Authors>
Expand Down
22 changes: 6 additions & 16 deletions LanguageExt.Core/Utility/IL.cs
Expand Up @@ -368,17 +368,7 @@ public static Func<R> Ctor<R>()
{
// Load constant 16777619
il.Emit(OpCodes.Ldc_I4, 16777619);

if (isValueType)
{
// Load A
il.Emit(OpCodes.Ldarga_S, 0);
}
else
{
// Load A
il.Emit(OpCodes.Ldarg_0);
}
il.Emit(OpCodes.Ldarg_0);

if (field.FieldType.GetTypeInfo().IsValueType)
{
Expand All @@ -392,7 +382,7 @@ public static Func<R> Ctor<R>()
.Where(m => default(EqArray<EqDefault<Type>, Type>).Equals(
m.GetParameters().Map(p => p.ParameterType).ToArray(),
new Type[0]))
.First();
.Head();
il.Emit(OpCodes.Call, method);
}
else
Expand Down Expand Up @@ -423,7 +413,7 @@ public static Func<R> Ctor<R>()
.Where(m => default(EqArray<EqDefault<Type>, Type>).Equals(
m.GetParameters().Map(p => p.ParameterType).ToArray(),
new Type[0]))
.First();
.Head();
il.Emit(OpCodes.Callvirt, method);

il.MarkLabel(useZero);
Expand Down Expand Up @@ -538,7 +528,7 @@ public static Func<R> Ctor<R>()
.Where(m => default(EqArray<EqDefault<Type>, Type>).Equals(
m.GetParameters().Map(p => p.ParameterType).ToArray(),
parms))
.First();
.Head();

il.Emit(OpCodes.Call, defaultMethod);
il.Emit(OpCodes.Ldarg_0);
Expand Down Expand Up @@ -649,7 +639,7 @@ public static Func<R> Ctor<R>()
.Where(m => default(EqArray<EqDefault<Type>, Type>).Equals(
m.GetParameters().Map(p => p.ParameterType).ToArray(),
parms))
.First();
.Head();

il.Emit(OpCodes.Call, defaultMethod);
il.Emit(OpCodes.Ldarg_0);
Expand Down Expand Up @@ -761,7 +751,7 @@ public static Func<R> Ctor<R>()
.Where(m => default(EqArray<EqDefault<Type>, Type>).Equals(
m.GetParameters().Map(p => p.ParameterType).ToArray(),
parms))
.First();
.Head();

il.Emit(OpCodes.Call, defaultMethod);
il.Emit(OpCodes.Ldarg_0);
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.FSharp/LanguageExt.FSharp.csproj
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard1.6;net451</TargetFrameworks>
<PackageVersion>2.1.11</PackageVersion>
<PackageVersion>2.1.13</PackageVersion>
<PackageId>LanguageExt.FSharp</PackageId>
<Title>LanguageExt.FSharp</Title>
<Authors>Paul Louth</Authors>
Expand Down
2 changes: 1 addition & 1 deletion LanguageExt.Parsec/LanguageExt.Parsec.csproj
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<PackageVersion>2.1.11</PackageVersion>
<PackageVersion>2.1.13</PackageVersion>
<PackageId>LanguageExt.Parsec</PackageId>
<Title>LanguageExt.Parsec</Title>
<Authors>Paul Louth</Authors>
Expand Down
3 changes: 3 additions & 0 deletions Samples/TestBed/Program.cs
Expand Up @@ -27,6 +27,9 @@ static void Main(string[] args)
var i = x.ToString();
var j = x > y;
var k = x < z;
var l = x.GetHashCode();
var m = y.GetHashCode();
var n = z.GetHashCode();


Console.WriteLine("Coming soon");
Expand Down

0 comments on commit b0a546e

Please sign in to comment.