Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Mar 3, 2020
2 parents cf52633 + a4bd302 commit 42a9b07
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 132 deletions.
2 changes: 0 additions & 2 deletions LanguageExt.Benchmarks/ListIterationBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class ListIterationBenchmarks<T, TOrd>
public int N;

T[] values;

ImmutableList<T> immutableList;
Lst<T> lst;
Seq<T> seq;
Expand All @@ -25,7 +24,6 @@ public class ListIterationBenchmarks<T, TOrd>
public void Setup()
{
values = ValuesGenerator.Default.GenerateUniqueValues<T>(N);

immutableList = ImmutableList.CreateRange(ValuesGenerator.Default.GenerateUniqueValues<T>(N));
lst = ValuesGenerator.Default.GenerateUniqueValues<T>(N).Freeze();
seq = Seq(ValuesGenerator.Default.GenerateUniqueValues<T>(N)).Strict();
Expand Down
2 changes: 0 additions & 2 deletions LanguageExt.Core/DataTypes/Seq/ISeqInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ internal interface ISeqInternal<A> : IEnumerable<A>
S FoldBack<S>(S state, Func<S, A, S> f);
ISeqInternal<A> Skip(int amount);
ISeqInternal<A> Take(int amount);
ISeqInternal<A> TakeWhile(Func<A, bool> pred);
ISeqInternal<A> TakeWhile(Func<A, int, bool> pred);
ISeqInternal<A> Strict();
ISeqInternal<A> Filter(Func<A, bool> f);
ISeqInternal<B> Map<B>(Func<A, B> f);
Expand Down
30 changes: 26 additions & 4 deletions LanguageExt.Core/DataTypes/Seq/Seq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,18 @@ public Seq<A> Take(int amount) =>
/// </summary>
/// <returns>A new sequence with the first items that match the
/// predicate</returns>
public Seq<A> TakeWhile(Func<A, bool> pred) =>
new Seq<A>(Value.TakeWhile(pred));
public Seq<A> TakeWhile(Func<A, bool> pred)
{
return new Seq<A>(new SeqLazy<A>(Yield(Value, pred)));
IEnumerable<A> Yield(IEnumerable<A> xs, Func<A, bool> f)
{
foreach (var x in xs)
{
if (!f(x)) break;
yield return x;
}
}
}

/// <summary>
/// Iterate the sequence, yielding items if they match the predicate
Expand All @@ -835,8 +845,20 @@ public Seq<A> TakeWhile(Func<A, bool> pred) =>
/// </summary>
/// <returns>A new sequence with the first items that match the
/// predicate</returns>
public Seq<A> TakeWhile(Func<A, int, bool> pred) =>
new Seq<A>(Value.TakeWhile(pred));
public Seq<A> TakeWhile(Func<A, int, bool> pred)
{
return new Seq<A>(new SeqLazy<A>(Yield(Value, pred)));
IEnumerable<A> Yield(IEnumerable<A> xs, Func<A, int, bool> f)
{
var i = 0;
foreach (var x in xs)
{
if (!f(x, i)) break;
yield return x;
i++;
}
}
}

/// <summary>
/// Compare to another sequence
Expand Down
27 changes: 0 additions & 27 deletions LanguageExt.Core/DataTypes/Seq/SeqConcat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,6 @@ IEnumerable<A> Yield()
}
return new SeqLazy<A>(Yield());
}
public ISeqInternal<A> TakeWhile(Func<A, bool> pred)
{
return new SeqLazy<A>(Yield());
IEnumerable<A> Yield()
{
foreach (var item in this)
{
if (!pred(item)) break;
yield return item;
}
}
}

public ISeqInternal<A> TakeWhile(Func<A, int, bool> pred)
{
return new SeqLazy<A>(Yield());
IEnumerable<A> Yield()
{
int i = 0;
foreach (var item in this)
{
if (!pred(item, i)) break;
yield return item;
i++;
}
}
}

