<div dir="ltr"><div><div><div>Sorry the fun part: the more you write code the less you have to write tests.<br><br></div># what's a strict email address:<br></div>def test_it_is_an_email_address_with_a_strict_user_name(self):<br></div> assert is_email_address.replace(has_user, has_strict_user) == is_strict_email_address<br></div><div class="gmail_extra"><br><div class="gmail_quote">2015-05-11 23:59 GMT+02:00 Gregory Salvan <span dir="ltr"><<a href="mailto:apieum@gmail.com" target="_blank">apieum@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div>In case you've not seen how it divides the volume of code you'll need to write, here are tests of "is_email_address":<br><br></div><div># What's an email address ?<br></div>def test_it_is_a_string(self):<br></div> assert is_string in is_email_address<br><br></div>def test_it_has_a_user_name(self):<br></div> assert has_user in is_email_address<br><br>def test_it_contains_at(self):<br> assert has_char('@') in is_email_address<br><br>def test_it_has_a_domain_name(self):<br> assert has_domain in is_email_address<br><br></div># answer: an email address is a string with a user name, char '@' and a domain name.<br><br></div>@Teddy Reedy with a class you'll have to write more tests and abuse of inheritance.<br><div><div><div><div><div><br></div></div></div></div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">2015-05-11 21:44 GMT+02:00 Gregory Salvan <span dir="ltr"><<a href="mailto:apieum@gmail.com" target="_blank">apieum@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Andrew Barnet we disagree. <br>In your example you have no information about if error comes from domain, user name or domain extension...<br>Writing a big regexp with group... really ? it is easy to maintain, test and reuse ? and for a novice ? muliply this by thousands of validators and their respectives tests.<br></div><div>I call that a mess and inside a project I lead, I will not accept it.<br></div><div><br></div>Even in Haskell people rarelly use arrows, I don't criticize this choice as arrows comes from category theory and we are used to think inside ZF set theory.<br></div>Somes prefer a syntax over another, there is not a good answer, but this also mean there is no irrelevant answer. <br>In fact both exists and choosing within the case is never easy. Thinking the same way for each problem is also wrong, so I will never pretend to resolve every problem with a single lib.<br><div><div><br></div><div>Now I understand this idea is not a priority, I've seen more and more threads about functional tools, I regret we can't find a solution but effectively this absence of solution now can't convince me to stop digging other paths. This is not irrespectuous.<br></div><div><br><br></div></div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">2015-05-11 20:25 GMT+02:00 Andrew Barnert <span dir="ltr"><<a href="mailto:abarnert@yahoo.com" target="_blank">abarnert@yahoo.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Monday, May 11, 2015 9:15 AM, Gregory Salvan <<a href="mailto:apieum@gmail.com" target="_blank">apieum@gmail.com</a>> wrote:<br>
<br>
<br>
>I don't want to insist and I respect your point of view, I just want to give a simplified real life example to show that function composition can be less painful than another syntax.<br>
<br>
</span>OK, let's compare your example to a Pythonic implementation of the same thing.<br>
<br>
import re<br>
<br>
ruser = re.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*$")<br>
rdomain = re.compile("^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$")<br>
rstrict_user = re.compile("^[a-z0-9][a-z0-9_-]+(?:\.[a-z0-9_-]+)*$")<br>
<br>
<br>
def is_email_address(addr):<br>
user, domain = addr.split('@', 1)<br>
return ruser.match(user) and rdomain.match(domain)<br>
<br>
def is_strict_email_address(addr):<br>
user, domain = addr.split('@', 1)<br>
return rstrictuser.match(user) and rdomain.match(domain)<br>
<br>
<br>
def is_org_address(addr):<br>
return is_email_address(addr) and addr.ends_with('.org')<br>
<br>
(An even better solution, given that you're already using regexps, might be to just use a single regexp with named groups for the user or strict-user, full domain, and TLD… but I've left yours alone.)<br>
<br>
Far from being more painful, the Pythonic version is easier to write, easier to read, easier to debug, shorter, and understandable to even a novice, without having to rewrite anything in your head. It also handles invalid input by returning failure values and/or raising appropriate exceptions rather than asserting and exiting. And it's almost certainly going to be significantly more efficient. And it works with any string-like type (that is, any type that has a .split method and works with re.match). And if you have to debug something, you will have, e.g., values named user and domain, rather than both being named value at different levels on the call stack.<br>
<br>
If you really want to come up with a convincing example for your idea, I'd take an example out of Learn You a Haskell or another book or tutorial and translate that to Python with your library. I suspect it would still have some of the same problems, but this example wouldn't even really be good in Haskell, so it's just making it harder to see why anyone would want anything like it. And by offering this as the response to Guido's "You're never going to convince me," well, if he _was_ still reading this thread with an open mind, he probably isn't anymore (although, to be honest, he probably wasn't reading it anyway).<br>
<div><div><br>
>import re<br>
><br>
>from lawvere import curry # curry is an arrow without type checking, inherits composition, mutiple dispatch<br>
><br>
>user_match = re.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*$").match<br>
>domain_match = re.compile("^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$").match<br>
>strict_user_match = re.compile("^[a-z0-9][a-z0-9_-]+(?:\.[a-z0-9_-]+)*$").match<br>
><br>
>@curry>def is_string(value):<br>
> assert isinstance(value, str), '%s is not a string' %value<br>
> return value<br>
><br>
>@curry<br>
>def apply_until_char(func, char, value):<br>
> func(value[:value.index(char)])<br>
> return value<br>
><br>
>@curry<br>
>def apply_from_char(func, char, value):<br>
> func(value[value.index(char) + 1:])<br>
> return value<br>
><br>
>@curry<br>
><br>
>def has_char(char, value):<br>
> assert value.count(char) == 1<br>
> return value<br>
><br>
>@curry<br>
>def assert_ends_with(text, value):<br>
> assert value.endswith(text), '%s do not ends with %s' % (value, text)<br>
> return value<br>
><br>
>@curry<br>
>def assert_user(user):<br>
> assert user_match(user) is not None, '%s is not a valid user name' % value<br>
> return user<br>
><br>
>@curry<br>
>def assert_strict_user(user):<br>
> assert strict_user_match(user) is not None, '%s is not a valid strict user' % value<br>
> return user<br>
><br>
>@curry<br>
>def assert_domain(domain):<br>
> assert domain_match(domain) is not None, '%s is not a valid domain name' % value<br>
> return domain<br>
><br>
># currying (be made with partial)<br>
><br>
>has_user = apply_until_char(assert_user, '@')<br>
><br>
>has_strict_user = apply_until_char(assert_strict_user, '@')<br>
><br>
>has_domain = apply_from_char(assert_domain, '@')<br>
><br>
><br>
># composition:<br>
><br>
>is_email_address = is_string >> has_char('@') >> has_user >> has_domain<br>
><br>
>is_strict_email_address = is_string >> has_char('@') >> has_strict_user >> has_domain<br>
><br>
><br>
># we just want org adresses ?<br>
><br>
>is_org_addess = is_email_address >> assert_ends_with('.org')<br>
><br>
><br>
><br>
><br>
>I found a lot of interest in this syntax, mainly for testing purpose, readability and maintenability of code.<br>
><br>
>No matters if I'm a fish out of python waters. :)<br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
><br>
>2015-05-11 16:41 GMT+02:00 Guido van Rossum <<a href="mailto:guido@python.org" target="_blank">guido@python.org</a>>:<br>
><br>
>As long as I'm "in charge" the chances of this (or anything like it) being accepted into Python are zero. I get a headache when I try to understand code that uses function composition, and I end up having to laboriously rewrite it using more traditional call notation before I move on to understanding what it actually does. Python is not Haskell, and perhaps more importantly, Python users are not like Haskel users. Either way, what may work out beautifully in Haskell will be like a fish out of water in Python.<br>
>><br>
>>I understand that it's fun to try to sole this puzzle, but evolving Python is more than solving puzzles. Enjoy debating the puzzle, but in the end Python will survive without the solution.<br>
>><br>
>><br>
>><br>
>>--<br>
>><br>
>>--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)<br>
>>_______________________________________________<br>
>>Python-ideas mailing list<br>
>><a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
>><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
>>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
>><br>
><br>
><br>
>_______________________________________________<br>
>Python-ideas mailing list<br>
><a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
><a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
>Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/codeofconduct/</a><br>
><br>
><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>