TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Go enjoy Python3

106 pointsby crncostaover 9 years ago

16 comments

crawshawover 9 years ago
There are several ways to solve this in Go. The first that comes to mind, assuming you want to truncate to the first 12 runes, not bytes:<p><pre><code> func main() { v := []rune(os.Args[1]) if len(v) &gt; 12 { v = v[:12] } fmt.Println(string(v)) } </code></pre> Or more in the spirit of the C example in the post:<p><pre><code> func main() { res := make([]rune, 12) copy(res, []rune(os.Args[1])) fmt.Println(string(res)) } </code></pre> Note that res will stay on the stack, just like C.<p>I expect the author is trying to say something about Go that I&#x27;m not quite getting. Perhaps that it is not an expression-based language, so to make code readable you need to make use of multiple statements. That&#x27;s by design, but I understand it may be unappealing if you want to program in an expression-heavy style.
评论 #10129732 未加载
评论 #10129863 未加载
评论 #10129738 未加载
masklinnover 9 years ago
&gt; Simple enough, in essence given first argument, print it up to length 12. As an added this also deals with unicode correctly<p>That&#x27;s not true, Python 3 uses codepoint-based indexing but it will break if combining characters are involved. For instance:<p><pre><code> &gt; python3 test.py देवनागरीदेवनागरी देवनागरीदेवन </code></pre> because there is no precombined version of the multi-codepoint grapheme clusters so some of these 10 user-visible characters takes more than a single you end up with 8 user-visible characters rather than the expected 10.<p>edit: the original version used the input string &quot;ǎěǐǒǔa̐e̐i̐o̐u̐ȃȇȋȏȗ&quot; where clusters turn out to have precomposed versions after all. Replaced it by devanāgarī repeated once (in the devanāgarī script)
评论 #10129623 未加载
评论 #10131119 未加载
评论 #10129699 未加载
评论 #10130920 未加载
flohofwoeover 9 years ago
Doesn&#x27;t the C version have a serious bug? If the input string has 12 or more characters, the destination string will not be zero-terminated.<p>From the strncpy docs:<p>&quot;No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).&quot;
评论 #10130388 未加载
Ianvdlover 9 years ago
The author awards some arbitrary points to C even though his implementation of the solution is broken. His similarly poor Go implementation receives zero of these arbitrary points.<p>Why does this deserve the attention of everyone here? The author did not compare languages, he compared his aptitude with these languages, and considered broken implementations to somehow be comparable.<p>A more meaningful comparison would be to implement simple, efficient, <i>working</i> solutions to these problems and comparing them. This, as it stands, does not lead to any useful discussion.
BinaryIdiotover 9 years ago
I&#x27;m not sure what the takeaway is from this blog entry. Is it that Python 3 can do substrings easier than the other languages therefore we should use Python 3? That was what I thought it was, anyway.<p>Seems silly to pick a language based off this single, silly criteria otherwise why not JavaScript or probably other languages that can make the code even smaller?<p>console.log(mystring.substring(0, 12));<p>So it just seems arbitrary and weak in my opinion.
评论 #10130095 未加载
_kst_over 9 years ago
There are at least three major flaws in the 7-line C program, even ignoring character set issues. (main returns int, argv[1] can be null, and strncpy doesn&#x27;t always null-terminate the target). If you&#x27;re going to compare languages, you should find someone who knows each of them well.
评论 #10130692 未加载
评论 #10130693 未加载
Daishimanover 9 years ago
The Unicode situation in most languages is dismal.<p>Honestly though, the lack of generics for that Math.min function makes me happy I&#x27;m not programming in Go.
评论 #10131094 未加载
BossHoggover 9 years ago
Article content aside, the slide out side menu that covers the scroll bar is incredibly annoying. Is that Blogger? Whatever it is needs to stop. Now.
Sir_Cmpwnover 9 years ago
The C code there fails if the unicode string includes characters whose width is greater than one octet.
评论 #10129630 未加载
darkstalkerover 9 years ago
Rust version:<p><pre><code> fn main() { if let Some(arg) = std::env::args().nth(1) { println!(&quot;{}&quot;, arg.chars().take(12).collect::&lt;String&gt;()); &#x2F;&#x2F; chars() iteraters over codepoints } }</code></pre>
评论 #10132787 未加载
Skunkletonover 9 years ago
How is this on the front page of hacker news? What a shit post.
edoficover 9 years ago
A mandatory smart-ass Haskell response<p><pre><code> import System.Environment (getArgs) main = do [str] &lt;- getArgs putStrLn $ take 12 str</code></pre>
评论 #10130343 未加载
评论 #10130468 未加载
评论 #10130368 未加载
_pmf_over 9 years ago
Of course, the C version could be just<p><pre><code> printf(&quot;(%.12s)\n&quot;, argv[1]);</code></pre>
评论 #10129744 未加载
jackieliiover 9 years ago
why can&#x27;t I downvote this!!! erhhhh
IshKebabover 9 years ago
Now try distributing your Python code as a single statically linked exe.
评论 #10130515 未加载
chapiumover 9 years ago
Completely off topic, so if you are looking for discussion about the article skip this.<p>The low contrast ratio and bright colors on this blog are a bit hard to read. I normally switch to readability mode in safari when I encounter this, but the sites layout prevents this from working.
评论 #10129748 未加载
评论 #10129943 未加载