Get in touch with functional programming in JavaScript or Python 3

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:

For JavaScript

Preparatory exercise

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) 

First of all, be gentle

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

I'll do my best

main.js


let a = b => c => d => b * c * d
console.log(a(1)(2)(3))
console.log(a(1)(2)(0))
// 6 
// 0

I will do my best

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

For Python

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)

First of all, be gentle

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

_____________'''


I'll do my best

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


'''


What you are doing is the same

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

Get in touch with functional programming in JavaScript or Python 3
Functional programming in Python Project Euler 1
Functional programming in Python Project Euler 3
Functional programming in Python Project Euler 2
Get started with Python in Blender
Get additional data in LDAP with python
Express Python yield in JavaScript or Java
Programming in python
Programming with Python / JavaScript / VBScript inline scripting in Automation Anywhere A 2019
[Python] Get the files in a folder with Python
What is "functional programming" and "object-oriented" in Python?
3. 3. AI programming with Python
Python programming with Atom
Competitive programming with python
Get date in Python
Get date with python
Python programming in Excel
Programming with Python Flask
Get all standard inputs used in paiza and competitive programming with int (python)
[Python] Get the numbers in the graph image with OCR
Get the result in dict format with Python psycopg2
Pretty print json or yaml with color in python
Sample code to get started with GLSL shaders in Processing (either Java or Python)
Get country code with python
Programming with Python and Tkinter
Scraping with selenium in Python
Get last month in python
Working with LibreOffice in Python
Get Twitter timeline with python
Scraping with chromedriver in python
Debugging with pdb in Python
Working with sounds in Python
Scraping with Selenium in Python
Tweet with image in Python
Get Terminal size in Python
Explicitly get EOF in python
Combined with permutations in Python
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Get Evernote notes in Python
Get home directory with python
Get keyboard events with python
Network programming with Python Scapy
Get Alembic information with Python
Get Japanese synonyms in Python
Compare HTTP GET / POST with cURL (command) and Python (programming)
Remove leading and trailing whitespace in Python, JavaScript, or Java
Get message from first offset with kafka consumer in python
Get Leap Motion data in Python.
Number recognition in images with Python
How is the progress? Let's get on with the boom ?? in Python
GUI programming in Python using Appjar
Testing with random numbers in Python
Link to get started with python
GOTO in Python with Sublime Text 3
Get data from Quandl in Python
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
[Python] Object-oriented programming learned with Pokemon
Get the desktop path in Python
Run mruby with Python or Blender