Live data from Hacker News

PHP in a Tweet

emanueleminotto.it

21–30 of 58 posts

Re: PHP in a Tweet

#21
post #17

Earlier quoted context omitted.

In PHP array_sum([1, 2, 3, 4, 5]);

In 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 fo…

PHP 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), which compiles to PHP, you could do:

    (reduce (+) 0 [1 2 3 4 5])

Re: PHP in a Tweet

#22
post #17

Earlier quoted context omitted.

In PHP array_sum([1, 2, 3, 4, 5]);

In 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 fo…

Well how about this then.

    foreach(array(1,2,3,4,5) as $x) $i=$i+$x; echo $i;

Re: PHP in a Tweet

#24
post #17

Earlier quoted context omitted.

In PHP array_sum([1, 2, 3, 4, 5]);

In 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 fo…

1. Separate folding and addition.

2. ?

3. Profit.

Re: PHP in a Tweet

#27

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, partic…

These "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.

Re: PHP in a Tweet

#29

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, partic…

In PHP array_sum([1, 2, 3, 4, 5]);

The 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.

Re: PHP in a Tweet

#30

Earlier quoted context omitted.

In PHP array_sum([1, 2, 3, 4, 5]);

array_sum_of_1_2_3_4_5(); (Added in PHP 6)

the funny thing is, with proper abuse of __call you can almost get that to work: https://gist.github.com/kennethrapp/328b6bd1bda8a8f94092
Post reply on HN