Last active
November 25, 2017 19:37
-
-
Save idettman/7300083e97d2a43f493afbc6ddcab935 to your computer and use it in GitHub Desktop.
JavaScript curry examples using Ramda and ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const eventHandler = message => event => { | |
| alert(message, event.target.id); | |
| } | |
| // <button id="button1" onClick="eventHandler('hello from')">click</button> | |
| // Ramda | |
| const requestWithOpts = R.curry((protocol, hostname, port) => | |
| protocol + '://' + hostname + (port === 80 ? '' : ':' + port) | |
| ); | |
| const requestHttp = requestWithOpts('http'); | |
| const requestHttps = requestWithOpts('https'); | |
| const request = requestHttp(R.__, 80); | |
| // Another Ramda example function | |
| function request( protocol, port, url ){ | |
| return protocol + "://" + url + (port == 80 ? "" : ":"+port) | |
| } | |
| // creating keyword argument curried function | |
| const kRequest = kwargs(request, { | |
| port: 80, | |
| protocol: "http", | |
| url: undefined | |
| }) | |
| //tests | |
| ;([ | |
| kRequest | |
| ({}) | |
| ({}) | |
| ({url: "google.com"}) == "http://google.com" | |
| , | |
| kRequest | |
| ({ port: 3000 }) | |
| ({url: "localhost"}) == "http://localhost:3000", | |
| kRequest | |
| ({ port: 3000 }) | |
| ({ protocol: "https", port: 3001 }) | |
| ({ url: "github.com"}) == "https://github.com:3001" | |
| ]).every( function(val){ return val} ) && "All tests passed" | |
| // Another Ramda | |
| const get = { | |
| dropbox: request({ protocol: "https" }), | |
| users: production ? | |
| request( protocol: "https", url: "production.api.com" ) : | |
| request({ url: "localhost", port: 3000 }), | |
| amazonS3: request({ url: ... }) //etc | |
| } | |
| // usage | |
| const users = get.users() | |
| // Another Ramda | |
| function render( controller, view, drawImmediately ){ | |
| if(drawImmediately){ | |
| view(controller) | |
| } else { | |
| // store the render instruction somewhere, and do it at a more efficient time | |
| // like when the user is no longer scrolling. | |
| // ... | |
| } | |
| } | |
| kRender = kwargs( render, {} ) | |
| // a lot clearer | |
| kRender({ controller: ctrl, view: view, drawImmediately: true }) | |
| //legacy function still available | |
| render( controller, view, true ) | |
| renderImmediate = kRender({ drawImmediately: true}) | |
| renderLater = kRender({ drawImmediately: false}) | |
| // Ramda GIT Node Commands example | |
| const git = R.curryNamed(function(options){ | |
| const command = ["git"].concat( | |
| options.subcommand, | |
| options.arguments | |
| ).join(" ") | |
| return exec(command, options.callback); | |
| }, { | |
| //default values are predefined | |
| subcommand: "help", | |
| arguments: [], | |
| //required indicated by undefined | |
| callback: undefined | |
| }) | |
| //git log | |
| const log = git({ | |
| subcommand: "log", | |
| }) | |
| const prettyLog = log({ | |
| arguments: [ | |
| "pretty=oneline" | |
| ] | |
| }) | |
| const diff = git({ | |
| subcommand: "diff" | |
| }) | |
| //executes `git log` because it's only required argument was provided. | |
| log({ | |
| callback: console.log | |
| }) | |
| prettyLog({ | |
| callback: console.log | |
| }) | |
| diff({ | |
| callback: console.log | |
| }) | |
| // ALT version | |
| const git = R.curryNamed(function(subcommand, arguments, callback){ | |
| const command = ["git"].concat( | |
| subcommand | |
| arguments | |
| ).join(" ") | |
| return exec(command, callback); | |
| }, ["subcommand","arguments", "visitor"], { | |
| //default values are predefined | |
| subcommand: "help", | |
| arguments: [] | |
| }) | |
| // 2 | |
| const curriedGit = R.curryNamed( git, ["subcommand","arguments", "visitor"], { | |
| subcommand: "help", | |
| arguments: [], | |
| }) | |
| // Ramda Named Curried Functions | |
| // There is no default argument here. Just stating argument order, and assigning names to that order. | |
| const namedRender = R.curryNamed( render, [ "controller", "view", "drawImmediately" ]) | |
| // Creating a version of `namedRender` where drawImmediately is bound to true | |
| const renderImmediately = namedRender({ drawImmediately: true }) | |
| // Creating a version of `namedRender` where drawImmediately is bound to false | |
| const renderLater = namedRender({drawImmediately: false}) | |
| // Creating a function built for mapping over views, and rendering them using the app's cotnroller. | |
| const renderViewLater = R.pipe( | |
| R.createMapEntry("view"), | |
| renderLater({ | |
| controller: app.controller | |
| }) | |
| ) | |
| //This view will render at a time of the apps choosing | |
| renderLater({ view: view1 } | |
| //This view will render immediately | |
| renderImmediately({ view: view2}) | |
| // Here we don't need to reference the view by name | |
| // as the definition included R.createMapEntry("view") | |
| R.map( | |
| // these views will all be rendered at a later time | |
| renderListLater, [ view1, view2, view3 ]) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment