Live data from Hacker News

PHP in a Tweet

emanueleminotto.it

31–40 of 58 posts

Re: PHP in a Tweet

#31
post #29

Earlier quoted context omitted.

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…

Prior to PHP 5.3 this could work as well:

    array_reduce(array(1,2,3,4,5), create_function('$v, $e', 'return $v | $e;'), 0);

Re: PHP in a Tweet

#32
post #29

Earlier quoted context omitted.

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…

Prior to PHP 5.3 this could work as well: array_reduce(array(1,2,3,4,5), create_function('$v, $e', 'return $v | $e;'), 0);

I'm really glad that 5.3 got proper anonymous functions. Specifying function bodies as strings to be eval'd makes me twitch.

Re: PHP in a Tweet

#33
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…

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])

That's really cool, but the syntax is strange to me. How do you distinguish between operator sectioning (+) and a side-effecting function call (f)?

Re: PHP in a Tweet

#34
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…

1. Separate folding and addition. 2. ? 3. Profit.

Orthogonality and composability dramatically increase abstraction, code reuse, testability, and likelihood of writing correct code, both for library maintainers and library users.

Step 2 is actually extremely well known.

Re: PHP in a Tweet

#35
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;

The array_reduce answers get closer to what I was looking for. This might be a "one line" answer, but it requires two special forms, assignment, sequencing operators (;), and two fresh variables.

Re: PHP in a Tweet

#36

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.

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

Re: PHP in a Tweet

#37
post #33

Earlier quoted context omitted.

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])

That's really cool, but the syntax is strange to me. How do you distinguish between operator sectioning (+) and a side-effecting function call (f)?

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 function has any side effects. Otherwise in this case at least operators are treated like functions.

Re: PHP in a Tweet

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

Re: PHP in a Tweet

#39

Earlier quoted context omitted.

this one is amazing: play{a=LFPulse;b=(1..4);Mix(a.ar(a.ar(a.ar(a.ar(b/32)+1/8)+1 b)+(Mix(a.ar(b/64))+a.ar(4/b) (a.ar(a.ar(b/8)) 2+b)) 100))/8!2}//#SuperCollider audio: http://fredrikolofsson.com/f0blog/files/audio/tweet0020.mp3 "this tweet is also totally deterministic and without any randomness. here a lot of nested square wave oscillators creates the complexity. basically there are 4 channels/voices mixed down to…

Reminds me of those: http://www.youtube.com/watch?v=GtQdIYUtAHg http://www.youtube.com/watch?v=qlrs2Vorw2Y http://www.youtube.com/watch?v=tCRPUv8V22o :)

Yeah I remember those ! In fact I did some bit shift melody stuff myself in supercollider based on those and some other code.

One of the sc people released an iPhone app that let's you write bitshift based melodies.

The early stuff that supercolliders creator James McCartney did really set the style for short, mathematically elegant music. It's really a great language for it. You can do operations on arrays, and do interesting multichannel expansion.

Re: PHP in a Tweet

#40
post #33

Earlier quoted context omitted.

That's really cool, but the syntax is strange to me. How do you distinguish between operator sectioning (+) and a side-effecting function call (f)?

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)?

Post reply on HN