Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b65634

Browse files
author
Badacadabra
committed
Add Currying (ES5 + ES6 + CoffeeScript + TypeScript)
1 parent 8c806af commit 3b65634

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

‎misc/Currying/CoffeeScript/index.coffee

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
profile = (firstName) ->
2+
(lastName) ->
3+
(age) ->
4+
(gender) ->
5+
(nationality) ->
6+
"""
7+
First name: #{firstName}
8+
Last name: #{lastName}
9+
Age: #{age}
10+
Gender: #{gender}
11+
Nationality: #{nationality}
12+
"""
13+
14+
myProfile = profile("Baptiste")("Vannesson")(27)("M")("French")
15+
16+
console.log myProfile

‎misc/Currying/ECMAScript/ES5/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var profile = function (firstName) {
2+
return function (lastName) {
3+
return function (age) {
4+
return function (gender) {
5+
return function (nationality) {
6+
var str = "";
7+
str += "First name: " + firstName;
8+
str += "\nLast name: " + lastName;
9+
str += "\nAge: " + age;
10+
str += "\nGender: " + gender;
11+
str += "\nNationality: " + nationality;
12+
return str;
13+
};
14+
};
15+
};
16+
};
17+
};
18+
19+
var myProfile = profile("Baptiste")("Vannesson")(27)("M")("French");
20+
21+
console.log(myProfile);

‎misc/Currying/ECMAScript/ES6/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const profile = firstName => {
2+
return lastName => {
3+
return age => {
4+
return gender => {
5+
return nationality => `First name: ${firstName}
6+
Last name: ${lastName}
7+
Age: ${age}
8+
Gender: ${gender}
9+
Nationality: ${nationality}`
10+
};
11+
};
12+
};
13+
};
14+
15+
let myProfile = profile("Baptiste")("Vannesson")(27)("M")("French");
16+
17+
console.log(myProfile);

‎misc/Currying/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Problem
2+
3+
Functions may have a high arity, which means that they can accept a lot of arguments. This is not optimal for at least two reasons:
4+
5+
1. Keeping a nice-looking indentation is hard if the list of parameters is really long
6+
2. It is difficult for the programmer to remember the order of many positional parameters
7+
8+
# Solution
9+
10+
We can use currying to reduce the arity of a function. The aim of this pattern is to transform a function with many parameters into multiple functions with only one parameter.
11+
Since JavaScript is an extremely flexible language, the only thing to do is to create a sort of "return chain" where each function returns another function (with only one parameter) until the number of parameters is exhausted.

‎misc/Currying/TypeScript/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const profile = (firstName: string) => {
2+
return (lastName: string) => {
3+
return (age: number) => {
4+
return (gender: string) => {
5+
return (nationality: string) => `First name: ${firstName}
6+
Last name: ${lastName}
7+
Age: ${age}
8+
Gender: ${gender}
9+
Nationality: ${nationality}`
10+
};
11+
};
12+
};
13+
};
14+
15+
let myProfile: string = profile("Baptiste")("Vannesson")(27)("M")("French");
16+
17+
console.log(myProfile);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /