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. LPC43xx SGPIO Slice 示意图

    SGPIO inverted clock qualifier Hi, With bits 6:5 of SGPIO_MUX_CFG the QUALIFIER_MODE is selected (0x ...

  2. [Go] Http包 使用简介

    请求的结构 HTTP 的交互以请求和响应的应答模式.Go 的请求我们早就见过了,handler 函数的第二个参数 http.Requests.其结构为: type Request struct { M ...

  3. Revit API判断是不是柱族模板

    OwnerFamily即族模板.获取类别的方法:Document.Settings.Categories.get_Item(BuiltInCategory.OST_Columns); //判断是不是柱 ...

  4. Revit API创建几何实体Solid并找到与之相交的元素

    几何实体的创建方法之一:构成封闭底面,指定拉伸方向与拉伸高度.GeometryCreationUtilities ;         , pt.Y - dBoxLength / , pt.Z);    ...

  5. 关于电商ERP的想法

    原文地址: http://www.chinaodoo.net/thread-465-1-1.html 试用了下odoo的淘宝订单处理模块,从整个业务流程上已经打通,如果要求不是很高的话,现有的功能基本 ...

  6. C#编程(三十八)----------运算符

    原文链接: http://blog.csdn.net/shanyongxu/article/details/46877353 运算符 类别 运算符 算术运算符 + - * / 逻辑运算符 &  ...

  7. Sales Order ORA-04062 FRM-40815 in EBS R12.2.4

    [oracle@ebs ~]$ su - oracle [oracle@ebs ~]$ source /u01/install/VISION/fs1/EBSapps/appl/APPSEBSDB_eb ...

  8. 11i and R12 Table Count in Different Module

    Advertisement Module 11i Tables R12 Tables New Tables AR 551 616 118 BOM 264 337 73 GL 186 309 140 A ...

  9. 都铎王朝第一至四季/全集The Tudors迅雷下载

    都铎王朝 第一.二.三.四季 The Tudors Season (2007-2010) 本季看点:本剧讲述了年轻的亨利八世对英国的统治以及他的婚姻生活,带有传奇色彩.Showtime电视台的古装热门 ...

  10. WorkerThread与MainThread之间通过Handler进行最简单的消息传递

    一.从自己开启的线程中给主线程发送信息,更新UI 这个实例的效果是,在线程中通过handler发送一条信息给handler,然后通过handler更改UI线程中,textview的文字. 主要方法是( ...