Larry Garfield introduces
some recent changes to the PHP language. "PHP 5.5 added a
feature called generators. Generators are in some sense a greatly
abbreviated syntax for iterators, but being so simple makes them
considerably more powerful. Essentially, a generator is just a function
that returns-and-pauses rather than returns-and-ends, and what is returned
is not a value but an iterator."
(Log in to post comments)
Language Feature Archaeology 101...
Posted Aug 27, 2013 1:19 UTC (Tue) by grantma (subscriber, #5225)
[Link]
Errmm, 'yield' is used ala Python generators, for 7 years?
By the way, Python copies from elsewhere as well, wondering where Python got 'yield' from.
Language Feature Archaeology 101...
Posted Aug 27, 2013 2:06 UTC (Tue) by artem (subscriber, #51262)
[Link]
I'm not sure how 'yield' got into Python, but AFAIK it appeared first in a language called CLU, almost 40 years ago
Language Feature Archaeology 101...
Posted Sep 6, 2013 0:42 UTC (Fri) by unsignedint (guest, #92715)
[Link]
Mind = blown, I just found where the TRON Legacy storyline was inspired from.
The programs in the system yielding to CLU...
Language Feature Archaeology 101...
Posted Aug 27, 2013 4:44 UTC (Tue) by eltoder (guest, #92562)
[Link]
According to PEP 255 Python got it from Sather and CLU.
Language Feature Archaeology 101...
Posted Aug 27, 2013 9:47 UTC (Tue) by rweir (subscriber, #24833)
[Link]
also Icon.
Please tell me this is a joke
Posted Aug 27, 2013 1:59 UTC (Tue) by HelloWorld (subscriber, #56129)
[Link]
Seriously? Let's take a look at a few other languages:
Scala:
val factor = 3
val triple = array map (_ * factor)
Perl 5:
my $factor = 3;
my $triple = map { $_ * $factor } @array
Python:
factor = 3
triple = [ elm * factor for elm in array ]
# or map(lambda x: x * factor, array)
They couldn't have fucked it up more if they tried, and everybody knows that the rest of PHP is just as broken. And yet this guy does as if PHP were the best thing since sliced bread, it's downright repulsive.
Please tell me this is a joke
Posted Aug 27, 2013 2:28 UTC (Tue) by kyanh (subscriber, #28894)
[Link]
no it is not :D
Please tell me this is a joke
Posted Aug 27, 2013 2:35 UTC (Tue) by nybble41 (subscriber, #55106)
[Link]
Haskell:
factor = 3
triple = fmap (* factor)
That's even shorter than the Perl 5 version. Bonus: It's completely type-safe--with the compiler-inferred type "(Functor f, Num a) => f a -> f a"--but works on any Functor instance, not just arrays. This includes List, Array, Map, Maybe, and most other containers, plus some things which aren't typical containers, like IO (or any other properly-defined Monad instance).
Yes, I am a card-carrying Haskell fan club member. :)
Please tell me this is a joke
Posted Aug 27, 2013 9:26 UTC (Tue) by HelloWorld (subscriber, #56129)
[Link]
> That's even shorter than the Perl 5 version.
It's shorter because it does something else...
Please tell me this is a joke
Posted Aug 27, 2013 10:11 UTC (Tue) by chirlu (subscriber, #89906)
[Link]
> > That's even shorter than the Perl 5 version.
> It's shorter because it does something else...
What does the Perl code do, then?
Please tell me this is a joke
Posted Aug 27, 2013 10:56 UTC (Tue) by andka (subscriber, #974)
[Link]
I think he just missed the 'array' part at the end. That is:
factor = 3
triple = fmap (* factor) array
Please tell me this is a joke
Posted Aug 27, 2013 15:34 UTC (Tue) by nybble41 (subscriber, #55106)
[Link]
> It's shorter because it does something else...
You're partly right; it does do something a bit different. I was taking advantage of Haskell's currying to provide a generic function for tripling the data in any Functor, but even after tacking "array" on the end as andka suggested it's still shorter than the Perl 5 code.
Please tell me this is a joke
Posted Aug 27, 2013 17:03 UTC (Tue) by donbarry (guest, #10485)
[Link]
in J (a dialect of APL):
factor =. 3
triple =. *&factor
(the J dictionary declares that a synonym for the bond conjunction & is "curry" in honor of Haskell Curry)
Please tell me this is a joke
Posted Aug 28, 2013 19:32 UTC (Wed) by iarna (guest, #92598)
[Link]
Or this Haskell version in Perl6:
$factor = 3
$triple = * <<*>> $factor;
(Later, use as $triple(@array) => tripled version of @array)
Or literally what was earlier:
$factor = 3
@triple = @array <<*>> $factor;
Please tell me this is a joke
Posted Aug 27, 2013 3:46 UTC (Tue) by eltoder (guest, #92562)
[Link]
Posted Aug 27, 2013 6:33 UTC (Tue) by dvdeug (subscriber, #10998)
[Link]
Re-invent Scheme? Wasn't Scheme the reinvention of Lisp 1.5?
In any case, I prefer my languages statically typed. It's rather interesting to see how all these old ideas are getting polished and merged into new languages.
Please tell me this is a joke
Posted Aug 27, 2013 7:30 UTC (Tue) by peter-b (subscriber, #66996)
[Link]
Well, to be fair, Scheme is a Lisp-1 whereas Lisp 1.5 was a Lisp-2 (IIRC).
Please tell me this is a joke
Posted Aug 27, 2013 7:38 UTC (Tue) by macson_g (subscriber, #12717)
[Link]
Posted Aug 27, 2013 8:41 UTC (Tue) by HelloWorld (subscriber, #56129)
[Link]
That isn't really comparable as std::transform does something else: it writes elements to an output iterator instead of returning a new collection. I actually thought about giving a C++ example but refrained from doing so due to that.
Please tell me this is a joke
Posted Aug 27, 2013 19:47 UTC (Tue) by oever (subscriber, #987)
[Link]
C++ has operator overloading. This particular example can be shortened by using the excellent, header library Eigen.
int factor = 3;
auto triple = factor * array;
Please tell me this is a joke
Posted Aug 27, 2013 23:18 UTC (Tue) by mathstuf (subscriber, #69389)
[Link]
Posted Aug 28, 2013 13:44 UTC (Wed) by jzbiciak (✭ supporter ✭, #5246)
[Link]
I am simultaneously intrigued and yet repulsed. While part of me thinks that's seriously cool—it's yet another approach to writing a concise domain specific language over C++, more or less—the rest of me thinks "even fewer people will understand my code, making hand-off to others even harder."
Please tell me this is a joke
Posted Aug 28, 2013 13:53 UTC (Wed) by mathstuf (subscriber, #69389)
[Link]
I'm certainly in that camp about this library.An interesting thing that isn't mentioned is that right-associativity is possible if you do something like "<arrow-" for Knuth arrows (or Conway arrows, but then you lose the resemblance too much, IMO). If you're *even more* masochisitic, you could even do "<(arrow^4)-", but the parentheses get in the way of the appearance here. Of course, these are only really going to work with a bignum library, but if you're this deep, why not?
Please tell me this is a joke
Posted Aug 27, 2013 8:06 UTC (Tue) by canatella (subscriber, #6745)
[Link]
And here goes the Ruby version. At least the map method is not falling from the sky but attached to an enumerable object as it should ;)
factor = 3
triple = array.map { |i| i * factor }
Please tell me this is a joke
Posted Aug 27, 2013 17:05 UTC (Tue) by mathstuf (subscriber, #69389)
[Link]
I never liked the 'map as method' from Ruby or JavaScripts array.forEach() as much since if I then make some other container, I have to implement my own map, fold, etc. Sometimes it makes sense (like C++'s std::set::find since that can be hard-coded to do binary search), but in general, I think that the algorithm should be distinct from the data structure implementation. How would Ruby idiomatically implement something like:
> zip :: [a] -> [b] -> [(a, b)]
with external iterators, it's simple, but it doesn't seem as obvious for internal iteration (it looks like you need a .map call on each array, but that just means that something like zip3 and zip4 is pretty messy).
Please tell me this is a joke
Posted Aug 27, 2013 23:06 UTC (Tue) by bloopletech (subscriber, #71203)
[Link]
To get map, you only have to define #each and then include Enumerable; that gives you a bunch of those collection methods.
Please tell me this is a joke
Posted Aug 27, 2013 9:59 UTC (Tue) by ibukanov (subscriber, #3942)
[Link]
JavaScript (essentially using 18 years old syntax with 4-years old library addition):
var factor = 3
var tripple = array.map(function(elem) { return elem * factor })
So indeed PHP wins verbosity contest. What exactly prevents having shorter forms of lambdas there?
Please tell me this is a joke
Posted Aug 28, 2013 8:52 UTC (Wed) by man_ls (guest, #15091)
[Link]
var factor = 3;
var triple = [factor * i for each (i in array)];
Array comprehensions available in Firefox; still not supported in Chrome or node.js though. Should be when ECMAScript 6 is released, hopefully later on this year.
Please tell me this is a joke
Posted Aug 27, 2013 10:27 UTC (Tue) by cesarb (subscriber, #6266)
[Link]
That is amusing. You post a small example to prove a point, and people can't help rewriting that example on every language under the sun.
Posted Aug 27, 2013 13:15 UTC (Tue) by k3ninho (subscriber, #50375)
[Link]
I have an implementation in Whitespace, but the margin of this comment thread is too small to record it.
K3n.
Please tell me this is a joke
Posted Aug 27, 2013 11:35 UTC (Tue) by NAR (subscriber, #1313)
[Link]
Erlang (just for the fun of it):
Factor = 3,
Triple = [Factor * X || X <- Array].
If they could lose that use part and also the return from that PHP example, it wouldn't be that bad...
Please tell me this is a joke
Posted Aug 27, 2013 21:49 UTC (Tue) by jrigg (subscriber, #30848)
[Link]
> everybody knows that the rest of PHP is just as broken
Out of curiosity, which of those other languages can be used (relatively) safely on a shared web server when run as an Apache module?
shared hosting
Posted Aug 27, 2013 23:10 UTC (Tue) by HelloWorld (subscriber, #56129)
[Link]
What's the point of doing that? If you want to do shared hosting, run the relevant interpreter/VM under a separate user account and connect it to the web server via suitable protocols such as FastCGI/SCGI/AJP.
Please tell me this is a joke
Posted Aug 27, 2013 23:12 UTC (Tue) by jrigg (subscriber, #30848)
[Link]
On apache2-mpm-itk, I should add. mod_php at least works with this.
PHP may be broken in many ways, but avoiding it isn't always practical.
Please tell me this is a joke
Posted Aug 27, 2013 23:41 UTC (Tue) by mathstuf (subscriber, #69389)
[Link]
Yeah…no. I serve all of my non-static files over FastCGI running in a separate FreeBSD jail which nginx then forwards traffic to. It also allows me to store SSL keys and certs only in the nginx root and then the web apps don't have any access to it at all in case something really goes wrong. I don't understand why loading arbitrary code into the web server is still used so often.
Please tell me this is a joke
Posted Aug 28, 2013 0:32 UTC (Wed) by b7j0c (subscriber, #27559)
[Link]
why do i need to run my code on a shared web server as an apache module?
Why shared servers
Posted Aug 28, 2013 9:52 UTC (Wed) by tialaramex (subscriber, #21167)
[Link]
PHP's success is because things begin on crappy shared servers. It's like BASIC in 1983. If you write your program in Lisp that's really cool, but it doesn't work on Joe Average's ZX81. If you write the program in BASIC it works on the ZX81. The BASIC on the ZX81 is horrible, you can list dozens of reasons not to write the program in Sinclair BASIC in principle. BUT it works on a ZX81 right out of the box, whereas your hypothetical Lisp program does not.
Every BASIC program I ever wrote was crappy. I disown them all. But if I had made a principled decision to use a language not available on microcomputers then I wouldn't have done any programming at all for several more years, probably I would have lost interest and done something else with my life.
PHP is *not* like BASIC
Posted Aug 28, 2013 10:10 UTC (Wed) by HelloWorld (subscriber, #56129)
[Link]
Shared web hosts are *not* a reason to use PHP. It's possible today to get a shared web host with Python or Ruby support for just as little. It's fundamentally different from the 80s where you needed a Lisp machine for thousands of dollars and the size of a wardrobe to do anything sensible.
I think I preferred my subject line
Posted Aug 28, 2013 15:54 UTC (Wed) by tialaramex (subscriber, #21167)
[Link]
But I didn't decide to write BASIC programs and then buy a ZX81, the ZX81 was already there‡. Most people's current shared hosting provider offers PHP, maybe Perl, and that's it. In fact a lot of hosts are surprised if you intend to actually do any development up front, they anticipate most users will drop in a ready-to-go PHP application like phpBB or Wordpress.
To get to a world where people stop using PHP, you need most providers to offer Ruby (or Python, or whatever) and have it run with decent performance. Good luck with both those things, I sincerely mean it, but don't expect everyone to put their crappy home-grown website on hold until you've got it figured out.
‡ Actually I'm not sure why I picked Sinclair's little black ZX81 for my illustration, I actually had a Commodore VIC 20 when I was learning.
I think I preferred my subject line
Posted Aug 28, 2013 16:59 UTC (Wed) by b7j0c (subscriber, #27559)
[Link]
true, but many of the very-low-end content requirements on the web can now be met with no coding whatsoever...blogging platforms, google pages, facebook etc. for someone with very modest needs, i wouldn't point them at any hosting provider at all, just go use twitter/google pages/wordpress etc etc etc
I think I preferred my subject line
Posted Aug 28, 2013 20:21 UTC (Wed) by HelloWorld (subscriber, #56129)
[Link]
> But I didn't decide to write BASIC programs and then buy a ZX81, the ZX81 was already there‡. Most people's current shared hosting provider offers PHP, maybe Perl, and that's it.
If your shared hosting provider doesn't provide the services you need (i. e. support for decent programming languages), it's time to change to another one, which, unlike buying a Lisp machine in 1983, is not an insurmountable challenge, financially or otherwise.
Please tell me this is a joke
Posted Aug 28, 2013 10:18 UTC (Wed) by sorpigal (subscriber, #36106)
[Link]
Perl at least can be run in a shared hosting environment without much difficulty and I assume it's possible for many other languages. As for safety: If your language is PHP you might be modestly better isolated from other users of PHP on the same box, but your application is likely riddled with serious vulnerabilities just by being written in PHP. It takes excessive care to have a secure PHP app.
Please tell me this is a joke
Posted Aug 28, 2013 18:12 UTC (Wed) by drag (subscriber, #31333)
[Link]
> Out of curiosity, which of those other languages can be used (relatively) safely on a shared web server when run as an Apache module?
I don't know, but I know that you can't do that safely with PHP. Nothing is safe with this language. *rimshot*
Garfield: A Look at PHP's Continuing Evolution
Posted Aug 28, 2013 0:31 UTC (Wed) by b7j0c (subscriber, #27559)
[Link]
$u = 4/0;
if ($u == $t) {
print "do stuff with *your* credit card number here\n";
}
(and where did $t come from?)
this is why php can never be used for anything serious
Garfield: A Look at PHP's Continuing Evolution
Posted Aug 28, 2013 3:16 UTC (Wed) by mathstuf (subscriber, #69389)
[Link]
That could make sense if you think of it as "$u is 4/0 which is undefined. $t hasn't been defined, so it is undefined. Equality!". The only debate I'd really like to have from this behavior is about how aggressive the "use something (*anything*) else" deadline should be.
Garfield: A Look at PHP's Continuing Evolution
Posted Aug 28, 2013 16:57 UTC (Wed) by b7j0c (subscriber, #27559)
[Link]
i can't believe someone with a username "mathstuf" thinks it is acceptable for php to define 4/0. and yes, "undefined" is a definition if it is acceptable in the runtime (which it is)
Garfield: A Look at PHP's Continuing Evolution
Posted Aug 28, 2013 17:42 UTC (Wed) by mathstuf (subscriber, #69389)
[Link]
Did you miss where I stated the only argument that should be happening after seeing this is how fast to drop PHP? I was only trying to think of some kind of logic that might have been used to justify the behavior described; I'd never actually defend it.
Garfield: A Look at PHP's Continuing Evolution
Posted Aug 28, 2013 18:23 UTC (Wed) by b7j0c (subscriber, #27559)
[Link]
sorry, should have read your response more carefully! :)
Garfield: A Look at PHP's Continuing Evolution
Posted Aug 29, 2013 11:18 UTC (Thu) by endecotp (guest, #36428)
[Link]
Which of the versions posted above do people actually find the most readable?
My exposure to most of these languages is occassional attempts to understand, debug or slightly modify some existing code. I doubt that I am unusual in that respect. I don't think I would have much chance of correctly parsing most of the snippets posted here.
I'm wondering what syntactic clues would be most helpful. The word "lambda", perhaps? And "map"?
I am primarily a C++ programmer at present, and the choice of symtax for C++11's lambda expressions is not something that I am very happy with; it lacks exactly this sort of obviuos syntactic clue. I would have much preferred a keyword like "lambda" rather then the [=] syntax.
Garfield: A Look at PHP's Continuing Evolution
Posted Aug 29, 2013 23:15 UTC (Thu) by mathstuf (subscriber, #69389)
[Link]
> Which of the versions posted above do people actually find the most readable?
A rough ordering (I code mostly in C++, but work with Python and shell regularly and am familiar with Haskell) is first pretty much sorted by class then languages sorted within it:
[List comprehensions]
- Python (it reads a lot like Haskell's list comprehensions, just more wordy)
- JavaScript 1.7
- Erlang (the "||" is probably a poor choice for drive-by reading IMO)
[Currying]
- Haskell
[Inline "Callbacks"]
- Ruby
- JavaScript 1.6
- Lisp (it'd probably be better in a monospace font where parentheses are more obvious)
- C++ (the [=] is indeed fugly, but the context gives a good idea)
- PHP (verbose and fugly, but decipherable)
[Perl]
- Perl 5 (magic variables ($_) and sigils (my, $, @) I'd have to look up without the nice variable names; would be up with Haskell if I worked with Perl more than changing literal strings typically)
- Perl 6 (*more* operator combinations‽)
[APL]
- J
For writing this kind of code, I'd prefer currying (less typing and patterns are easier to spot) > list comprehension (reads left to right fairly well) > callback with each section in roughly the same order.
> I would have much preferred a keyword like "lambda" rather then the [=] syntax.
Adding new keywords at this point would be a disaster. The boost::lambda namespace would be wrecked with such a keyword addition.