如何使这个斐波那契数列生成器更加优雅?

huangapple go评论108阅读模式
英文:

How do I make this Fibonacci sequence generator more elegant?

问题

以下是你提供的代码的中文翻译:

  1. package main
  2. import "fmt"
  3. func fib_seq() func() int {
  4. n0, n1 := 0, 1
  5. return func() int {
  6. result := n0
  7. n0, n1 = n1, n0 + n1
  8. return result
  9. }
  10. }
  11. func main() {
  12. f := fib_seq()
  13. for i := 0; i < 10; i++ {
  14. fmt.Println(f())
  15. }
  16. }

这是一个生成斐波那契数列的程序。result 的定义是不需要的(但是必要的)。

我想知道在 return 之后是否有一种方法可以执行 x, y = y, x + y

英文:
  1. package main
  2. import &quot;fmt&quot;
  3. func fib_seq() func() int {
  4. n0, n1 := 0, 1
  5. return func() int {
  6. result := n0
  7. n0, n1 = n1, n0 + n1
  8. return result
  9. }
  10. }
  11. func main() {
  12. f := fib_seq()
  13. for i := 0; i &lt; 10; i++ {
  14. fmt.Println(f())
  15. }
  16. }

This is my Fibonacci sequence generator. The definition of result is unwanted (but necessary).

I wonder is there a way to execute x, y = y, x + y after return?

答案1

得分: 3

你可能想要看一下defer

  1. func fib_seq() func() int {
  2. n0, n1 := 0, 1
  3. return func() int {
  4. defer func() {
  5. n0, n1 = n1, n0 + n1
  6. }()
  7. return n0
  8. }
  9. }
英文:

You may want to take a look at defer:

  1. func fib_seq() func() int {
  2. n0, n1 := 0, 1
  3. return func() int {
  4. defer func() {
  5. n0, n1 = n1, n0 + n1
  6. }()
  7. return n0
  8. }
  9. }

答案2

得分: 2

命名返回。但是你已经有了足够可读的内容。

  1. func fib_seq() func() int {
  2. n0, n1 := 0, 1
  3. return func() (r int) {
  4. r, n0, n1 = n0, n1, n0 + n1
  5. return
  6. }
  7. }
英文:

Named return. But what you have is already readable enough.

  1. func fib_seq() func() int {
  2. n0, n1 := 0, 1
  3. return func() (r int) {
  4. r, n0, n1 = n0, n1, n0 + n1
  5. return
  6. }
  7. }

答案3

得分: 0

个人而言,我更喜欢以下的代码(为了可读性):

  1. type fib struct{ n0, n1 int }
  2. func (f *fib) next() int {
  3. defer func() {
  4. f.n0, f.n1 = f.n1, f.n0+f.n1
  5. }()
  6. return f.n0
  7. }
  8. func main() {
  9. fib := &fib{0, 1}
  10. for i := 0; i < 10; i++ {
  11. fmt.Println(fib.next())
  12. }
  13. }

请注意,我只提供了代码的翻译部分,不包括其他内容。

英文:

Personally, I'd prefer the following (for readability):

  1. type fib struct{ n0, n1 int }
  2. func (f *fib) next() int {
  3. defer func() {
  4. f.n0, f.n1 = f.n1, f.n0+f.n1
  5. }()
  6. return f.n0
  7. }
  8. func main() {
  9. fib := &amp;fib{0, 1}
  10. for i := 0; i &lt; 10; i++ {
  11. fmt.Println(fib.next())
  12. }
  13. }

答案4

得分: 0

“优雅”对不同的人来说有不同的含义。对一些人来说,它可能意味着“简洁”,对其他人来说可能意味着“简单”或“易读”。

这是我的理解:

  1. public class Fibonacci {
  2. private static int x;
  3. private static int y;
  4. private static int z;
  5. public static void main(String[] args) {
  6. x = 0;
  7. y = 1;
  8. z = 0;
  9. System.out.println(x);
  10. System.out.println(y);
  11. while (z < 100) {
  12. z = x + y;
  13. System.out.println(z);
  14. x = y;
  15. y = z;
  16. }
  17. }
  18. }

如你所见,我更注重代码的可读性而非复杂性 如何使这个斐波那契数列生成器更加优雅?

英文:

"Elegant" means different things to different people. To some it may mean "conciseness," and to others it may mean "simplicity," or "readability."

Here is my take on it:

  1. public class Fibonacci {
  2. private static int x;
  3. private static int y;
  4. private static int z;
  5. public static void main(String[] args) {
  6. x = 0;
  7. y = 1;
  8. z = 0;
  9. System.out.println(x);
  10. System.out.println(y);
  11. while (z &lt; 100) {
  12. z = x + y;
  13. System.out.println(z);
  14. x = y;
  15. y = z;
  16. }
  17. }
  18. }

As you can see, I favor readability over sophistication 如何使这个斐波那契数列生成器更加优雅?

huangapple
  • 本文由 发表于 2017年5月29日 20:42:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/44242803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定