This lesson teaches you how arguments passed to a curried function allow us to store data in closure to be reused in our programs and applications. Since each argument, except for the final one, returns a new function, we can easily create reusable functions by supplying some of these arguments beforehand, and sharing these partially applied functions with other parts of our codebase. In this lesson, we'll create a curried function to fetch requests from an API that uses partial application to create reusable functionality.

Another way to do partially API call is using Proxy. Check this out.

// Partial Application

// Curried functions create a wonderful emergent property, "partial application",
// that is useful for building up reusability in our applications that you
// just can't get with normal, multivariate functions. Because curried functions
// return a new function with each argument (except for the final one), we are
// able to "partially apply" values and store them in closure, available to any
// subsequent function, thus creating new, reusable functions with some values
// already supplied. // Imagine we have an application that needs to make requests to different APIs.
// We can create a function that bakes in the base URL, while allowing other
// arguments to be passed in later const fetch = require('node-fetch') const getFromAPI = baseURL => endPoint => callback =>
fetch(`${baseURL}${endPoint}`)
.then(res => res.json())
.then(data => callback(data))
.catch(err => {
console.error(err.message)
}) // Now we can partially apply a baseURL to create a reusable function for
// one of our APIs const getGithub = getFromAPI(
'https://api.github.com'
) // We can create several get request functions by partially applying different
// endpoints to our getGithub function const getGithubUsers = getGithub('/users')
const getGithubRepos = getGithub('/repositories') // Now we can use our callback to get the data and do something with it. getGithubUsers(data =>
data.forEach(user => {
console.log(`User: ${user.login}`)
})
)
getGithubRepos(data =>
data.forEach(repo => {
console.log(`Repo: ${repo.name}`)
})
) // We can still continue to reuse previous partially applied functions const getGithubOrgs = getGithub('/organizations')
getGithubOrgs(data =>
data.forEach(org => {
console.log(`Org: ${org.login}`)
})
) // We can start the process all over by partially applying a new baseURL const getReddit = getFromAPI('https://reddit.com') // And let's get some pictures of some cute animals const getRedditAww = getReddit('/r/aww.json') // And fetch the URLs of those images const imageURLs = getRedditAww(payload =>
payload.data.children.forEach(child => {
console.log(
child.data.preview.images[].source.url
)
})
)

[Functional Programming] Create Reusable Functions with Partial Application in JavaScript的更多相关文章

  1. [Functional Programming] From simple implementation to Currying to Partial Application

    Let's say we want to write a most simple implementation 'avg' function: const avg = list => { let ...

  2. [Functional Programming] Running though a serial number prediction functions for tagging, pairing the result into object

    Let's we have some prediction functions, for each prediction function has a corresponding tag: const ...

  3. Functional Programming without Lambda - Part 1 Functional Composition

    Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...

  4. a primary example for Functional programming in javascript

    background In pursuit of a real-world application, let’s say we need an e-commerce web applicationfo ...

  5. Functional programming

    In computer science, functional programming is a programming paradigm, a style of building the struc ...

  6. Currying vs Partial Application

    柯里化相当于函数重构: 偏函数相当于函数适配. So, what is the difference between currying and partial application? As we s ...

  7. Monad (functional programming)

    In functional programming, a monad is a design pattern that defines how functions, actions, inputs, ...

  8. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

  9. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

随机推荐

  1. SGU 275. To xor or not to xor (高斯消元法)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 题意:给你n个数,可以选择任意个数异或,但是要使得最后的异或值最大. 我们把每 ...

  2. LabTool : LPC LINK2, LPC4370 cheap scope: 80Ms/s 12 bit

    80MHz 12 bit ADC processor LPC4370.LPCxpresso do a LPC LINK2 and LABTOOLS open source oscilloscope d ...

  3. E3-1260L (8M Cache, 2.40 GHz) E3-1265L v2 (8M Cache, 2.50 GHz)

    http://ark.intel.com/compare/52275,65728         Product Name Intel® Xeon® Processor E3-1260L (8M Ca ...

  4. Go 导入当前项目下的包

    其实和其他语言很类似 import (     "../controllers" //这里就是导入上一级目录中的controllers     "./models&quo ...

  5. [翻译] 10 个实用的 Git 高级命令

    1. 输出最后一次提交的改变 这个命令,我经常使用它 来发送其他没有使用 git 的人来检查或者集成所修改的.它会输出最近提交的修改内容到一个 zip 文件中. git archive -o ../u ...

  6. 安装oracle环境变量path的值大于1023的解决办法

    介绍解决安装oracle安装问题 方法/步骤     安装oracle 10g时遇到环境变量path的值超过1023字符,无法设置该值,如图: ‍ 安装oracle 10g时遇到环境变量path的值超 ...

  7. Time Zones And Daylight Savings Time

    This page describes code for working with Time Zones and Daylight Savings Time. Neither VBA nor VB6 ...

  8. Windows Phone本地数据库(SQLCE):14、删除数据(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的最后一篇第十四篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需 ...

  9. windows phone 基础

    一.安装 建议安装Windows 7环境,XP中不能运行模拟器,Vista系统支持,但不解释.系统安装完后,直接去微软网站在线安装即可,非常方便,美中不足的是如果你的网速不快,那可能要折磨你半天,快得 ...

  10. 理解 HTTPS 协议

    英文原文:Understanding HTTPS Protocol 最近我们看到很多站点使用 HTTPS 协议提供网页服务.通常情况下我们都是在一些包含机密信息的站点像银行看到 HTTPS 协议. 如 ...