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

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

How do I make this Fibonacci sequence generator more elegant?

问题

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

package main

import "fmt"

func fib_seq() func() int {
	n0, n1 := 0, 1

	return func() int {
		result := n0
		n0, n1 = n1, n0 + n1
		return result
	}
}

func main() {
	f := fib_seq()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

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

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

英文:
package main

import &quot;fmt&quot;

func fib_seq() func() int {
	n0, n1 := 0, 1

	return func() int {
		result := n0
		n0, n1 = n1, n0 + n1
		return result
	}
}

func main() {
	f := fib_seq()
	for i := 0; i &lt; 10; i++ {
		fmt.Println(f())
	}
}

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

func fib_seq() func() int {
    n0, n1 := 0, 1

    return func() int {
        defer func() {
            n0, n1 = n1, n0 + n1
        }()

        return n0
    }
}
英文:

You may want to take a look at defer:

func fib_seq() func() int {
    n0, n1 := 0, 1

    return func() int {
        defer func() {
            n0, n1 = n1, n0 + n1
        }()

        return n0
    }
}

答案2

得分: 2

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

func fib_seq() func() int {
    n0, n1 := 0, 1

    return func() (r int) {
        r, n0, n1 = n0, n1, n0 + n1
        return
    }
}
英文:

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

func fib_seq() func() int {
    n0, n1 := 0, 1

    return func() (r int) {
        r, n0, n1 = n0, n1, n0 + n1
        return
    }
}

答案3

得分: 0

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

type fib struct{ n0, n1 int }

func (f *fib) next() int {
    defer func() { 
        f.n0, f.n1 = f.n1, f.n0+f.n1 
    }()
    return f.n0
}

func main() {
    fib := &fib{0, 1}
    for i := 0; i < 10; i++ {
        fmt.Println(fib.next())
    }
}

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

英文:

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

type fib struct{ n0, n1 int }

func (f *fib) next() int {
	defer func() { 
        f.n0, f.n1 = f.n1, f.n0+f.n1 
    }()
	return f.n0
}

func main() {
	fib := &amp;fib{0, 1}
	for i := 0; i &lt; 10; i++ {
		fmt.Println(fib.next())
	}
}

答案4

得分: 0

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

这是我的理解:

public class Fibonacci {

	private static int x;
	private static int y;
	private static int z;

	public static void main(String[] args) {
		x = 0;
		y = 1;
		z = 0;

		System.out.println(x);
		System.out.println(y);
		while (z < 100) {
			z = x + y;
			System.out.println(z);
			x = y;
			y = z;
		}
		
	}
}

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

英文:

"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:

public class Fibonacci {

	private static int x;
	private static int y;
	private static int z;

	public static void main(String[] args) {
		x = 0;
		y = 1;
		z = 0;

		System.out.println(x);
		System.out.println(y);
		while (z &lt; 100) {
			z = x + y;
			System.out.println(z);
			x = y;
			y = z;
		}
		
	}
}

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:

确定