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.

Recreating Python's Slice Syntax in JavaScript Using ES6 Proxies

77 pointsby foobalmost 7 years ago

14 comments

xg15almost 7 years ago
I understand the idea behind the article - explain proxies and have a fun project to do so, so it doesn&#x27;t get boring as hell - but, in general, can we please stop using runtime features of a language to implement syntactic sugar?<p>Actual syntactic suger can make a program more readable, easier to debug and even more efficient. Those kind of hacks are neither.<p>It&#x27;s not more readable because your expressions are limited to the original grammar, so you have to resort to weird compromises. It&#x27;s harder to debug because you add layers of abstraction (like the proxies here) that have no purpose at all except changing how your code looks. And it&#x27;s less efficient because those layers of abstraction will still stay with the program when it&#x27;s being executed even though you only needed them while you were writing the code.<p>So, please, syntactic sugar is awesome and more thought should be given to letting users add their own sugars - but can we do it via metaprogramming and preprocessors and not by hacks like this?<p>(Disclaimer: This pattern annoys me to no end in Java, so it&#x27;s gotten a bit of a pet peeve. No fault to the author of this post who has written an excellent article)
评论 #17423901 未加载
westoncbalmost 7 years ago
I used ES6 proxies recently to write a program which monitors changes on javascript data structures, and generates operation objects reflecting the changes to send to my data structure visualizer project[0].<p>You can see the source for the javascript client that does the monitoring with proxies here: <a href="https:&#x2F;&#x2F;github.com&#x2F;westoncb&#x2F;JS-Watcher&#x2F;blob&#x2F;master&#x2F;watcher.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;westoncb&#x2F;JS-Watcher&#x2F;blob&#x2F;master&#x2F;watcher.j...</a> —it just works for arrays at the moment, but the basic idea is there. (I&#x27;ve written another much more complete client that does monitoring in Java; this JS one is more experimental.)<p>[0] <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=KwZmAgAuIkY" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=KwZmAgAuIkY</a>
评论 #17423907 未加载
spraakalmost 7 years ago
Recently I implemented a wrapper function that given an object returns a Proxy which has a get method that recursively calls itself with the accessed object path and finally returns the object and logs the full path. (The code is not complicated and only about as long as this post, but it&#x27;s hard to type code on a phone)<p>The reason for this is to give me logging for this part of the application to aid in extracting it out to a separate service. That is, to see what from this part of the application is actually being used&#x2F;called.<p>Edit: I meant to say that I wanted to share this because previously I hadn&#x27;t yet found an interesting use for a Proxy.<p>Also, I thought of another use case: in the get method you could return a percentage of the time a different object, i.e. an A&#x2F;B test.
评论 #17422971 未加载
zawerfalmost 7 years ago
&gt; My proposed solution here is to try to capture the spirit of Python’s slice syntax rather than the exact syntax itself. We can accomplish this using an alternative syntax where we use double square brackets and commas in place of colons: array[::-1] =&gt; array[[,,-1]], array[:n] =&gt; array[[,n]], array[-5::2] =&gt; array[[-5,,2]], and so on. This might not be quite as concise as Python’s syntax, but it only requires two extra keystrokes per slice and I think it’s as close as we’re gonna get.<p>I wonder if it makes more sense to write a babel plugin to support the python syntax. Since everyone using es6 features are transpiling anyway.
评论 #17421888 未加载
评论 #17422564 未加载
rollulusalmost 7 years ago
Great PoC, but please, don&#x27;t do this. At my first gig I had to work on a huge Lua code base, where the original author did tricks like this, effectively creating a new language. Learning by example how things should not be done is effective, but the suffering wasn&#x27;t worth it.
aoeusnth1almost 7 years ago
Fun, I learned something today! I imagine the performance of proxies is pretty terrible, so this is just a toy - but it could come in handy for implementing equivalents of some of the nice syntax sugar that numpy and tensorflow have in JavaScript.
评论 #17421786 未加载
_0w8talmost 7 years ago
I wonder why not to use the call syntax for JS slices? I.e. use a(1, 3) to mean a[1:3]? The drawback is that 0 at the beginning of the slice cannot be skipped, but things like a() in place of a[:] or a(i, j, k) in place of a[i:j:k] will work.
评论 #17424193 未加载
abdulkareemsnalmost 7 years ago
We have a legacy angular2+ project (I know angular2+ is not that old) where we use a global object to store state. We are using ngrx in new code , but still need to access that monstrous global object. As other part of code is still updating that.<p>I&#x27;m thinking to replace that global object with recursive proxies so that all updates can be forwarded to ngrx store<p>And slowly replace all reads with ngrx selectors
hasahmedalmost 7 years ago
&quot;array[`${start}:${stop}:${step}`]. That’s not syntactic sugar… it’s syntactic aspartame at best.&quot; I lol&#x27;d
juansgaitanalmost 7 years ago
Fan of katas? <a href="https:&#x2F;&#x2F;www.codewars.com&#x2F;kata&#x2F;5a6c75c8fd5777274e000042" rel="nofollow">https:&#x2F;&#x2F;www.codewars.com&#x2F;kata&#x2F;5a6c75c8fd5777274e000042</a>
sun41almost 7 years ago
Just by looking at the implementation I dare to bet that this is the most expensive slice ever invented in JS. And all that conversion and processing to only allow for arr[-1], oh my..
jwattealmost 7 years ago
But why?
评论 #17422645 未加载
评论 #17422810 未加载
ameliusalmost 7 years ago
Can anyone summarize what the resulting syntax looks like?
评论 #17424287 未加载
cyberpunk0almost 7 years ago
Why do so many JavaScript devs use 2 spaces for tabs? Its terrible for quickly reading through code
评论 #17424543 未加载
评论 #17423508 未加载
评论 #17423820 未加载
评论 #17422589 未加载
评论 #17422661 未加载