IEnumerator IEnumerable.GetEnumerator()
{
Expand Down
6 changes: 0 additions & 6 deletions LanguageExt.Core/DataTypes/Seq/SeqEmptyInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ public ISeqInternal<A> Strict() =>

public ISeqInternal<A> Take(int amount) =>
this;

public ISeqInternal<A> TakeWhile(Func<A, bool> pred) =>
this;

public ISeqInternal<A> TakeWhile(Func<A, int, bool> pred) =>
this;

public IEnumerator<A> GetEnumerator()
{
Expand Down
28 changes: 0 additions & 28 deletions LanguageExt.Core/DataTypes/Seq/SeqLazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,34 +398,6 @@ public ISeqInternal<A> Take(int amount)
}
}

public ISeqInternal<A> TakeWhile(Func<A, bool> pred)
{
return new SeqLazy<A>(Yield());
IEnumerable<A> Yield()
{
foreach (var item in this)
{
if (!pred(item)) break;
yield return item;
}
}
}

public ISeqInternal<A> TakeWhile(Func<A, int, bool> pred)
{
return new SeqLazy<A>(Yield());
IEnumerable<A> Yield()
{
int i = 0;
foreach (var item in this)
{
if (!pred(item, i)) break;
yield return item;
i++;
}
}
}

public IEnumerator<A> GetEnumerator()
{
int nstart = data.Length - count;
Expand Down
63 changes: 0 additions & 63 deletions LanguageExt.Core/DataTypes/Seq/SeqStrict.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,69 +362,6 @@ public ISeqInternal<A> Take(int amount) =>
amount < count
? new SeqStrict<A>(data, start, amount, NoCons, NoAdd)
: this;

/// <summary>
/// Iterate the sequence, yielding items if they match the predicate
/// provided, and stopping as soon as one doesn't
/// </summary>
/// <returns>A new sequence with the first items that match the
/// predicate</returns>
public ISeqInternal<A> TakeWhile(Func<A, bool> pred)
{
var data = new A[DefaultCapacity];
var index = HalfDefaultCapacity;

foreach (var item in this)
{
if (pred(item))
{
if (index == data.Length)
{
var ndata = new A[Math.Max(1, data.Length << 1)];
System.Array.Copy(data, ndata, data.Length);
data = ndata;
}

data[index] = item;
index++;
}
}
return index == HalfDefaultCapacity
? SeqEmptyInternal<A>.Default
: new SeqStrict<A>(data, HalfDefaultCapacity, index - HalfDefaultCapacity, 0, 0);
}

/// <summary>
/// Iterate the sequence, yielding items if they match the predicate
/// provided, and stopping as soon as one doesn't. An index value is
/// also provided to the predicate function.
/// </summary>
/// <returns>A new sequence with the first items that match the
/// predicate</returns>
public ISeqInternal<A> TakeWhile(Func<A, int, bool> pred)
{
var data = new A[DefaultCapacity];
var index = HalfDefaultCapacity;

foreach (var item in this)
{
if (pred(item, index))
{
if (index == data.Length)
{
var ndata = new A[Math.Max(1, data.Length << 1)];
System.Array.Copy(data, ndata, data.Length);
data = ndata;
}

data[index] = item;
index++;
}
}
return index == HalfDefaultCapacity
? SeqEmptyInternal<A>.Default
: new SeqStrict<A>(data, HalfDefaultCapacity, index - HalfDefaultCapacity, 0, 0);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ISeqInternal<A> Strict() =>
Expand Down
2 changes: 2 additions & 0 deletions Samples/TestBed/TestCodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ from _ in FreeIO.WriteAllText("I:\\temp\\test2.txt", t)
Fail<A> (var error) => error,
ReadAllText<A> (var path, var next) => Interpret(next(Read(path))),
WriteAllText<A> (var path, var text, var next) => Interpret(next(Write(path, text))),
_ => throw new NotSupportedException()
};

static string Read(string path) =>
Expand All @@ -76,6 +77,7 @@ static Unit Write(string path, string text)
Fail<A> (var error) => await Task.FromException<A>(error),
ReadAllText<A> (var path, var next) => await InterpretAsync(next(await File.ReadAllTextAsync(path))),
WriteAllText<A> (var path, var text, var next) => await InterpretAsync(next(await File.WriteAllTextAsync(path, text).ToUnit())),
_ => throw new NotSupportedException()
};
}

Expand Down

0 comments on commit 42a9b07

Please sign in to comment.