扩展 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. 【转载】about slack

    About Slack slack is the difference b/w the REQUIRED TIME and the ARRIVAL TIME. 1.WHAT IS SLACK WITH ...

  2. linux内核打印"BUG: scheduling while atomic

    linux内核打印"BUG: scheduling while atomic"和"bad: scheduling from the idle thread"错误 ...

  3. Postgresql 存储过程调试 1

    看来人真的有些力不从心,半个月前还很得意掌握的简单的Postgresql 存储过程的调试,一段时间没使用,做新功能就忘了! Postgresql 在开源的数据库里面算是很强悍的了,但现在就是不方便调试 ...

  4. responsive layout

    http://cssdeck.com/labs/7wsdvxdc http://getbootstrap.com/css/ http://getbootstrap.com/2.3.2/scaffold ...

  5. linux - 自动删除n天前日志

    1.删除文件命令: find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; 实例命令: find /opt/soft/log/ -m ...

  6. IOS常用加密DES

    NSString+DES.h // // NSString+DES.h // haochang // // Created by Administrator on 14-4-15. // Copyri ...

  7. Jenkins-测试自动化环境搭建(Python+RobotFramework+selenium)

    下载插件: Python:https://wiki.jenkins-ci.org/display/JENKINS/Python+Plugin RobotFramework:https://wiki.j ...

  8. python-面向对象(股票对象举例)

    股票对象实例 class Stock(object): def __init__(self,stockCode ,stockName,averagePrice_yesterday,averagePri ...

  9. Oracle创建表时涉及的参数解析

    1.oracle pctfree和pctused详解   http://www.cnblogs.com/linjiqin/archive/2012/01/16/2323320.html http:// ...

  10. Codeforces Round #278 (Div. 2)

    题目链接:http://codeforces.com/contest/488 A. Giga Tower Giga Tower is the tallest and deepest building ...