1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptかPython3で関数型プログラミングに触れてみる

Posted at

関数型プログラミングとはなんぞやという人間が、JavaScriptかPythonで関数型プログラミングを初めての経験ということで、触ってみた。
まだまだ勉強中なんでとりあえず学んだことをメモします。

今日本日はカレーがメインです:curry:

JavaScriptの場合

準備運動

main.js
        // filterで絞り込みをする

        let talentList = [
            {'name' : "高田健志" , 'Belong' : null},
            {'name' : "もっさん" , 'Belong' : null},
            {'name' : "みどにぃ" , 'Belong' : "N○"},
            {'name' : "こくにぃ" , 'Belong' : null},
            {'name' : "かっさん" , 'Belong' : "無らっしゅ"},
            {'name' : "ジャングル" , 'Belong' : "無らっしゅ"},
        ]

        let getFi = talentList.filter( x => x.Belong === null )
        getFi.forEach( e => console.log(e.name))
        /*
            高田健志
            もっさん
            こくにぃ
        */


        // filterした結果でforEachする

        let ZimushoAruName = []

        talentList
            .filter( x => !x.Belong)
            .forEach( x => ZimushoAruName.push(x.name))
        console.log(ZimushoAruName)

        /*
            (3) ["みどにぃ", "かっさん", "ジャングル"]
        */

        // なにやら人気のあるreduce

        // 大きい方を返却する
        const getMax = (a,b) => a > b ? a : b

        let fruits = [
            { "name" : "りんご" , "price" : 200 },
            { "name" : "ぶどう" , "price" : 500 },
            { "name" : "" , "price" : 150 },
            { "name" : "いちご" , "price" : 300 },
            { "name" : "メロン" , "price" : 900 },
            { "name" : "ばなな" , "price" : 100 },
        ]

        let m = fruits.reduce( (a,b) => getMax(a,b.price),0)
        console.log(m)
        // 900

今回頑張って作る関数

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("高田健志の") // c
hogee("お通りだ") // e
// 高田健志のお通りだ

もうちょっと頑張る

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" : "お茶" , "price" : 100 , "sales": 1000 },
            { "name" : "おにぎり" , "price" : 150 , "sales": 1300 },
            { "name" : "お弁当" , "price" : 500 , "sales": 300 },
            { "name" : "ケーキ" , "price" : 300 , "sales": 200 },
            { "name" : "フライドチキン" , "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

Pythonの場合

今回頑張って作る関数

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},
	]

# 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()))

'''__出力結果______________

[{'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()))



'''
出力結果
[{'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()))



jsonでGETしたID1が表示されます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?