In [24]: pilots =[{'id':10,'name':'Poe Dameron','years':14},...:{'id':2,'name':"Temmin 'Snap' Wexley",'years':30},...:{'id':41,'name':'Tallissan Lintra','years':16},...:{'id':99,'name':'Ello Asty','years':22}]
In [25]:map(lambda x : f"Mr. {x['name']}", pilots)
Out[25]:<map at 0x22fd24e0108>
In [26]:list(map(lambda x : f"Mr. {x['name']}", pilots))
Out[26]:['Mr. Poe Dameron',"Mr. Temmin 'Snap' Wexley",'Mr. Tallissan Lintra','Mr. Ello Asty']
In [27]:
In [28]: pilots
Out[28]:[{'id':10,'name':'Poe Dameron','years':14},{'id':2,'name':"Temmin 'Snap' Wexley",'years':30},{'id':41,'name':'Tallissan Lintra','years':16},{'id':99,'name':'Ello Asty','years':22}]
In [29]:filter(lambda x : x['years']>20, pilots)
Out[29]:<filter at 0x22fd24b3188>
In [30]:list(filter(lambda x : x['years']>20, pilots))
Out[30]:[{'id':2,'name':"Temmin 'Snap' Wexley",'years':30},{'id':99,'name':'Ello Asty','years':22}]
In [20]:import functools
In [21]: pilots =[{'id':10,'name':'Poe Dameron','years':14},...:{'id':2,'name':"Temmin 'Snap' Wexley",'years':30},...:{'id':41,'name':'Tallissan Lintra','years':16},...:{'id':99,'name':'Ello Asty','years':22}]
In [22]: longestfunctools.reduce(lambda a,b : a if a["years"]> b["years"]else b, pilots)
Out[22]:{'id':2,'name':"Temmin 'Snap' Wexley",'years':30}
Javascript
nakamono@ninja MINGW64~/wk/coding/hello_node
$ cat reduce.js
var pilots =[{
id:10,
name:"Poe Dameron",
years:14,},{
id:2,
name:"Temmin 'Snap' Wexley",
years:30,},{
id:41,
name:"Tallissan Lintra",
years:16,},{
id:99,
name:"Ello Asty",
years:22,}];
longest_year = pilots.reduce((accumulator, pilot)=>{return accumulator > pilot.years ? accumulator : pilot.years;},0);
console.log(`Longest year of service=${longest_year}`);
oldest_pilot = pilots.reduce((accumulator, pilot)=>{return(accumulator.years ||0)> pilot.years ? accumulator : pilot;},{});
console.log('oldest pilot is as below');
console.log(oldest_pilot);
console.log(oldest_pilot.years);
inakamono@ninja MINGW64~/wk/coding/hello_node
$ node reduce.js
Longest year of service=30
oldest pilot is as below
{ id:2, name:"Temmin 'Snap' Wexley", years:30}30