Live data from Hacker News

PHP in a Tweet

emanueleminotto.it

41–50 of 58 posts

Re: PHP in a Tweet

#41

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…

Or one true line in APL: +/ 1 2 3 4 5

Since we are golfing...

+/⍳5

Re: PHP in a Tweet

#42
post #25
post #5

Don't invite Perl to this party

Too late, and she brought JavaScript too...

Wow, Perl's seen better days but I think they're a good match for each other. Perl's fascinated by JavaScript's objectivity and popularity, and JavaScript's wearing Perl for retro effect like some kind of fashion item. The two of them are like darkness and light. No prize for guessing who brought the drugs...

Re: PHP in a Tweet

#43
post #17

Earlier quoted context omitted.

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;

This works, is fast enough and readable. Perfectly fine solution to me.

Re: PHP in a Tweet

#44
post #40

Earlier quoted context omitted.

It 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 functio…

So it's tracking the arity of functions and turning anything which has been applied to fewer than ARITY arguments to a partial application? I suppose that comes at the cost of (+ 1 2 3 4)?

Why would it?

Re: PHP in a Tweet

#45
post #38

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…

[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 :( ...

It'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.

Re: PHP in a Tweet

#46
I'm really conflicted on what opinion to have of this. On the one hand, these PHP constructs are even worse than most of what you see in that language, but on the other hand, constraining PHP sources to a maximum length of 140 characters should help reduce the scope of the contagion.

Re: PHP in a Tweet

#47
post #30

Earlier quoted context omitted.

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

That'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.

Re: PHP in a Tweet

#48

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…

Yeah, 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).

Re: PHP in a Tweet

#49
post #40

Earlier quoted context omitted.

It 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 functio…

So it's tracking the arity of functions and turning anything which has been applied to fewer than ARITY arguments to a partial application? I suppose that comes at the cost of (+ 1 2 3 4)?

Surprisingly, not really.

    {-# OPTIONS -fglasgow-exts #-}
    
    class BuildList a r  | r-> a where
      build' :: [a] -> a -> r
    
    instance BuildList a [a] where
      build' l x = reverse$ x:l
    
    instance BuildList a r => BuildList a (a->r) where
      build' l x y = build'(x:l) y
    
    varargs x = build' [] x
    
    main = print $ ( sum $ varargs 1 2 3 4 5 6 100)


    $ ./Test
    121

    
http://okmij.org/ftp/Haskell/vararg-fn.lhs

Re: PHP in a Tweet

#50
post #40

Earlier quoted context omitted.

It 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 functio…

So it's tracking the arity of functions and turning anything which has been applied to fewer than ARITY arguments to a partial application? I suppose that comes at the cost of (+ 1 2 3 4)?

It does track the arity of functions, but remember that this happens at compile time. Regular addition in Pharen is still just regular addition in PHP:

    (def a (+ 1 2 3 4)) ; => $a = (1 + 2 + 3 + 4);
Of course, there are situations where partial application won't always happen because the compiler is unable to detect it:

    (fn add (x y)
      (+ x y))

    (map #add [1 2 3])
Pharen won't be able to realize that `add` here is being partially applied inside `map`. It's not smart enough for that yet. If you try to run this you'll end up with 'Missing argument 2' and 'Undefined variable: y' all over the place.

However, I'm not putting a whole lot of emphasis on partials. They're there as a convenience, but they're not really a core part of the language.

But yes, back to the original point, all the tracking is done at compile time so that for addition with two or more arguments the resulting PHP will look like regular addition in PHP.

Post reply on HN