FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
javascript-patterns/function-patterns/currying.html at master · chosh31/javascript-patterns · GitHub
chosh31
javascript-patterns
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
javascript-patterns
/
function-patterns
/
currying.html
Copy path
More file actions
More file actions
Latest commit
History
History
History
178 lines (149 loc) · 4.77 KB
Breadcrumbs
javascript-patterns
/
function-patterns
/
currying.html
Copy path
File metadata and controls
178 lines (149 loc) · 4.77 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!doctype html
>
<
html
lang
="
en
"
>
<
head
>
<
title
>
JavaScript Patterns
</
title
>
<
meta
charset
="
utf-8
"
>
</
head
>
<
body
>
<
script
>
/* Title: Currying
Description: used to create new functions dynamically by partially applying a set of arguments
*/
/***
function application
***/
// define a function
var
sayHi
=
function
(
who
)
{
return
"Hello"
+
(
who
?
", "
+
who
:
""
)
+
"!"
;
}
;
// invoke a function
sayHi
(
)
;
// "Hello!"
sayHi
(
'world'
)
;
// "Hello, world!"
// apply a function
sayHi
.
apply
(
null
,
[
"hello"
]
)
;
// "Hello, hello!"
var
alien
=
{
sayHi
:
function
(
who
)
{
return
"Hello"
+
(
who
?
", "
+
who
:
""
)
+
"!"
;
}
}
;
alien
.
sayHi
(
'world'
)
;
// "Hello, world!"
sayHi
.
apply
(
alien
,
[
"humans"
]
)
;
// "Hello, humans!"
// the second is more efficient, saves an array
sayHi
.
apply
(
alien
,
[
"humans"
]
)
;
// "Hello, humans!"
sayHi
.
call
(
alien
,
"humans"
)
;
// "Hello, humans!"
/***
partial application
***/
// for illustration purposes
// not valid JavaScript
// we have this function
function
add
(
x
,
y
)
{
return
x
+
y
;
}
// and we know the arguments
add
(
5
,
4
)
;
// step 1 -- substitute one argument
function
add
(
5
,
y
)
{
return
5
+
y
;
}
// step 2 -- substitute the other argument
function
add
(
5
,
4
)
{
return
5
+
4
;
}
/***
currying
***/
// a curried add()
// accepts partial list of arguments
function
add
(
x
,
y
)
{
var
oldx
=
x
,
oldy
=
y
;
if
(
typeof
oldy
===
"undefined"
)
{
// partial
return
function
(
newy
)
{
return
oldx
+
newy
;
}
}
// full application
return
x
+
y
;
}
// test
typeof
add
(
5
)
;
// "function"
add
(
3
)
(
4
)
;
// 7
// create and store a new function
var
add2000
=
add
(
2000
)
;
add2000
(
10
)
;
// 2010
function
schonfinkelize
(
fn
)
{
var
slice
=
Array
.
prototype
.
slice
,
stored_args
=
slice
.
call
(
arguments
,
1
)
;
return
function
(
)
{
var
new_args
=
slice
.
call
(
arguments
)
,
args
=
stored_args
.
concat
(
new_args
)
;
return
fn
.
apply
(
null
,
args
)
;
}
;
}
// reference
// http://www.jspatterns.com/
// http://shop.oreilly.com/product/9780596806767.do?sortby=publicationDate
// Here's a generic curry function (from kybernetikos):
// It's slightly more complex than the common examples of currying (e.g. the one above), but that's because
// the function can afterwards be called either in the normal way, or in the curried way.
// If you call a 3 argument curried function with it, the returned function *is also curried*.
// e.g. you can write func(arg1)(arg2)(arg3) or func(arg1, arg2, arg3) or func(arg1)(arg2, arg3) etc...
/* the curry procedure needs to know how many arguments are required before it should calculate the result */
function
curry
(
func
,
minArgs
)
{
if
(
minArgs
==
undefined
)
{
minArgs
=
1
;
}
function
funcWithArgsFrozen
(
frozenargs
)
{
return
function
(
)
{
// could do an optimisation here - if called with no arguments
// return exactly this function.
var
args
=
Array
.
prototype
.
slice
.
call
(
arguments
)
;
var
newArgs
=
frozenargs
.
concat
(
args
)
;
if
(
newArgs
.
length
>=
minArgs
)
{
return
func
.
apply
(
this
,
newArgs
)
;
}
else
{
return
funcWithArgsFrozen
(
newArgs
)
;
}
}
;
}
return
funcWithArgsFrozen
(
[
]
)
;
}
/* Here are some example uses - I use these with a functional immutable list structure implemented in js
but they also work ok for normal higher order functions over arrays.
*/
var
plus
=
curry
(
function
(
)
{
var
result
=
0
;
for
(
var
i
=
0
;
i
<
arguments
.
length
;
++
i
)
{
result
+=
arguments
[
i
]
;
}
return
result
;
}
,
2
)
;
/* Now you can call
plus(3,2) // normal call
plus(3) // partial application (returns a function that adds 3 to its argument)
plus(3)(2) // complete application (returns 5)
plus()(3)()()(2) // returns 5
plus(3, 2, 4, 5) // the normal call can optionally take more than the minimum number of arguments
plus(3)(2, 3, 5) // the last application can too.
*/
var
minus
=
curry
(
function
(
x
)
{
var
result
=
x
;
for
(
var
i
=
1
;
i
<
arguments
.
length
;
++
i
)
{
result
-=
arguments
[
i
]
;
}
return
result
;
}
,
2
)
;
/* flip switches the order of the first two arguments on a function. It is curried itself and
the function it returns is curried too. Particularly useful if you want a function that subtracts a number */
var
flip
=
curry
(
function
(
func
)
{
return
curry
(
function
(
a
,
b
)
{
return
func
(
b
,
a
)
;
}
,
2
)
;
}
)
;
/* for example
minus(5) // returns a function that takes its argument away from 5
flip(minus)(5) // returns a function that takes 5 away from its argument
*/
</
script
>
</
body
>
</
html
>
Back
|
FazBrowse Home
|
New Git URL