扩展 delphi 泛型 以实现类似lambda功能 , C#中的any count first last 等扩展方法

在C#中对泛型的扩展,输入参数是泛型本身的内容,返回值则是bool.基于这一点,开始构造在delphi下如何实现.

首先
1.delphi 是支持匿名函数的其语法为:
名称 = reference to 函数类型定义
例如:
TFun = reference to function(const num: Integer): Integer;

2.对泛型的扩展的语法如下:
TList<T> = class(Generics.Collections.TList<T>) 
public
***********
end;

有了如上的两点,便可以实现我们想要的功能了.其主要思路为:
为泛型扩展的any count方法添加类型为"Ibool接口"的参数.Ibool接口类型的参数就是泛型本身的内容,返回值则是Boolean

在调用这些扩展时,只需要构造出一个支持Ibool接口的对像就可以了.

对于Ibool接口,支持Ibool接口的对像,any等扩展方法之间的关系请细细考虑.对高手来说很简单了吧,就是一个简单的设计模式.

主要代码,注释如下:
unit UCryHelper;

interface
uses
Generics.Collections;
type
IBool<T>=interface //这就是Ibool接口的定义
function Compare(Right: T): Boolean;
end;

TBoolsion<T> = reference to function( Right: T): Boolean;//匿名函数 因为是对泛型的支持所以要有那个"T". 这个的类型与Ibool接口方法的类型是一样的,定义成这样后,用这个匿名函数连接你写的实际的函数体和泛型

TBool<T>=class(TInterfacedObject, IBool<T>) //这是支持IBool接口的对像,这是虚函数,实际的执行者是下边的定义.
public
class function Construct(const EqualityComparison: TBoolsion<T>): IBool<T>;
function Compare(Right: T): Boolean;virtual; abstract;
end;

TDelegatedBool<T> = class(TBool<T>) //前三个类型是必备的类型,这行则是实际的执行者.
private
FEquals: TBoolsion<T>;
public
constructor Create(const AEquals: TBoolsion<T>);
function Compare(Right: T): Boolean; overload; override;
end;

TList<T> = class(Generics.Collections.TList<T>) //这是对泛型的扩展,不用多说了.
public
type
TDynArray = array of T;
function ToArray: TDynArray;
function Any(const AComparer: IBool<T>):Boolean; //扩展方法Any的参数为IBool<T>返回值是Boolean
end;
implementation

function TList<T>.Any(const AComparer: IBool<T>): Boolean;
var
one:T;
begin
Result:=False;
for one in Self do
begin
if AComparer.Compare(one) then
begin
Result:=True;
Exit;
end;
end;
end;

function TList<T>.ToArray: TDynArray;
var
I: Integer;
begin
SetLength(Result, self.Count);
for I := 0 to Self.Count - 1 do
begin
Result[I] := Self[I];
end;
end;
{ TBool<T> }

class function TBool<T>.Construct(const EqualityComparison: TBoolsion<T>): IBool<T>;
begin
Result:= TDelegatedBool<T>.Create(EqualityComparison);
end;

{ TDelegatedBool<T> }

function TDelegatedBool<T>.Compare(Right: T): Boolean;
begin
Result := FEquals(Right);
end;

constructor TDelegatedBool<T>.Create(const AEquals: TBoolsion<T>);
begin
FEquals:= AEquals;
end;
end.

最终的调用就是这样用了.先use UCryHelper 然后

procedure TfrmSampleInput.btnQuitClick(Sender: TObject);
var
listint:TList<Integer>;
boolCal:IBool<Integer>;
begin
inherited;
listint:=TList<Integer>.Create;
listint.Add(1);
listint.Add(33);
listint.Add(3);
listint.Add(4);

boolCal:=TBool<Integer>.Construct(
function ( Avalue: integer): Boolean
begin
result := Avalue=listint.Count;
end
);
if(listint.Any(boolCal)) then
ShowMessage('OOKK!!');
{如果想把boolCal:IBool<Integer>;的声明省了,这样用也是可以的,怎么样?和C#里的any方法差不多吧.
if listint.Any(Tbool<Integer>.Construct(function(Avalue:Integer):Boolean
begin
result := Avalue=listint.Count;
end
)) then
ShowMessage('OOKK!!');}
listint.Free;
end;

以上就是一个扩展any的实现,有了这个any,再扩展count first last等其它方法还不就简单了?快试试吧.

http://www.cnblogs.com/ttgss/p/3252469.html

