Kỹ thuật khác, trực tiếp từ lập trình hàm, như sau. Xác định một struct IO như thế này:
/// <summary>TODO</summary>
public struct IO<TSource> : IEquatable<IO<TSource>> {
/// <summary>Create a new instance of the class.</summary>
public IO(Func<TSource> functor) : this() { _functor = functor; }
/// <summary>Invokes the internal functor, returning the result.</summary>
public TSource Invoke() => (_functor | Default)();
/// <summary>Returns true exactly when the contained functor is not null.</summary>
public bool HasValue => _functor != null;
X<Func<TSource>> _functor { get; }
static Func<TSource> Default => null;
}
và biến nó thành một đơn nguyên LINQ-thể với những phương pháp khuyến nông:
[SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces")]
public static class IO {
public static IO<TSource> ToIO<TSource>(this Func<TSource> source) {
source.ContractedNotNull(nameof(source));
return new IO<TSource>(source);
}
public static IO<TResult> Select<TSource,TResult>(this IO<TSource> @this,
Func<TSource,TResult> projector
) =>
@this.HasValue && projector!=null
? New(() => projector(@this.Invoke()))
: Null<TResult>();
public static IO<TResult> SelectMany<TSource,TResult>(this IO<TSource> @this,
Func<TSource,IO<TResult>> selector
) =>
@this.HasValue && selector!=null
? New(() => selector(@this.Invoke()).Invoke())
: Null<TResult>();
public static IO<TResult> SelectMany<TSource,T,TResult>(this IO<TSource> @this,
Func<TSource, IO<T>> selector,
Func<TSource,T,TResult> projector
) =>
@this.HasValue && selector!=null && projector!=null
? New(() => { var s = @this.Invoke(); return projector(s, selector(s).Invoke()); })
: Null<TResult>();
public static IO<TResult> New<TResult> (Func<TResult> functor) => new IO<TResult>(functor);
private static IO<TResult> Null<TResult>() => new IO<TResult>(null);
}
và bây giờ bạn có thể sử dụng LINQ toàn diện cú pháp như sau:
using Xunit;
[Fact]
public static void IOTest() {
bool isExecuted1 = false;
bool isExecuted2 = false;
bool isExecuted3 = false;
bool isExecuted4 = false;
IO<int> one = new IO<int>(() => { isExecuted1 = true; return 1; });
IO<int> two = new IO<int>(() => { isExecuted2 = true; return 2; });
Func<int, IO<int>> addOne = x => { isExecuted3 = true; return (x + 1).ToIO(); };
Func<int, Func<int, IO<int>>> add = x => y => { isExecuted4 = true; return (x + y).ToIO(); };
var query1 = (from x in one
from y in two
from z in addOne(y)
from _ in "abc".ToIO()
let addOne2 = add(x)
select addOne2(z)
);
Assert.False(isExecuted1); // Laziness.
Assert.False(isExecuted2); // Laziness.
Assert.False(isExecuted3); // Laziness.
Assert.False(isExecuted4); // Laziness.
int lhs = 1 + 2 + 1;
int rhs = query1.Invoke().Invoke();
Assert.Equal(lhs, rhs); // Execution.
Assert.True(isExecuted1);
Assert.True(isExecuted2);
Assert.True(isExecuted3);
Assert.True(isExecuted4);
}
Khi người ta muốn một đơn nguyên IO soạn nhưng chỉ trả về void, hãy xác định cấu trúc này và phụ thuộc phương pháp ent:
public struct Unit : IEquatable<Unit>, IComparable<Unit> {
[CLSCompliant(false)]
public static Unit _ { get { return _this; } } static Unit _this = new Unit();
}
public static IO<Unit> ConsoleWrite(object arg) =>
ReturnIOUnit(() => Write(arg));
public static IO<Unit> ConsoleWriteLine(string value) =>
ReturnIOUnit(() => WriteLine(value));
public static IO<ConsoleKeyInfo> ConsoleReadKey() => new IO<ConsoleKeyInfo>(() => ReadKey());
mà dễ dàng cho phép bằng văn bản về các đoạn mã như thế này:
from pass in Enumerable.Range(0, int.MaxValue)
let counter = Readers.Counter(0)
select (from state in gcdStartStates
where _predicate(pass, counter())
select state)
into enumerable
where (from _ in Gcd.Run(enumerable.ToList()).ToIO()
from __ in ConsoleWrite(Prompt(mode))
from c in ConsoleReadKey()
from ___ in ConsoleWriteLine()
select c.KeyChar.ToUpper() == 'Q'
).Invoke()
select 0;
nơi cũ C dấu phẩy hành điểm dễ nhận ra cho những gì nó là: a monadic soạn hoạt động.
Các công đức thực sự của cú pháp hiểu rõ ràng khi một nỗ lực để viết rằng đoạn theo phong cách flunt:
(Enumerable.Range(0,int.MaxValue)
.Select(pass => new {pass, counter = Readers.Counter(0)})
.Select(_ => gcdStartStates.Where(state => _predicate(_.pass,_.counter()))
.Select(state => state)
)
).Where(enumerable =>
((Gcd.Run(enumerable.ToList())).ToIO()
.SelectMany(_ => ConsoleWrite(Prompt(mode)),(_,__) => new {})
.SelectMany(_ => ConsoleReadKey(), (_, c) => new {c})
.SelectMany(_ => ConsoleWriteLine(), (_,__) => _.c.KeyChar.ToUpper() == 'Q')
).Invoke()
).Select(list => 0);
List.Add không trả lại một danh sách mới nhưng chỉ chỉnh sửa lại nó tại chỗ. Theo nghĩa này, nó không hoạt động. –