扩展 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. c# 各种排序算法+找第二大的数+句子单词反转

    冒泡排序 // 冒泡排序 bubble sort public static int[] BubbleSort(int []array) { bool isContinue = true; ; i & ...

  2. mysql查询重复

    select * from tablename where id in (    select id from tablename    group by id    having count(id) ...

  3. Introduction to Haskell

    "I know why you're here. ...why you hardly sleep, why night after night, you sit by your comput ...

  4. 水王ID查找

    一. 题目 1 三人行设计了一个灌水论坛.信息学院的学生都喜欢在上面交流灌水,传说在论坛上有一个“水王”,他不但喜欢发帖,还会回复其他ID发的每个帖子.坊间风闻该“水王”发帖数目超过了帖子数目的一半. ...

  5. 原生javascript开发仿微信打飞机小游戏

    今天闲来无事,于是就打算教一个初学javascript的女童鞋写点东西,因此为了兼顾趣味性与简易程度,果断想到了微信的打飞机小游戏.. 本来想用html5做的,但是毕竟人家才初学,连jquery都还不 ...

  6. WinForm Control - DataGridView

    http://blog.csdn.net/fangxing80/article/details/1561011 .NET 2.0 - WinForm Control - DataGridView 编程 ...

  7. How to find and fix Bash Shell-shock vulnerability CVE-2014-6271 in unix like system

    type command - env x='() { :;}; echo vulnerable' bash -c 'echo hello' in your terminal.   if your sy ...

  8. 关于myeclipse代码提示的一些问题

    默认是  .xxx  输入点提示,要写注释 @xxx的时候怎么输入@后面有代码提示呢? Auto activation delay 是代码提示出现的速度  下面一行是出现代码提示的条件 我们在.后面加 ...

  9. 802.11 wireless 七

    802.11 wireless 7Wireless Fundamentals : End-to-End Discovering the NetworkGetting Connect Clients i ...

  10. 【CentOs】配置nginx

    参考资料:http://nginx.org/en/linux_packages.html#stable 1.添加nginx.repo 2.配置nginx 3.启动nginx 1.添加nginx.rep ...