Return每个数组使用map的最大数量
Return the maximum number in each array using map
function each(coll, f) {
if (Array.isArray(coll)) {
for (var i = 0; i < coll.length; i++) {
f(coll[i], i);
}
} else {
for (var key in coll) {
f(coll[key], key);
}
}
}
function map(array, f) {
var acc = [];
each(array, function(element, i) {
acc.push(f(element, i));
});
return acc;
}
function max(numbers) {
var maximum = numbers[0];
each(numbers,function(x){
if(x>maximum){
maximum = x;}
});
return maximum;
}
function maximums(arrays){
return map(arrays, function(x){
return max(arrays);
})
}
maximums([1,2,3],[5,6,7])
我不太懂地图。我在每个数组函数中写了一个最大数量,我想实现它来映射。我知道我没有 return 使用 return max(arrays) 做正确的事,但我也试过 return max(arrays[x]) 来强调我想遍历数组的整个参数。 return max(arrays) ==> [3,3,3], returns 是一个数组中最大的数字 3 次,我也不明白为什么。
你试过return max(x);
了吗?我想这可能就是你想要的。 javascript 中还有一个 Max() 所以你不需要定义它:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
您需要从 x
中获取最大值
function maximums(arrays) {
return map(arrays, function (x) {
return max(x);
// ^
});
}
并用数组数组调用函数
maximums([[1, 2, 3], [5, 6, 7]]);
// ^ ^
function each(coll, f) {
if (Array.isArray(coll)) {
for (var i = 0; i < coll.length; i++) {
f(coll[i], i);
}
} else {
for (var key in coll) {
f(coll[key], key);
}
}
}
function map(array, f) {
var acc = [];
each(array, function (element, i) {
acc.push(f(element, i));
});
return acc;
}
function max(numbers) {
var maximum = numbers[0];
each(numbers, function (x) {
if (x > maximum) {
maximum = x;
}
});
return maximum;
}
function maximums(arrays) {
return map(arrays, function (x) {
return max(x);
})
}
console.log(maximums([[1, 2, 3], [5, 6, 7]]));
对于其中的大部分功能,您无需自行实施。像这样将 Math.max
与 Array#map
结合使用:
function maximum (array) {
return Math.max.apply(null, array)
}
console.log(
[[1,2,3], [5,6,7]].map(maximum)
) //=> [3, 7]
// if you still want a `maximums` function
function maximums (arrays) {
return arrays.map(maximum)
}
console.log(
maximums([[1,2,3], [5,6,7]]))
) //=> [3, 7]
您可以组合 map
和 reduce
函数:
function maximums(x) {
return x.map(function(y){
return y.reduce(function(a,b){ return Math.max(a,b); });
});
}
您的代码基于错误的构建块 (each
),因此您的其余代码会受到影响。我的回答将从一个更好的构建块开始,reduce
(又名 foldLeft
),并向您展示如何从那里构建。
关于这个答案,您会注意到我们将每个功能分解成非常简单的部分,然后使用 higher-order functions 将所有内容组合在一起。
- 避免使用
each
这会限制我们使用副作用函数
- 无需检查
Array.isArray
- 无命令式风格
for
-带有可变迭代器的循环,i
- 不需要检查数组
.length
属性
我强烈建议您逐步完成此代码的评估,看看所有部分是如何工作的。如果您能够了解这些程序中的任何一个是如何工作的,那么您将能够很好地掌握函数式编程的一些最重要的基础知识:递归、不变性、引用透明性、higher-order functions、柯里化和函数组合†
ES6 提供了 arrow functions, destructuring assignment, and spread syntax,这使得在 JavaScript 中编写函数式程序变得轻而易举——虽然一开始读起来会感觉很不一样。我将在此代码段下方提供 ES6 之前的版本。
相关: What do multiple arrow functions mean in JavaScript?
// reduce is your new, gold-standard building block
const reduce = f => y => ([x,...xs]) => {
if (x === undefined)
return y
else
return reduce (f) (f (y) (x)) (xs)
}
// derive map from reduce
const map = f => reduce (acc => x => [...acc, f(x)]) ([])
// get max of 2 numbers
const max = x => y => x > y ? x : y
// get max of a list of numbers
const maximum = reduce (max) (-Infinity)
// get each max of a list of a list of numbers
const maximums = map (maximum)
// see the result of your hard work
console.log(maximums ([ [ 1, 3, 2 ], [ 7, 5, 6 ] ]))
// => [ 3, 7 ]
如果您在上面的代码上遇到困难,这段代码(下面)是用 ES5(更准确地说,ES6 之前的版本)编写的。更熟悉的语法可能会在您还在磨牙的时候帮助您。它在 ES5 中更冗长,但它的工作方式(几乎)相同。
// reduce is your new, gold-standard building block
function reduce (f) {
return function (y) {
return function (xs) {
if (xs.length === 0)
return y
else
return reduce (f) (f (y) (xs[0])) (xs.slice(1))
}
}
}
// derive map from reduce
function map (f) {
return reduce (function (acc) {
return function (x) {
return acc.concat([ f(x) ])
}
}) ([])
}
// get max of 2 numbers
function max (x) {
return function (y) {
return x > y ? x : y
}
}
// get max of a list of numbers
var maximum = reduce (max) (-Infinity);
// get each max of a list of a list of numbers
var maximums = map (maximum);
// see the result of your hard work
console.log(maximums ([ [ 1, 3, 2 ], [ 7, 5, 6 ] ]))
// => [ 3, 7 ]
†当然还有函数式编程的其他基础知识,但这无疑是一个好的开始
还是卡住了?
reduce
无疑是上述程序中最复杂的函数。如果您正在为它苦苦挣扎,我们可以稍微作弊以简化我们对程序的理解。 JavaScript 提供了一个内置的 Array.prototype.reduce ,它(几乎)工作相同。我们可以使用 JavaScript 编写我们的 reduce
并免费获得其余部分!
还有一件事。 JavaScript 的 reduce 期望 binary function but the rest of our program is expecting curried functions (sequences of unary functions). To get around that, we will first make a small uncurry
combinator 将所有内容组合在一起
// convert sequence of unary functions to a binary function
const uncurry = f => (x,y) => f (x) (y)
// we can cheat using JavaScript built-in reduce with uncurry
const reduce = f => y => xs => xs.reduce(uncurry(f), y)
// the rest stays the same !
// ...
// derive map from reduce
const map = f => reduce (acc => x => [...acc, f(x)]) ([])
// get max of 2 numbers
const max = x => y => x > y ? x : y
// get max of a list of numbers
const maximum = reduce (max) (-Infinity)
// get each max of a list of a list of numbers
const maximums = map (maximum)
// see the result of your hard work
console.log(maximums ([ [ 1, 3, 2 ], [ 7, 5, 6 ] ]))
// => [ 3, 7 ]
额外学分 1
所以我告诉过你 reduce
是你的 黄金标准 积木。除了 map
,您知道您还可以使用 reduce
来实现无数其他功能吗?其中包括:filter
、find
、some
、every
、keys
和 entries
.
额外学分 2
Extra Credit 1中提到的一些函数应该short-circuit在到达数组末尾之前返回最终答案。哪些可以短路?我们如何重写 reduce
来促进这种提前退出行为?
function each(coll, f) {
if (Array.isArray(coll)) {
for (var i = 0; i < coll.length; i++) {
f(coll[i], i);
}
} else {
for (var key in coll) {
f(coll[key], key);
}
}
}
function map(array, f) {
var acc = [];
each(array, function(element, i) {
acc.push(f(element, i));
});
return acc;
}
function max(numbers) {
var maximum = numbers[0];
each(numbers,function(x){
if(x>maximum){
maximum = x;}
});
return maximum;
}
function maximums(arrays){
return map(arrays, function(x){
return max(arrays);
})
}
maximums([1,2,3],[5,6,7])
我不太懂地图。我在每个数组函数中写了一个最大数量,我想实现它来映射。我知道我没有 return 使用 return max(arrays) 做正确的事,但我也试过 return max(arrays[x]) 来强调我想遍历数组的整个参数。 return max(arrays) ==> [3,3,3], returns 是一个数组中最大的数字 3 次,我也不明白为什么。
你试过return max(x);
了吗?我想这可能就是你想要的。 javascript 中还有一个 Max() 所以你不需要定义它:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
您需要从 x
function maximums(arrays) {
return map(arrays, function (x) {
return max(x);
// ^
});
}
并用数组数组调用函数
maximums([[1, 2, 3], [5, 6, 7]]);
// ^ ^
function each(coll, f) {
if (Array.isArray(coll)) {
for (var i = 0; i < coll.length; i++) {
f(coll[i], i);
}
} else {
for (var key in coll) {
f(coll[key], key);
}
}
}
function map(array, f) {
var acc = [];
each(array, function (element, i) {
acc.push(f(element, i));
});
return acc;
}
function max(numbers) {
var maximum = numbers[0];
each(numbers, function (x) {
if (x > maximum) {
maximum = x;
}
});
return maximum;
}
function maximums(arrays) {
return map(arrays, function (x) {
return max(x);
})
}
console.log(maximums([[1, 2, 3], [5, 6, 7]]));
对于其中的大部分功能,您无需自行实施。像这样将 Math.max
与 Array#map
结合使用:
function maximum (array) {
return Math.max.apply(null, array)
}
console.log(
[[1,2,3], [5,6,7]].map(maximum)
) //=> [3, 7]
// if you still want a `maximums` function
function maximums (arrays) {
return arrays.map(maximum)
}
console.log(
maximums([[1,2,3], [5,6,7]]))
) //=> [3, 7]
您可以组合 map
和 reduce
函数:
function maximums(x) {
return x.map(function(y){
return y.reduce(function(a,b){ return Math.max(a,b); });
});
}
您的代码基于错误的构建块 (each
),因此您的其余代码会受到影响。我的回答将从一个更好的构建块开始,reduce
(又名 foldLeft
),并向您展示如何从那里构建。
关于这个答案,您会注意到我们将每个功能分解成非常简单的部分,然后使用 higher-order functions 将所有内容组合在一起。
- 避免使用
each
这会限制我们使用副作用函数 - 无需检查
Array.isArray
- 无命令式风格
for
-带有可变迭代器的循环,i
- 不需要检查数组
.length
属性
我强烈建议您逐步完成此代码的评估,看看所有部分是如何工作的。如果您能够了解这些程序中的任何一个是如何工作的,那么您将能够很好地掌握函数式编程的一些最重要的基础知识:递归、不变性、引用透明性、higher-order functions、柯里化和函数组合†
ES6 提供了 arrow functions, destructuring assignment, and spread syntax,这使得在 JavaScript 中编写函数式程序变得轻而易举——虽然一开始读起来会感觉很不一样。我将在此代码段下方提供 ES6 之前的版本。
相关: What do multiple arrow functions mean in JavaScript?
// reduce is your new, gold-standard building block
const reduce = f => y => ([x,...xs]) => {
if (x === undefined)
return y
else
return reduce (f) (f (y) (x)) (xs)
}
// derive map from reduce
const map = f => reduce (acc => x => [...acc, f(x)]) ([])
// get max of 2 numbers
const max = x => y => x > y ? x : y
// get max of a list of numbers
const maximum = reduce (max) (-Infinity)
// get each max of a list of a list of numbers
const maximums = map (maximum)
// see the result of your hard work
console.log(maximums ([ [ 1, 3, 2 ], [ 7, 5, 6 ] ]))
// => [ 3, 7 ]
如果您在上面的代码上遇到困难,这段代码(下面)是用 ES5(更准确地说,ES6 之前的版本)编写的。更熟悉的语法可能会在您还在磨牙的时候帮助您。它在 ES5 中更冗长,但它的工作方式(几乎)相同。
// reduce is your new, gold-standard building block
function reduce (f) {
return function (y) {
return function (xs) {
if (xs.length === 0)
return y
else
return reduce (f) (f (y) (xs[0])) (xs.slice(1))
}
}
}
// derive map from reduce
function map (f) {
return reduce (function (acc) {
return function (x) {
return acc.concat([ f(x) ])
}
}) ([])
}
// get max of 2 numbers
function max (x) {
return function (y) {
return x > y ? x : y
}
}
// get max of a list of numbers
var maximum = reduce (max) (-Infinity);
// get each max of a list of a list of numbers
var maximums = map (maximum);
// see the result of your hard work
console.log(maximums ([ [ 1, 3, 2 ], [ 7, 5, 6 ] ]))
// => [ 3, 7 ]
†当然还有函数式编程的其他基础知识,但这无疑是一个好的开始
还是卡住了?
reduce
无疑是上述程序中最复杂的函数。如果您正在为它苦苦挣扎,我们可以稍微作弊以简化我们对程序的理解。 JavaScript 提供了一个内置的 Array.prototype.reduce ,它(几乎)工作相同。我们可以使用 JavaScript 编写我们的 reduce
并免费获得其余部分!
还有一件事。 JavaScript 的 reduce 期望 binary function but the rest of our program is expecting curried functions (sequences of unary functions). To get around that, we will first make a small uncurry
combinator 将所有内容组合在一起
// convert sequence of unary functions to a binary function
const uncurry = f => (x,y) => f (x) (y)
// we can cheat using JavaScript built-in reduce with uncurry
const reduce = f => y => xs => xs.reduce(uncurry(f), y)
// the rest stays the same !
// ...
// derive map from reduce
const map = f => reduce (acc => x => [...acc, f(x)]) ([])
// get max of 2 numbers
const max = x => y => x > y ? x : y
// get max of a list of numbers
const maximum = reduce (max) (-Infinity)
// get each max of a list of a list of numbers
const maximums = map (maximum)
// see the result of your hard work
console.log(maximums ([ [ 1, 3, 2 ], [ 7, 5, 6 ] ]))
// => [ 3, 7 ]
额外学分 1
所以我告诉过你 reduce
是你的 黄金标准 积木。除了 map
,您知道您还可以使用 reduce
来实现无数其他功能吗?其中包括:filter
、find
、some
、every
、keys
和 entries
.
额外学分 2
Extra Credit 1中提到的一些函数应该short-circuit在到达数组末尾之前返回最终答案。哪些可以短路?我们如何重写 reduce
来促进这种提前退出行为?