関数潮流(Function Tendency)
- 76. js> function add2(x) { return x + 2; }
js> add2(5);
7
js> var f = function(x) { return x + 2; }
js> f(8);
10
js> (function(x) { return x + 2; })(9)
11
- 77. js> function add2(x) { return x + 2; }
js> add2(5);
7
js> var f = function(x) { return x + 2; }
js> f(8);
10
js> (function(x) { return x + 2; })(9)
11
- 78. js> function add2(x) { return x + 2; }
js> add2(5);
7
js> var f = function(x) { return x + 2; }
js> f(8);
10
js> (function(x) { return x + 2; })(9)
11
- 79. js> function add2(x) { return x + 2; }
js> add2(5);
7
js> var f = function(x) { return x + 2; }
js> f(8);
10
js> (function(x) { return x + 2; })(9)
11
- 80. js> function add2(x) { return x + 2; }
js> add2(5);
7
js> var f = function(x) { return x + 2; }
js> f(8);
10
js> (function(x) { return x + 2; })(9)
11
- 81. Func<int, int> f = (x) => x + 2;
var f = function(x) { return x + 2; }
f = lambda x: x + 2;
f = x -> x + 2
(fset 'f (lambda (x) (+ x 2)))
- 87. Func<string, string>
foo(Func<DateTime, string> f) {
return
x => x.ToUpper() + " "
+ f(DateTime.Today);
}
Func<DateTime, string> d =
x => x.Year + "/" + x.Month;
Func<string, string> c = foo(d);
string result = c("oblove");
“OBLOVE 2011/7”
- 88. Func<string, string>
foo(Func<DateTime, string> f) {
return
x => x.ToUpper() + " "
+ f(DateTime.Today);
}
Func<DateTime, string> d =
x => x.Year + "/" + x.Month;
Func<string, string> c = foo(d);
string result = c("oblove");
“OBLOVE 2011/7”
- 89. Func<string, string>
foo(Func<DateTime, string> f) {
return
x => x.ToUpper() + " "
+ f(DateTime.Today);
}
Func<DateTime, string> d =
x => x.Year + "/" + x.Month;
Func<string, string> c = foo(d);
string result = c("oblove");
“OBLOVE 2011/7”
- 90. Func<string, string>
foo(Func<DateTime, string> f) {
return
x => x.ToUpper() + " "
+ f(DateTime.Today);
}
Func<DateTime, string> d =
x => x.Year + "/" + x.Month;
Func<string, string> c = foo(d);
string result = c("oblove");
“OBLOVE 2011/7”
- 91. Func<string, string>
foo(Func<DateTime, string> f) {
return
x => x.ToUpper() + " "
+ f(DateTime.Today);
}
Func<DateTime, string> d =
x => x.Year + "/" + x.Month;
Func<string, string> c = foo(d);
string result = c("oblove");
“OBLOVE 2011/7”
- 92. Func<string, string>
foo(Func<DateTime, string> f) {
return
x => x.ToUpper() + " "
+ f(DateTime.Today);
}
Func<DateTime, string> d =
x => x.Year + "/" + x.Month;
Func<string, string> c = foo(d);
string result = c("oblove");
“OBLOVE 2011/7”
- 93. Func<string, string>
foo(Func<DateTime, string> f) {
return
x => x.ToUpper() + " "
+ f(DateTime.Today);
}
Func<DateTime, string> d =
x => x.Year + "/" + x.Month;
Func<string, string> c = foo(d);
string result = c("oblove");
“OBLOVE 2011/7”
- 104. int sum(int acc, IEnumerable<int> list) {
if (!list.Any()) {
return acc;
}
int head = list.First();
return sum(acc + head, list.Skip(1));
}
List<int> ls = new List<int> { 3, 5, 8 };
int result = sum(0, ls);
- 105. int sum(int acc, IEnumerable<int> list) {
if (!list.Any()) {
return acc;
}
int head = list.First();
return sum(acc + head, list.Skip(1));
}
List<int> ls = new List<int> { 3, 5, 8 };
int result = sum(0, ls);
- 106. int sum(int acc, IEnumerable<int> list) {
if (!list.Any()) {
return acc;
}
int head = list.First();
return sum(acc + head, list.Skip(1));
}
List<int> ls = new List<int> { 3, 5, 8 };
int result = sum(0, ls);
- 107. int sum(int acc, IEnumerable<int> list) {
if (!list.Any()) {
return acc;
}
int head = list.First();
return sum(acc + head, list.Skip(1));
}
List<int> ls = new List<int> { 3, 5, 8 };
int result = sum(0, ls);
- 108. int sum(int acc, IEnumerable<int> list) {
if (!list.Any()) {
return acc;
}
int head = list.First();
return sum(acc + head, list.Skip(1));
}
List<int> ls = new List<int> { 3, 5, 8 };
int result = sum(0, ls);
- 109. int sum(int acc, IEnumerable<int> list) {
if (!list.Any()) {
return acc;
}
int head = list.First();
return sum(acc + head, list.Skip(1));
}
List<int> ls = new List<int> { 3, 5, 8 };
int result = sum(0, ls);
- 110. int sum(int acc, IEnumerable<int> list) {
if (!list.Any()) {
return acc;
}
int head = list.First();
return sum(acc + head, list.Skip(1));
}
List<int> ls = new List<int> { 3, 5, 8 };
int result = sum(0, ls);
- 117. >>> s = ['C#', 'Java', 'Python' ]
>>> map(lambda x: x.upper(), s)
['C#', 'JAVA', 'PYTHON']
>>> map(lambda x: len(x), s)
[2, 4, 6]
- 118. >>> s = ['C#', 'Java', 'Python' ]
>>> map(lambda x: x.upper(), s)
['C#', 'JAVA', 'PYTHON']
>>> map(lambda x: len(x), s)
[2, 4, 6]
- 119. >>> s = ['C#', 'Java', 'Python' ]
>>> map(lambda x: x.upper(), s)
['C#', 'JAVA', 'PYTHON']
>>> map(lambda x: len(x), s)
[2, 4, 6]
- 120. >>> s = [1, 2, 3, 4, 5, 6]
>>> filter(lambda x: x % 2 == 0, s)
[2, 4, 6]
>>> s = ['C#', 'Java', 'Python' ]
>>> map(lambda x: x.upper(),
filter(lambda x: len(x) > 3, s))
['JAVA', 'PYTHON']
- 121. >>> s = [1, 2, 3, 4, 5, 6]
>>> filter(lambda x: x % 2 == 0, s)
[2, 4, 6]
>>> s = ['C#', 'Java', 'Python' ]
>>> map(lambda x: x.upper(),
filter(lambda x: len(x) > 3, s))
['JAVA', 'PYTHON']
- 122. >>> s = [1, 2, 3, 4, 5, 6]
>>> filter(lambda x: x % 2 == 0, s)
[2, 4, 6]
>>> s = ['C#', 'Java', 'Python' ]
>>> map(lambda x: x.upper(),
filter(lambda x: len(x) > 3, s))
['JAVA', 'PYTHON']
- 123. >>> s = [1, 2, 3, 4, 5, 6]
>>> filter(lambda x: x % 2 == 0, s)
[2, 4, 6]
>>> s = ['C#', 'Java', 'Python' ]
>>> map(lambda x: x.upper(),
filter(lambda x: len(x) > 3, s))
['JAVA', 'PYTHON']
- 124. >>> s = [3, 4, 5, 7, 9]
>>> reduce(lambda x, y: x + y, s, 0)
28
>>> reduce(
lambda x, y:
x + (1 if (y % 2 == 0) else 0),
s, 0)
1
- 125. >>> s = [3, 4, 5, 7, 9]
>>> reduce(lambda x, y: x + y, s, 0)
28
>>> reduce(
lambda x, y:
x + (1 if (y % 2 == 0) else 0),
s, 0)
1
- 126. >>> s = [3, 4, 5, 7, 9]
>>> reduce(lambda x, y: x + y, s, 0)
28
>>> reduce(
lambda x, y:
x + (1 if (y % 2 == 0) else 0),
s, 0)
1
- 127. >>> import itertools
>>> s = ['a', 'b', 'c', 'd']
>>> zip(s, itertools.count())
[('a', 0), ('b', 1), ('c', 2), ('d', 3)]
>>> filter(lambda x: x[1] != 1,
zip(s, itertools.count()))
[('a', 0), ('c', 2), ('d', 3)]
- 128. >>> import itertools
>>> s = ['a', 'b', 'c', 'd']
>>> zip(s, itertools.count())
[('a', 0), ('b', 1), ('c', 2), ('d', 3)]
>>> filter(lambda x: x[1] != 1,
zip(s, itertools.count()))
[('a', 0), ('c', 2), ('d', 3)]
- 129. >>> import itertools
>>> s = ['a', 'b', 'c', 'd']
>>> zip(s, itertools.count())
[('a', 0), ('b', 1), ('c', 2), ('d', 3)]
>>> filter(lambda x: x[1] != 1,
zip(s, itertools.count()))
[('a', 0), ('c', 2), ('d', 3)]
- 132. >>> [x for x in [1, 2, 3, 4, 5]
if (x % 2 != 0)]
[1, 3, 5]
var l = from x
in new List<int> { 1, 2, 3, 4, 5 }
where x % 2 != 0
select x;
- 133. >>> [x * 2 for x in [1, 2, 3, 4, 5]
if (x % 2 != 0)]
[2, 6, 10]
var l = from x
in new List<int> { 1, 2, 3, 4, 5 }
where x % 2 != 0
select x * 2;
- 135. def cor(f):
i = 0
while True:
i = f(i)
yield i
l = cor(lambda x: x + 4)
[l.next() for x in range(0, 3)]
[4, 8, 12]
- 136. def cor(f):
i = 0
while True:
i = f(i)
yield i
l = cor(lambda x: x + 4)
[l.next() for x in range(0, 3)]
[4, 8, 12]
- 137. def cor(f):
i = 0
while True:
i = f(i)
yield i
l = cor(lambda x: x + 4)
[l.next() for x in range(0, 3)]
[4, 8, 12]
- 138. def cor(f):
i = 0
while True:
i = f(i)
yield i
l = cor(lambda x: x + 4)
[l.next() for x in range(0, 3)]
[4, 8, 12]
- 139. def cor(f):
i = 0
while True:
i = f(i)
yield i
l = cor(lambda x: x + 4)
[l.next() for x in range(0, 3)]
[4, 8, 12]
- 140. def cor(f):
i = 0
while True:
i = f(i)
yield i
l = cor(lambda x: x + 4)
[l.next() for x in range(0, 3)]
[4, 8, 12]
- 141. def cor(f):
i = 0
while True:
i = f(i)
yield i
l = cor(lambda x: x + 4)
[l.next() for x in range(0, 3)]
[4, 8, 12]
- 142. IEnumerable<int> cor(Func<int, int> f) {
int i = 0;
while (true) {
i = f(i);
yield return i;
}
}
var li = cor(x => x + 4);
var ls = from x in li.Take(3)
select "<" + x.ToString() + ">";
[“<4>”, “<8>”, “<12>”]
- 154. import static java.util.Arrays.asList;
import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*;
selected =
filter(greaterThan(3),
asList(1, 2, 3, 4, 5));
[4, 5]
List<String> fruits =
asList("apple", "orange", "banana");
selected =
filter(startsWith("a"), fruits);
[“apple”]
- 155. import static java.util.Arrays.asList;
import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*;
selected =
filter(greaterThan(3),
asList(1, 2, 3, 4, 5));
[4, 5]
List<String> fruits =
asList("apple", "orange", "banana");
selected =
filter(startsWith("a"), fruits);
[“apple”]
- 156. import static java.util.Arrays.asList;
import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*;
selected =
filter(greaterThan(3),
asList(1, 2, 3, 4, 5));
[4, 5]
List<String> fruits =
asList("apple", "orange", "banana");
selected =
filter(startsWith("a"), fruits);
[“apple”]
- 157. import static java.util.Arrays.asList;
import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*;
selected =
filter(greaterThan(3),
asList(1, 2, 3, 4, 5));
[4, 5]
List<String> fruits =
asList("apple", "orange", "banana");
selected =
filter(startsWith("a"), fruits);
[“apple”]
- 158. import static java.util.Arrays.asList;
import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*;
selected =
filter(greaterThan(3),
asList(1, 2, 3, 4, 5));
[4, 5]
List<String> fruits =
asList("apple", "orange", "banana");
selected =
filter(startsWith("a"), fruits);
[“apple”]
- 159. selected = convert(fruits,
new Converter<String, String>() {
public String convert(String arg0) {
return arg0.toUpperCase();
}
});
[“APPLE”, “ORANGE”, “BANANA”]
Closure c = closure(); {
of(System.out).println(var(String.class));
}
c.apply("foo");
c.each(fruits);
- 160. selected = convert(fruits,
new Converter<String, String>() {
public String convert(String arg0) {
return arg0.toUpperCase();
}
});
[“APPLE”, “ORANGE”, “BANANA”]
Closure c = closure(); {
of(System.out).println(var(String.class));
}
c.apply("foo");
c.each(fruits);
- 161. selected = convert(fruits,
new Converter<String, String>() {
public String convert(String arg0) {
return arg0.toUpperCase();
}
});
[“APPLE”, “ORANGE”, “BANANA”]
Closure c = closure(); {
of(System.out).println(var(String.class));
}
c.apply("foo");
c.each(fruits);
- 162. selected = convert(fruits,
new Converter<String, String>() {
public String convert(String arg0) {
return arg0.toUpperCase();
}
});
[“APPLE”, “ORANGE”, “BANANA”]
Closure c = closure(); {
of(System.out).println(var(String.class));
}
c.apply("foo");
c.each(fruits);
- 163. selected = convert(fruits,
new Converter<String, String>() {
public String convert(String arg0) {
return arg0.toUpperCase();
}
});
[“APPLE”, “ORANGE”, “BANANA”]
Closure c = closure(); {
of(System.out).println(var(String.class));
}
c.apply("foo");
c.each(fruits);
- 165. import static fj.data.Array.*;
import static fj.function.Integers.*;
import fj.data.Array;
Array<Integer> a = array(1, 2, 3, 4, 5, 6);
Array<Integer> b =
a.filter(even).map(add.f(10));
[12, 14, 16]