簡體   English   中英

如何在函數式編程中用於循環?

[英]How to use for cycles in functional programming?

晚上好,我想請社區提供幫助以了解以下內容,我一直在嘗試使用函數編程方法,並且發現了諸如map () reduce () filter ()類的基本功能,但是我想要什么知道函數式編程中for cycles會發生什么。

例如,在這段代碼中,我必須執行許多for周期才能找到結果:

test = {'one':1,'two':{'two-two':{'two-two-two':222,'three-three-three':333}},'three':3}

for i in test.items():
    if i[0]=="two":
        for s in i[1].items():
            if s[0] == "two-two":
                for a in s[1].items():
                    print(a[1])

基本上我不知道在這種情況下會用什么,它可能是map ()filter ()或其他東西,我希望您能對我有所幫助

我得到了,這里您已經使用函數式編程術語來介紹代碼中map()filter()reduce()的用法,但是在這種情況下,您不應該在此使用它,因為函數式編程是通過以下方式實現問題的使用功能(模塊化設計)。

在您的情況下,您不能使用filter()reduce()來獲得預期的結果,因為這些函數不能為您提供一種靈活的方式來控制程序的控件。

您可以嘗試類似的方法,但我不希望您使用該方法,如果在map()的情況下不滿足條件,則可能會得到None 使用filter() / reduce()沒有意義。

在這里,我試圖使其按預期工作。

>>> def f(tup):
...     items = []
...     if tup[0] == 'two':
...         for s in tup[1].items():
...             if s[0] == "two-two":
...                 for a in s[1].items():
...                     print(a[1])
...                     items.append(a[1])
...         return items
...     else:
...         return None
...
>>> test
{'one': 1, 'two': {'two-two': {'two-two-two': 222, 'three-three-three': 333}},
'three': 3}
>>>
>>> out = list(map(f, test.items()))
222
333
>>> out
[None, [222, 333], None]
>>>
>>> out[1]
[222, 333]
>>>

map()filter()通常用於處理可迭代項(例如列表,元組,字典,集合等),並通過對項目執行操作來生成另一個可迭代項。 filter()允許我們過濾數據(從列表中選擇偶數)。

reduce()通常用於處理可迭代對象並將其減少為單個值(例如,獲取數字列表的總和)。

初始化

>>> l = [9, 4, 3, 2, 1]
>>> d = {'first': 'Rishikesh', 'last': 'Agrawani'}
>>> t = (3, 4, 5)
>>>

使用map()

>>> # Using map()
...
>>> map_obj = map(lambda n: n ** 2, l)
>>> map_obj
<map object at 0x0000016DAF88B6D8>
>>>
>>> squares = list(map_obj)  # list(map(lambda n: n ** 2, l))
>>> squares
[81, 16, 9, 4, 1]
>>>
>>> details = {k + '-name': v for k, v in d.items()}
>>> details
{'first-name': 'Rishikesh', 'last-name': 'Agrawani'}
>>>
>>> details = dict(map(lambda tup: (tup[0] + '_name', tup[1]), d.items()))
>>> details
{'first_name': 'Rishikesh', 'last_name': 'Agrawani'}
>>>

使用filter()

>>> # Using filter() - let's filter even numbers from list
...
>>> filter_obj = filter(lambda n: n % 2 == 0, l)
>>> filter_obj
<filter object at 0x0000016DAF88B908>
>>>
>>> evens = list(filter_obj)
>>> evens
[4, 2]
>>>

使用reduce()

>>> # Using reduce() - finding sum of al numbers in a list
... # i.e. reducing list of values to a single value
...
>>> from functools import reduce
>>>
>>> total = reduce(lambda n1, n2: n1 + n2, l)
>>> total
19
>>>

您可以使用forEach()函數將一些回調函數應用於集合中的每個項目

基本實現如下所示:

   function forEach(callback) {
      for(index=0;index<len_of_collection;index++) {
         callback(collection[index], index, collection);
      }
   }

您需要確保您的集合實現了此方法,以便您可以像這樣從集合中調用它:

some_collection.forEach(myCustomCallback)

或內聯:

some_collection.forEach(item => console.log(item))

請原諒JavaScript,我知道問題在Python中,但是概念不是特定於語言的。

編輯: https//www.geeksforgeeks.org/python-map-function/

在將給定函數應用於給定可迭代項的每個項目(列表,元組等)之后,map()函數返回結果列表。

您的示例未顯示修改。 因此,相符地映射將是不正確的。 如果要應用某些功能而不做任何修改,請瀏覽類似於forEach的選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM