鸭子打字属于真正的多形体 [英] Duck typing alows true polymorfisim

查看:68
本文介绍了鸭子打字属于真正的多形体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你想要一个通用的数值算法,比如总和

Ruby


def sum lst

lst .inject(0){|总,电流|总计*当前}

结束


Java //我不知道数字是否有数字超类


class Sum {

public static int sum(int [] lst){

int total = 0;

for(int current: lst){

总计+ =当前;

}

总回报;

}

//重复所有其他数字类型

}

lets say you want a generic numerical algorithom like sum

Ruby

def sum lst
lst.inject(0){|total,current| total*current}
end

Java // i dont know if there is a numeric super class for numbers

class Sum{
public static int sum(int[] lst){
int total = 0;
for(int current : lst){
total+=current;
}
return total;
}
// repeat for all other number types
}

推荐答案

在******* @ aol.com 写道:

let比方说你想要一个通用的数值算法


Ruby


def sum lst

lst.inject(0 ){|总,电流|总计*当前}

结束


Java //我不知道数字是否有数字超类


class Sum {

public static int sum(int [] lst){

int total = 0;

for(int current: lst){

总计+ =当前;

}

总回报;

}

//重复所有其他数字类型

}
lets say you want a generic numerical algorithom like sum

Ruby

def sum lst
lst.inject(0){|total,current| total*current}
end

Java // i dont know if there is a numeric super class for numbers

class Sum{
public static int sum(int[] lst){
int total = 0;
for(int current : lst){
total+=current;
}
return total;
}
// repeat for all other number types
}



你的问题是什么? (或者,如果没有问题,请点?):-)


完全偏离主题(实际上是off-list......)真的是如何红宝石

总结清单?这是如何运作的?不是'''''意味着倍增吗?和

管道符号是什么? (请随意忽略这些问题。如果我很好奇,我应该自己去查看...)


和平,

~Simon

What''s your question? (Or, if no question, point?) :-)

Totally off topic (and indeed "off-list"..) is that really how ruby
sums a list? How does that work? Doesn''t the ''*'' mean multiply? and
what are the pipe symbols for? (Feel free to ignore these questions. I
should really go look it up myself if I''m so curious..)

Peace,
~Simon


at * ******@aol.com 写道:
at*******@aol.com wrote:

假设你想要一个通用的数值算法,例如sum
lets say you want a generic numerical algorithom like sum



嗯,我以为你要宣布你是Xah Lee的第一个出生的儿子。实际上你做到了!


-

John MexIT: http://johnbokma.com/mexit/

个人页面: http://johnbokma.com/

经验丰富的程序员可用: http://castleamber.com/

快乐的客户: http://castleamber.com/testimonials.html

Hmmm, I thought you were going to announce that you were the first born
son of Xah Lee. Actually you did!

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html



在******* @ aol.com 写道:

假设你想要一个通用数值算法总和


Ruby


def sum lst

lst.inject(0){| total ,当前|总计*当前}

结束


Java //我不知道数字是否有数字超类


class Sum {

public static int sum(int [] lst){

int total = 0;

for(int current: lst){

总计+ =当前;

}

总回报;

}

//重复所有其他数字类型

}
lets say you want a generic numerical algorithom like sum

Ruby

def sum lst
lst.inject(0){|total,current| total*current}
end

Java // i dont know if there is a numeric super class for numbers

class Sum{
public static int sum(int[] lst){
int total = 0;
for(int current : lst){
total+=current;
}
return total;
}
// repeat for all other number types
}



还有什么需要添加的吗?你的问题不是很清楚。你在询问如何在Python中执行更高阶的程序吗?这里的总和为高阶

程序......它将a到b的数字相加(或任何可以比较和添加的b $ b的数字) ;<"和+)。


def sum(term,next,a,b):

if(ab):

返回0

否则:

返回(term(a)+(Sum(term,next,next(a),b)


这个函数有两个函数(term和next)和两个对象(

我们的案例编号)代表总和的范围。现在让's说

你想要添加数字1到10.总和的术语

只是数字,所以术语函数只是一个

函数叫做身份,可以定义为


def identity(x):

返回x


为了获得下一个学期,我们每次只添加一个(即1,2,3,

4,...)。所以我们的下一个功能是


def增量(x):

返回x + = 1


然后我们称Sum(身份,增量,1,10),我们得到1 + 2 + 3 + 4

+等等。


现在如果你想要1到10的CUBES之和怎么办?很简单,我们

用函数cube(x)替换term函数,返回x * x * x。


Sum(cube,increment,1, 10)


因此我们求和中的每个项都是立方体,但我们仍然每次增加一个
,所以我们得到(1 ^ 3 + 2 ^ 3 + 3 ^ 3 + 4 ^ 3 + ...)


同样我们可以改变下一个功能跳过某个数字,遇见

某些要求,做一些转变,无论如何。事实上,

python接受并使用函数作为其他函数的参数是

在功率方面非常棒,但它可以让人有点难以置信
$ b有时是$ b。


Any more to add? Your question is not exactly clear. Are you asking how
to do higher order procedures in Python? Here''s sum as a higher-order
procedure... It sums the numbers from a to b (or any thing that can be
compared and added with "<" and "+").

def Sum(term, next, a, b):
if(a b):
return 0
else:
return (term(a) + (Sum(term, next, next(a), b)

This function takes two functions (term and next) and two objects (in
our case numbers) representing the range to sum across. Now let''s say
you want to add the numbers 1 through 10. The term for the summation
would just be the number, so the term function would simply be a
function called identity, and could be defined as

def identity(x):
return x

To get the next term, we are just adding one each time (i.e. 1, 2, 3,
4, ...). So our next function is

def increment(x):
return x += 1

Then we call Sum(identity, increment, 1, 10), and we get 1 + 2 + 3 + 4
+ and so on.

Now what if you wanted the sum of the CUBES of 1 through 10? Easy, we
replace the term function with a function cube(x) that returns x*x*x.

Sum(cube,increment,1,10)

Hence each term in our summation is the cube, but we are still
incrementing by one each time, so we get (1^3 + 2^3 + 3^3 + 4^3 + ...)

Similarly we can change the next function to skip certain number, meet
certain requirement, do some tranformation, whatever. The fact that
python accept and uses functions as parameters to other functions is
awesome in terms of power, but it can get a little mind boggling
sometimes.


这篇关于鸭子打字属于真正的多形体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