What is functional programming? I tried to touch it because it was my first experience with functional programming with JavaScript or Python. I'm still studying, so I'll make a note of what I learned for the time being.
Curry is the main thing today: curry:
main.js
//Filter by filter
let talentList = [
{'name' : "Takada Kenshi" , 'Belong' : null},
{'name' : "Motto" , 'Belong' : null},
{'name' : "Midoni" , 'Belong' : "N○"},
{'name' : "Kokuni" , 'Belong' : null},
{'name' : "Kassan" , 'Belong' : "No Rashu"},
{'name' : "jungle" , 'Belong' : "No Rashu"},
]
let getFi = talentList.filter( x => x.Belong === null )
getFi.forEach( e => console.log(e.name))
/*
Takada Kenshi
Motto
Kokuni
*/
//For Each with the filtered result
let ZimushoAruName = []
talentList
.filter( x => !x.Belong)
.forEach( x => ZimushoAruName.push(x.name))
console.log(ZimushoAruName)
/*
(3) ["Midoni", "Kassan", "jungle"]
*/
//Somehow popular reduce
//Return the larger one
const getMax = (a,b) => a > b ? a : b
let fruits = [
{ "name" : "Apple" , "price" : 200 },
{ "name" : "Grape" , "price" : 500 },
{ "name" : "persimmon" , "price" : 150 },
{ "name" : "Strawberry" , "price" : 300 },
{ "name" : "melon" , "price" : 900 },
{ "name" : "Banana" , "price" : 100 },
]
let m = fruits.reduce( (a,b) => getMax(a,b.price),0)
console.log(m)
// 900
Function to make hard this time
main.js
const reduce = func => start => datas => datas.reduce( (acum,product) => func(acum)(product.price),0)
main.js
let ggj = function(c){
return function(e){
console.log(c + e)
}
}
hogee = ggj("Takada Kenshi's") // c
hogee("It's a street") // e
//It's the street of Takada Kenshi
main.js
let a = b => c => d => b * c * d
console.log(a(1)(2)(3))
console.log(a(1)(2)(0))
// 6
// 0
main.js
const products = [
{ "name" : "tea" , "price" : 100 , "sales": 1000 },
{ "name" : "rice ball" , "price" : 150 , "sales": 1300 },
{ "name" : "Bento" , "price" : 500 , "sales": 300 },
{ "name" : "cake" , "price" : 300 , "sales": 200 },
{ "name" : "Fried chicken" , "price" : 200 , "sales": 500 },
]
const add = x => y => x + y
const reduce = func => start => datas => datas.reduce( (acum,product) => func(acum)(product.price),0)
const getSumPrice = reduce(add)(0)(products)
console.log(getSumPrice)
// 1250
Function to make hard this time
qiita.py
def getData(f):
return lambda w : lambda v : lambda d : lambda : filter(lambda x : f(x[w])(v),d)
qiita.py
product = [
{'name':'takadakenshi','price':300},
{'name':'yokoyamamidori','price':100},
{'name':'babayutaka','price':500},
{'name':'nodazori','price':10},
]
#Return using filter
def addfilter(datas,price):
return filter(lambda x: x['price'] >= price,datas)
def searchWithPrice(datas):
return lambda price : lambda : addfilter(datas,price)
getsan = searchWithPrice(product)(300)
getyon = searchWithPrice(product)(400)
print(list(getsan()))
print('++++++++++++++')
print(list(getyon()))
'''__Output result______________
[{'name': 'takadakenshi', 'price': 300}, {'name': 'babayutaka', 'price': 500}]
++++++++++++++
[{'name': 'babayutaka', 'price': 500}]
_____________'''
qiita.py
def eq(a):
return lambda b : a == b
def bigger(a):
return lambda b : a >= b
def kaiserSearch(func):
return lambda where : lambda word : lambda product : lambda : filter(lambda x : func(x[where])(word),product)
out2 = kaiserSearch(eq)('name')('babayutaka')(product)
out3 = kaiserSearch(eq)('price')(100)(product)
out4 = kaiserSearch(bigger)('price')(300)(product)
print(list(out2()))
print(list(out3()))
print('''
bigger:
''')
print(list(out4()))
'''
Output result
[{'name': 'babayutaka', 'price': 500}]
[{'name': 'yokoyamamidori', 'price': 100}]
bigger:
[{'name': 'takadakenshi', 'price': 300}, {'name': 'babayutaka', 'price': 500}]
'''
qiita.py
import requests
url = 'https://jsonplaceholder.typicode.com/users'
json_data = requests.get(url).json()
def eq(a):
return lambda b : a == b
def getData(f):
return lambda w : lambda v : lambda d : lambda : filter(lambda x : f(x[w])(v),d)
result = getData(eq)('id')(1)(json_data)
print(list(result()))
ID1 GET with json is displayed.
Recommended Posts