5 ms·
I enjoy PHP like I enjoy going home to see my family for the holidays. It's fun for a bit, the nostalgia makes me feel warm and fuzzy, and the familiarity of ev
by whalesalad 13y ago
I enjoy PHP like I enjoy going home to see my family for the holidays. It's fun for a bit, the nostalgia makes me feel warm and fuzzy, and the familiarity of everything is very comforting. You quickly realize though that the grass really is a lot greener on the other side. You remember why you moved out of your parent's place and went out on your own.
It's hilarious looking at how cryptic these one-liners are, particularly when I consider that with languages like Ruby and Python you can do a LOT with a tiny little line if code.
As a side-note, one of my favorites is summing an array in Ruby:
x = [1, 2, 3, 4, 5]
x.inject &:+
=> 15
- dugmartin 13y agoOr one true line in APL: +/ 1 2 3 4 5
- lkey 13y agoSince we are golfing... +/⍳5
- sevenproxies 13y agoIn PHP array_sum([1, 2, 3, 4, 5]);
- DorianMarie 13y agoarray_sum_of_1_2_3_4_5(); (Added in PHP 6)
- x3ro 13y agoMade me laugh :)
- krapp 13y agothe funny thing is, with proper abuse of __call you can almost get that to work: https://gist.github.com/kennethrapp/328b6bd1bda8a8f94092 https://gist.github.com/kennethrapp/328b6bd1bda8a8f94092
- aaronem 13y agoThat's not funny. Horrifying, maybe, but not funny. Edit: Strike 'maybe'. I looked at it again and it's definitely horrifying. I saved it anyway, though; Halloween's only a few weeks off, and I think I shall print it out and stick it on the wall of my cube next to the PHP hammer and the "periodic" table of Perl 6 operators.
- agumonkey 13y agokudos. almost as cool as the cpp template hack to compute area based on part of the source code 'ascii art' size. (which I can't find anymore. googlefu--)
- tel 13y agoIn case this was a serious entry... which I honestly can't tell. The problem with array_sum is not that it's too long but instead that it's combined two rather specific bits of functionality into a fixed (if common) form, while the Ruby code separates the ideas of "folding" and "addition" allowing for many orthogonal creations. To pick another favorite, in Haskell you could write Foldable.foldl1 (+) which sums any foldable thing—anything which has elements which can be combined together one-by-one. So it'll apply to Trees or Sets or Dictionaries (well, Maps) just as easily while also allowing things like (+) to be replaced by other binary operators.
- Scriptor 13y agoPHP does have array_reduce, however operators like '+' can't be used as callables so you'd have to make your own wrapper functions. Edit: Screw it, haven't self promoted on a while. In Pharen (https://github.com/scriptor/pharen https://github.com/scriptor/pharen), which compiles to PHP, you could do: (reduce (+) 0 [1 2 3 4 5])
- tel 13y agoThat's really cool, but the syntax is strange to me. How do you distinguish between operator sectioning (+) and a side-effecting function call (f)?
- Scriptor 13y agoIt works (superficially) similarly to how Haskell does things and treats (+) as a partial function call by recognizing that + isn't getting at least 2 arguments. You could also do stuff like: ((+ 1) 2) ; => 3 (map (* 2) [1 2 3]) ; => [2 4 6] If a function f takes any arguments and Pharen knows this, it'll convert (f) into a partial function call as well. Unfortunately I haven't figured out a way to check if a function has any side effects. Otherwise in this case at least operators are treated like functions.
- tel 13y ago
- TylerE 13y agoirb(main):001:0> x = [1,2,3,4,5] => [1, 2, 3, 4, 5] irb(main):002:0> x.inject &:* => 120
- cheald 13y agoThe GP's point is that Ruby doesn't require a built-in method that specifically sums an array to still get a clean, terse operation. You can use #inject to apply an arbitrary operation to (operator, last result, current element) and arrive at a result. For example, given a list of numbers, you can generate a bitwise OR mask easily: [1,2,3,4,5].inject(:|) What this does is iterate over the list and apply ($last_result | $current_element) and return the result, which is passed on as $last_result to the next iteration. $last_result is 0 by default. This is equivalent to (as of PHP 5.4): array_reduce([1,2,3,4,5], function($v, $e) { return $v | $e; }, 0); Or prior to PHP 5.3: function or_mask($v, $e) { return $v | $e; } array_reduce(array(1,2,3,4,5), "or_mask", 0); It's doable in both languages, but Ruby's functional language lineage and its object-oriented nature results in an exceptionally clean approach.
- shaunxcode 13y agoyou'll love APL: x ← 1 2 3 4 5 +/ x http://ngn.github.io/apl/web/index.html#code=x%20%u2190%201%202%203%204%205%0A+/%20x http://ngn.github.io/apl/web/index.html#code=x%20%u2190%201%...
- dubcanada 13y agoThese "one-liners" are cryptic because they are complex, it's a what can you do in the smallest form possible. It doesn't require code readability. Obviously you wouldn't use a 120 character dependency injector. That's just silly. But you can make one in 120 characters.
- tel 13y agoI disagree. I think they're simpler because they're built of smaller numbers of orthogonal concepts. I think code like this is certainly more dense, but I find the consequence of that density is that I spend less time pattern matching and skipping over "things that look like common idioms" and more time thinking about how there large, powerful abstractions wire together to achieve the intended end.
- ramigb 13y ago[1,2,3,4,5].reduce(:+) Or in case of a range ... (1...6).reduce(:+) I'm sorry i couldn't fight the urge to FIFY :( ...
- aaronem 13y agoIt's a good fix and no bad thing to point out, and I'm not just saying that because I was about to point out the same thing before I noticed you'd beat me to it.
- epidemian 13y agoYeah, but remember to pass the 0 to it; otherwise the sum of an empty array would be nil: x.inject(0, &:+) That's one of the disadvantages of not having a powerful type system to infer what's the "zero" of some type. Still, the Ruby code is pretty readable and a good example of orthogonal concepts that compose well. E.g., getting the product of the numbers instead: x.inject(1, &:*). Or the least common multiple: x.inject(&:lcm).
- draegtun 13y agoHere it is in Perl6: [+] ^6 Actually cheated slightly because ^6 creates list from 0..5 :) So correct match would be... [+] 1..5 Here's how you would do it in Io: list(1, 2, 3, 4, 5) reduce(+) In Rebol there isn't a functional fold/reduce built in. So one way to do the one-liner would be: x: [1 2 3 4 5] forskip x 2 [insert x '+] x: do next x Above changes x to 15. Alternatively approach which doesn't clobber x: x: [1 2 3 4 5] y: [] forall x [repend y [x/1 '+]] take/last y do y I'm sure someone with more Rebol chops can see other (better!) ways of doing this :)