English 中文(简体)
Examples of useful or non-trival dual interfaces
原标题:

Recently Erik Meijer and others have show how IObservable/IObserver is the dual of IEnumerable/IEnumerator. The fact that they are dual means that any operation on one interface is valid on the other, thus providing a theoretical foundation for the Reactive Extentions for .Net

Do other dual interfaces exist? I m interested in any example, not just .Net based.

最佳回答

Another example would be TextReader and TextWriter, though there is even more noise than in case of observables and enumerables. In principle the type signatures would be:

interface ITextReader {
  // Read: void -> int
  int Read();
}

interface ITextWriter {
  // Write: int -> void
  void Write(int val);
}
问题回答

Another example is the product type A.B and the sum type A+B of two types A and B. In Haskell you can write them as:

data Prod a b = P a b -- this is the same as the pair type (a,b)

data Sum a b = Left a | Right b -- the same as the Either a b type

check here for details

Covariance and contravariance is another example. I think. I could be wrong.

Bart De Smet says: "Lots of dualities exist in various disciplines, providing for great knowledge transfers between different domains. For example, in formal logic, De Morgan’s law allows converting expressions built from conjunctions into ones built from disjunctions, and vice versa. In electronics, similarities exist between the behavior of capacitors and inductances: know one and how to go back and forth between domains, and you know the other. Fourier calculus provides duals between time and frequency domains." Interesting.

They also call System.Reactive the dual of System.Interactive. So most of the functions in one of the assemblies has it s dual in the other. To clarify, it s not just that IO is the dual of IE, but the functions that operate on them are also dualized.

So to answer your question, many dualized interfaces exist. You can dualize any interface. You just swap inputs and outputs and the direction of the function. Some won t be useful, or will be the dual of themselves. However, sometimes there are really powerful ones hiding waiting to be uncovered.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签