2013-12-09 50 views
0

Groovy對於簡單的clojures具有很好的語法,它不需要明確地指定 單個參數。它將被命名爲它默認爲Groovy的Python模擬器嗎?

def closure = { print it } 
closure("hi there") //prints "hi there" 

什麼是模擬 magikal參數在Python中的最佳方式?我想要什麼

例子:

>>> it = It() 
>>> print map(it + 5, [1, 2, 3]) 
[6, 7, 8] 
+1

那麼,什麼是預期的輸入和輸出? –

+3

不,Python沒有針對lambda函數或函數的隱式「第一個參數」。顯式比隱式更好。 –

+0

@MartijnPieters我當然知道,Python無法做到這一點。我正在尋找一些技巧來做類似的事情。 – uhbif19

回答

1

到目前爲止,我已經找到了我的問題確切的anwser。這是實現_ magikal變量和一些可用的函數式編程概念的庫fn.py。使用

例子:

>>> from fn import _ 
>>> list(map(_ * 2, range(5))) 
[0,2,4,6,8] 
3

你只要給你的拉姆達或功能的明確第一個參數:

lambda it: ' {}'.format(it) 

這正好與Zen of Python

顯式優於隱式

0

我的第一個thougt是做一些magikal 對象與生產curryied功能瑪姬方法。

class It : 
    def __add__(self, another_one) : 
     return lambda x: x + another_one 



it = It() 
print map(it + 5, [1, 2, 3])