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.

Beautiful Binary Search in D

72 pointsby alex_muscarover 2 years ago

7 comments

theodpHNover 2 years ago
Beauty is in the eye of the beholder. While clever, this is (IMO) far less understandable and more error-prone than say COBOL&#x27;s positively ancient built-in SEARCH ALL binary search statement. Here&#x27;s an example of a 3-key binary table search from IBM&#x27;s COBOL documentation that includes options for found and not found conditions.<p><pre><code> SEARCH ALL TABLE-ENTRY AT END PERFORM NOENTRY WHEN KEY-1 (INDX-1) = VALUE-1 AND KEY-2 (INDX-1) = VALUE-2 AND KEY-3 (INDX-1) = VALUE-3 MOVE PART-1 (INDX-1) TO OUTPUT-AREA END-SEARCH </code></pre> <a href="https:&#x2F;&#x2F;www.ibm.com&#x2F;docs&#x2F;en&#x2F;cobol-zos&#x2F;6.1?topic=all-example-binary-search" rel="nofollow">https:&#x2F;&#x2F;www.ibm.com&#x2F;docs&#x2F;en&#x2F;cobol-zos&#x2F;6.1?topic=all-example-...</a>
评论 #34850050 未加载
评论 #34849679 未加载
alex_muscarover 2 years ago
Where I use D&#x27;s metaprogramming capabilities to generate a binary search function using Shar&#x27;s algorithm.
Kukumberover 2 years ago
D is a refreshing language in the sea of &quot;alternative to C languages&quot;, they didn&#x27;t reinvent the syntax, so if you come from C, using D will feel very natural and at home! (and you&#x27;ll be protected from most unsafe&#x2F;ub quirks of C)<p>When it comes to metaprogramming, it&#x27;s excellent, but it is a double edged sword, it can be useful, but if abused it&#x27;ll tank your build speed, so one must find a proper balance to avoid pain later<p>One of the few languages that offers its own backend! (LLVM&#x2F;GCC backends are also available)<p>I love this level of independence, a real labor of love, one of the best better C, i&#x27;d even say this is the evolution C needed
评论 #34854587 未加载
skocznymrocznyover 2 years ago
Unless I am missing something, you can just use .length on a static array:<p><pre><code> int[5] a = [1,2,3,4,5]; writeln(a.length); &#x2F;&#x2F; 5</code></pre>
评论 #34850074 未加载
评论 #34849478 未加载
kazinatorover 2 years ago
Reminds me of <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Successive-approximation_ADC" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Successive-approximation_ADC</a>
wood_spiritover 2 years ago
Can Shar’s unrolling be combined with Duff’s device to work on any size of dynamic array? And how would it perform?
gavinrayover 2 years ago
D is the systems language I enjoy writing the most out of C++, Rust, and Go.<p>It feels a lot like a mix between C# and JS, but at the low level access of C++.<p>It has a lot of unique features but the &quot;killer feature&quot; by far for me is Contract Programming and Invariants<p><a href="https:&#x2F;&#x2F;tour.dlang.org&#x2F;tour&#x2F;en&#x2F;gems&#x2F;contract-programming" rel="nofollow">https:&#x2F;&#x2F;tour.dlang.org&#x2F;tour&#x2F;en&#x2F;gems&#x2F;contract-programming</a><p>Invariants changed my life, so much so that I built a GCC plugin to add them to C++<p>It used to be the case that the developer tooling wasn&#x27;t very good, but the &quot;code-d&quot; VS Code plugin has matured greatly in the last few years and has some features you won&#x27;t find even in bigger languages<p>(Like the ability to colorize source code lines with GC profiling info or test coverage status)<p>---<p>Another useful bit of info is that AddressSanitizer and tools like LLVM libFuzzer work with the LDC compiler
评论 #34849152 未加载
评论 #34849475 未加载