将厘米转换为英尺和英寸
Convert centimetres to feet and inches
我正在尝试编写一个将 cm
转换为 feet
和 inches
的函数
cmToFtIn(30) === {"feet": 1, "inches": 0}
cmToFtIn(29) === {"feet": 0, "inches": 11}
我已经完成的就是这个复杂的函数
const cmToFeetMultiplier = multiply(1/30.5)
const ftToInMultiplier = multiply(12)
const floor = (num) => Math.floor(num)
const round = (num) => Math.round(num)
const cmToFtIn = pipe(
cmToFeetMultiplier,
juxt([identity, floor]),
juxt([identity, pipe(apply(subtract), ftToInMultiplier, round)]),
flatten,
cond([
[pipe(nth(2), equals(12)), (d) => ({feet: d[1] + 1, inches: 0})],
[always(true), (d) => ({feet: d[1], inches: d[2]})],
])
)
也许有人对如何简化它有一些建议?
一英寸有 2.54
厘米。因此,我们有等式。
const cmToInches = cm => cm / 2.54;
一英尺有 12
英寸。因此,我们有等式。
const inchesToFtIn = inches => ({
feet: Math.floor(inches / 12),
inches: inches % 12,
});
现在,要将厘米转换为英尺和英寸,我们只需组合这两个函数。
const cmToInches = cm => cm / 2.54;
const inchesToFtIn = inches => ({
feet: Math.floor(inches / 12),
inches: inches % 12,
});
const cmToFtIn = cm => inchesToFtIn(cmToInches(cm));
console.log(cmToFtIn(30.48)); // 1 foot
console.log(cmToFtIn(27.94)); // 11 inches
如果您需要对英寸进行四舍五入,那是一个小改动。
const cmToInches = cm => cm / 2.54;
const inchesToFtIn = inches => ({
feet: Math.floor(inches / 12),
inches: inches % 12,
});
const cmToFtIn = cm => inchesToFtIn(Math.round(cmToInches(cm)));
console.log(cmToFtIn(30)); // 1 foot
console.log(cmToFtIn(29)); // 11 inches
希望对您有所帮助。
当你 google "cm to inch":
和"cm to foot":
然后我们可以构建一个 inch
和 foot
函数:
const inch = flip(divide)(2.54);
const foot = flip(divide)(30.48);
inch(30); //=> 11.811023622047244
foot(30); //=> 0.984251968503937
如果你需要从 cm 中的值 return 一个对象 {inch, foot}
你可以不用 Ramda:
const cmToFtIn = cm => ({inch: inch(cm), foot: foot(cm)});
cmToFtIn(30);
//=> {"foot": 0.984251968503937, "inch": 11.811023622047244}
使用 Ramda:
const cmToFtIn = applySpec({inch, foot});
cmToFtIn(30);
//=> {"foot": 0.984251968503937, "inch": 11.811023622047244}
我个人建议您不要 return 直接从 inch
和 foot
函数舍入值。您可以在需要的地方应用第二遍来将它们四舍五入。两个选项:
在您的 {inch, foot}
对象上应用 Math.round
:
map(Math.round, cmToFtIn(30));
//=> {"foot": 1, "inch": 12}
或组合Math.round
和inch
/foot
函数:
const cmToFtIn = applySpec({
inch: compose(Math.round, inch),
foot: compose(Math.round, foot)
});
cmToFtIn(30);
//=> {"foot": 1, "inch": 12}
除非这是学习 Ramda 的练习,否则我建议 Ramda 不是这项工作的正确工具。我是 Ramda 的作者之一,也是 Ramda 的忠实粉丝,但它是一种旨在帮助使 FP 样式在 JS 中更可口的工具,而不是通用实用程序库。
问题在于这个函数可以用 vanilla JS 非常简单地编写:
const cmToInFt = (cm, inches = Math .round (cm / 2.54)) => ({
feet: Math .floor (inches / 12),
inches: inches % 12
})
const myHeight = cmToInFt (185)
console .log (myHeight)
或者,如果您不喜欢默认参数,它可能是:
const cmToInFt = (cm) => {
const inches = Math .round (cm / 2.54)
return {
feet: Math .floor (inches / 12),
inches: inches % 12
}
}
Ramda 对此无能为力。显然,正如您的方法和 customcommander 的回答所示,您可以使用 Ramda 函数来提供帮助,并且一点点 compose
通常很有用。但这(与 Aadit M Shah 的回答非常相似,结构略有不同)已经很干净且可读了。我看不出有什么能让它更容易使用的。
我正在尝试编写一个将 cm
转换为 feet
和 inches
cmToFtIn(30) === {"feet": 1, "inches": 0}
cmToFtIn(29) === {"feet": 0, "inches": 11}
我已经完成的就是这个复杂的函数
const cmToFeetMultiplier = multiply(1/30.5)
const ftToInMultiplier = multiply(12)
const floor = (num) => Math.floor(num)
const round = (num) => Math.round(num)
const cmToFtIn = pipe(
cmToFeetMultiplier,
juxt([identity, floor]),
juxt([identity, pipe(apply(subtract), ftToInMultiplier, round)]),
flatten,
cond([
[pipe(nth(2), equals(12)), (d) => ({feet: d[1] + 1, inches: 0})],
[always(true), (d) => ({feet: d[1], inches: d[2]})],
])
)
也许有人对如何简化它有一些建议?
一英寸有 2.54
厘米。因此,我们有等式。
const cmToInches = cm => cm / 2.54;
一英尺有 12
英寸。因此,我们有等式。
const inchesToFtIn = inches => ({
feet: Math.floor(inches / 12),
inches: inches % 12,
});
现在,要将厘米转换为英尺和英寸,我们只需组合这两个函数。
const cmToInches = cm => cm / 2.54;
const inchesToFtIn = inches => ({
feet: Math.floor(inches / 12),
inches: inches % 12,
});
const cmToFtIn = cm => inchesToFtIn(cmToInches(cm));
console.log(cmToFtIn(30.48)); // 1 foot
console.log(cmToFtIn(27.94)); // 11 inches
如果您需要对英寸进行四舍五入,那是一个小改动。
const cmToInches = cm => cm / 2.54;
const inchesToFtIn = inches => ({
feet: Math.floor(inches / 12),
inches: inches % 12,
});
const cmToFtIn = cm => inchesToFtIn(Math.round(cmToInches(cm)));
console.log(cmToFtIn(30)); // 1 foot
console.log(cmToFtIn(29)); // 11 inches
希望对您有所帮助。
当你 google "cm to inch":
和"cm to foot":
然后我们可以构建一个 inch
和 foot
函数:
const inch = flip(divide)(2.54);
const foot = flip(divide)(30.48);
inch(30); //=> 11.811023622047244
foot(30); //=> 0.984251968503937
如果你需要从 cm 中的值 return 一个对象 {inch, foot}
你可以不用 Ramda:
const cmToFtIn = cm => ({inch: inch(cm), foot: foot(cm)});
cmToFtIn(30);
//=> {"foot": 0.984251968503937, "inch": 11.811023622047244}
使用 Ramda:
const cmToFtIn = applySpec({inch, foot});
cmToFtIn(30);
//=> {"foot": 0.984251968503937, "inch": 11.811023622047244}
我个人建议您不要 return 直接从 inch
和 foot
函数舍入值。您可以在需要的地方应用第二遍来将它们四舍五入。两个选项:
在您的 {inch, foot}
对象上应用 Math.round
:
map(Math.round, cmToFtIn(30));
//=> {"foot": 1, "inch": 12}
或组合Math.round
和inch
/foot
函数:
const cmToFtIn = applySpec({
inch: compose(Math.round, inch),
foot: compose(Math.round, foot)
});
cmToFtIn(30);
//=> {"foot": 1, "inch": 12}
除非这是学习 Ramda 的练习,否则我建议 Ramda 不是这项工作的正确工具。我是 Ramda 的作者之一,也是 Ramda 的忠实粉丝,但它是一种旨在帮助使 FP 样式在 JS 中更可口的工具,而不是通用实用程序库。
问题在于这个函数可以用 vanilla JS 非常简单地编写:
const cmToInFt = (cm, inches = Math .round (cm / 2.54)) => ({
feet: Math .floor (inches / 12),
inches: inches % 12
})
const myHeight = cmToInFt (185)
console .log (myHeight)
或者,如果您不喜欢默认参数,它可能是:
const cmToInFt = (cm) => {
const inches = Math .round (cm / 2.54)
return {
feet: Math .floor (inches / 12),
inches: inches % 12
}
}
Ramda 对此无能为力。显然,正如您的方法和 customcommander 的回答所示,您可以使用 Ramda 函数来提供帮助,并且一点点 compose
通常很有用。但这(与 Aadit M Shah 的回答非常相似,结构略有不同)已经很干净且可读了。我看不出有什么能让它更容易使用的。