在 Mathematica 中创建可变长度的空格字符串
以下 Mathematica 函数 f 创建一个长度为 n 的空白字符串。
f[n_]:=Fold[StringJoin,"",Array[" "&,n]]
必须有无数种替代方案来创建此功能。
你会怎么做呢?
The following Mathematica function f creates a string of whitespace of length n.
f[n_]:=Fold[StringJoin,"",Array[" "&,n]]
There must be a zillion alternatives to create this function.
How would you have done it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:由于@与@@一样惯用,并且速度更快(感谢Mr.Wizard进行基准测试)并且更短,因此我更新了解决方案。
Edit: since @ is as idiomatic as @@ and a bit faster (thanks to Mr.Wizard for benchmarking) and shorter i updated the solution.
顺便说一句:您应该知道 faq 中不赞成这种类型的问题:
如果问题已结束,请不要感到惊讶。
By the way: you should be aware that this type of question is frowned upon in the faq:
Don't be surprised if the question is closed.
Spacer
和Invisible
对于创建空白也很有用,只是指定空格大小的方式有所不同。Spacer
andInvisible
are also be useful for creating whitespace, with differences in how you specify the size of the space.这大约是 Thies Heidecke 函数的两倍,但远不及 Sjoerd 函数快。对于较大的
n
,较长的初始字符串会很有帮助。这比 Sjoerd 的n > 方法更快。 10000 :
This is about twice as fast as Thies Heidecke's function,but not nearly as fast as Sjoerd's.For large
n
a longer initial string is helpful. This is faster than Sjoerd's method forn > 10000
: