5 ms·
Game of Life in one Ruby statement inspired by APL
- 7thaccount 7y agoThis is a pretty nifty piece of work. Not really a oneliner if it requires a 1/4 page of code, but I'll let it slide :) I do like the concept of keeping I/O in a more traditional language and the matrix/array code in something closer to APL.
- samatman 7y agoThis is why it says one statement, not one line. I've written single-statement argument parsers that take up most of a page of code. I like this because I've barely written more Ruby than APL (none of the latter) but it's perfectly legible to me. I respect the APL family and think the notation-as-a-tool-of-thought camp has a lot going for it. I'm just illiterate in that language.
- deleted 7y ago[deleted]
- userbinator 7y agoNot really a oneliner if it requires a 1/4 page of code, but I'll let it slide I was more surprised by the fact that the GitHub repo containing it actually has quite a few files; I was not expecting it to contain more than 2 or perhaps 3 files.
- smabie 7y agokdb+/q let's you call the server using really any language you would like: https://code.kx.com/q/interfaces/ https://code.kx.com/q/interfaces/ Though, I don't think there's really a problem with just doing I/O in q directly. The theoretical performance won't be as good, but since I/O is, well, I/O bound, it shouldn't really matter. And besides, I bet that even when doing non-array operations, q is faster than Python or Ruby.
- deleted 7y ago[deleted]
- burlesona 7y agoThis is very cool, and as an interesting side effect, it helped me understand exactly what's going on in the "famous" APL code. Well done!
- dan-robertson 7y agoWell mostly but the explanation forgets to mention the ravel (comma/array flatten) operator in the comment section and the article doesn’t really explain the inner product bit (matrix multiplication in the rig of or and and), which I think is more confusing. In particular the 1 ω part.
- zverok 7y ago(author here) I noticed the thing about flatten an hour ago and fixed it (somehow thought +/, is "reduce 2 times", then understood it is flatten + reduce, and my lib already had flatten, so it was easy to fix). As for "inner product", I just haven't found suitable Ruby metaphor and fell back to zip+reduce, which seems to be equivalent.
- faizshah 7y agoI wish Ruby was “the data science language” tbh. Ruby has some of the most beautiful and concise idioms of any language. I remember the first time I learned procs, blocks and lambdas my mind was blown with how elegant ruby solutions to problems can be. It’s a shame I have to use python instead.
- jbverschoor 7y agoI think 2 reasons: - it looks just easier in python to use c bindings - python is used in universities, just like java is/was I think crystal is the better of both, and would be great for this kind of stuff
- burlesona 7y agoI think it’s about 95% the academic thing. Not sure exactly how Python came into favor as the preferred scripting language of Academia, but once it did that basically ensured the math and science ecosystem would be very robust, which in turn made Python an obvious good choice for data science, ML, etc.
- jbverschoor 7y agoWell, python was created by a Dutch guy who worked at the research center for math, later some other research positions. the CWI has ties with the university, so that makes sense. Python is older than ruby, and matz probably had less ties, and probably uses more JP than guido did Dutch.
- tinco 7y agoYeah even if Ruby is not much younger than Python, it only gained popularity outside of Japan somewhere around 2003, and only extreme popularity after the release of rails.
- jbverschoor 7y agoI always wondered how dhh came into contact with this unknown language
- kragen 7y agoWhile I think this is helpful as a way to understand the APL original, the title is sort of false advertising. It's sort of not one Ruby statement if it starts with require 'apl' and uses some APL::Ary class that, as far as I can tell, isn't in the Ruby standard library. I was hoping to see how Enumerable enabled this, not how you can implement APL in some arbitrarily large amount of Ruby code offscreen, because of course you can do that.
- semanticist 7y agoThe APL module is in the `lib` directory of the repo holding the code, you can see the implementation there. It wouldn't be that bad to pull all the library code out into the 'main' command, but it would lose the 'single statement' impact and probably be harder for people to follow along with.
- kragen 7y agoAha, I thought maybe it was an existing gem. Anyway, I think it probably should lose the "single statement" impact, because in that context it's just misdirection, magician's patter. Yes, a single Ruby statement can invoke an arbitrarily complex piece of library code written for the article, but that doesn't tell you anything interesting about either Ruby or how to use it — the same is true of any programming language that supports programs in multiple modules, even assembly language: start: CALLQ play_life Now, the particular statement in question is considerably more illuminating than that, but presumably if you wanted to do almost anything else in an APLish style in Ruby, you'd probably have to implement more operations in apl.rb. I see that it doesn't include, for example, scan, slicing, or any broadcasting operations other than +, &, and |. (*, -, and / are missing, for example.) I mean, I've certainly written worse implementations of APLish operations. You could certainly extend apl.rb into something practically useful. I just don't think it's fair to put four pages of code in one file and then direct people's attention to the single statement in another file that invokes it.
- tinco 7y agoThat "ofcourse" you can implement Game of Life in one statement if you implement (parts of) the standard library of APL is not obvious to many people. The point of the blog is to introduce people to the concepts of APL, not to pitch some innovation. As a Ruby programmer who already knew about APL, I thought the title exactly matched the article. I was pleasantly surprised at how clean he designed the apl library, and the nice explanations.
- burke 7y agoHere’s a version of Game if Life I wrote in ruby ages ago that was inspired by APL in a, uh, different way: https://gist.github.com/burke/1428424 https://gist.github.com/burke/1428424
- ngcc_hk 7y agoQuite a good intro to ruby. Wonder can one do one line lisp as well.
- abrudz 7y agoHow does the performance stack up against APL's 1000 generations of a 1000-by-1000 world in under a second? https://aplwiki.com/wiki/John_Scholes%27_Conway%27s_Game_of_Life#Performance https://aplwiki.com/wiki/John_Scholes%27_Conway%27s_Game_of_...
- submeta 7y agoWow, this is beautiful! Thanks for sharing. Btw I love that Ruby methods return a value, so method chaining works so good. That‘s something I appreciate in Mathematica‘s postfix notation (for instance `x // Sin`), and also what I love in R‘s pipe operator, Unix‘s pipe. So many other languages like Elixir offer it. Unfortunately this does not work out-of-the box in my beloved Python.
- dan-robertson 7y agoDo you mean that ruby methods are values and so they can be passed to functions?
- submeta 7y agoThey return a value, like functions.
- SydneyPumpkin 7y agoWhy is Ruby so popular here at HackerNews, I just don't get it!
- burlesona 7y agoA very large number of SAAS products are built in (or started out in) Ruby, including these that I know of: Github, Gitlab, Twitter, Shopify, Stripe, AirBNB. There are more but those are the ones that come to the top of my mind. The engineers working at those places represent a non-trivial chunk of the software engineering world, especially in San Francisco, and SWE’s (especially in San Francisco) are a major cohort of HN readers. Additionally, the Ruby ecosystem had a major influence on many other ecosystems that spun out for various reasons. For example, Sinatra popularized the “minimal REST framework” and has been a major design influence on dozens of others, like ExpressJS and Flask. CoffeeScript, which established “transpile into JavaScript” as a thing, and profoundly influenced the shape of ES6, was heavily adopted and made prominent by the Rails team. Similarly notable projects like Ember, Elixir and Phoenix, were created by former Ruby and Rails core team members, Crystal is inspired by it, and plenty of older projects like SailsJS or Grails for groovy, etc. IIRC Rust’s cargo package management system and JS Yarn were also inspired by and contributed to by Ruby core folks. It’s influence is a lot bigger than its footprint. So Ruby is a nice, elegant language with a lot of influence in the software world, it’s used by many readers here, and a lot of people here have “warm fuzzy feelings” about it enjoy sharing and discussing interesting news or tidbits about it. That’s why :)
- dang 7y agoOff-topic, sorry, but could you please stop creating accounts for every few comments you post? We ban accounts that do that. This is in the site guidelines: https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html. You needn't use your real name of course, but for HN to be a community, users need some identity for others to relate to. Otherwise we may as well have no usernames and no community, and that would be a different kind of forum. https://hn.algolia.com/?sort=byDate&dateRange=all&type=comment&storyText=false&prefix&page=0&query=by:dang%20community%20identity https://hn.algolia.com/?sort=byDate&dateRange=all&type=comme...