扩展 delphi 泛型 以实现类似lambda功能 , C#中的any count first last 等扩展方法的更多相关文章

  1. Delphi泛型动态数组的扩展--转贴

    此文章转载于http://www.raysoftware.cn/?p=278&tdsourcetag=s_pcqq_aiomsg的博客 从Delphi支持泛型的第一天起就有了一种新的动态数组类 ...

  2. Swift 学习笔记(扩展和泛型)

    在开始介绍Swift中的扩展之前,我们先来回忆一下OC中的扩展. 在OC中如果我们想对一个类进行功能的扩充,我们会怎么做呢. 对于面向对象编程的话,首先会想到继承,但是继承有两个问题. 第一个问题:继 ...

  3. 用setTimeout实现与setInteval类似的功能

    用setTimeout实现与setInteval类似的功能,代码如下: (function(){ var self = arguments.callee; //获取函数本身 count++; if ( ...

  4. 【转载】ASP.NET以Post方式抓取远程网页内容类似爬虫功能

    使用HttpWebRequest等Http相关类,可以在应用程序中或者网站中模拟浏览器发送Post请求,在请求带入相应的Post参数值,而后请求回远程网页信息.实现这一功能也很简单,主要是依靠Http ...

  5. delphi 泛型 c++builder 泛型

    delphi 泛型 System.Generics.Collections.pas TList<T> http://docwiki.embarcadero.com/Libraries/Be ...

  6. C# 通过IEnumberable接口和IEnumerator接口实现泛型和非泛型自定义集合类型foreach功能

    IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环,如 ...

  7. iOS开发学习-类似微信聊天消息中的电话号码点击保存到通讯录中的功能

    类似微信聊天消息中的电话号码点击保存到通讯录中的功能,ABAddress的实现在iOS9中是不能正常使用的,点击完成后,手机会非常的卡,iOS9之后需要使用Contact新提供的方法来实现该功能.快捷 ...

  8. js扩展String.prototype.format字符串拼接的功能

    1.题外话,有关概念理解:String.prototype 属性表示 String原型对象.所有 String 的实例都继承自 String.prototype. 任何String.prototype ...

  9. Delphi xe7 up1 调用android振动功能

    Delphi xe7 up1 调用android振动功能 振动用到以下4个单元: Androidapi.JNI.App,Androidapi.JNIBridge,Androidapi.JNI.Os,A ...

随机推荐

  1. java笔试题(1)

    char型变量中能不能存贮一个中文汉字? char型变量是用来存储Unicode编码的字符的,unicode编码字符集中包含了汉字,所以,char型变量中当然可以存储汉字啦.不过,如果某个特殊的汉字没 ...

  2. Careercup - Google面试题 - 5162732873580544

    2014-05-08 08:26 题目链接 原题: Given a preorder traversal, create a binary search tree in optimized time ...

  3. 【BZOJ】【3171】【TJOI2013】循环格

    网络流/费用流 最后能走回出发点……说明全部是环= = 而二分图上的环说明什么呢……完备匹配 对于每个点,它都有四个可能的匹配点,且已知它已经(伪)匹配的一个点,那么我们于已知每条(伪)匹配边,我们连 ...

  4. 使用NPOI和线程池快速加载EXCEL数据

    private void FilterData() { List<Task> tasks = new List<Task>(); IWorkbook workbook = Cs ...

  5. 重启nginx后丢失nginx.pid解决

    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  6. C语言预处理命令

    1.#error Directive (C/C++) The #error directive emits a user-specified error message at compile time ...

  7. 企业级账号更新app

    企业级账号 版本更新总结       参考:http://jingyan.baidu.com/article/a3aad71aa5fbfbb1fb0096b1.html 1.打包ipa,plist工具 ...

  8. shell 运算

    一个下午折腾一个脚本,shell好久不用,重新学起 一个小成果 size= ] do table=albums_index_${table_num} count=$size times= while ...

  9. no module named staticfiles

    原地址:http://blog.sina.com.cn/s/blog_77500e110100umms.html 今天想试一下django的Uploadify,找了个例子,运行时出错:myprojec ...

  10. Ext Js学习之IIS理解

    站点分为静态网站和动态网站,纯粹利用html编写的网站属于静态网站,不宜维护和更新而利用C#+extjs等前台+后台技术编写的网站就属于动态站点,有更多的交互,易维护和更新,比如降价的页面,利用htm ...