Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67717

Re: Functional programming

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: Functional programming
Date 2014-03-04 22:50 +0200
Organization A noiseless patient Spider
Message-ID <8738ixofhl.fsf@elektro.pacujo.net> (permalink)
References (13 earlier) <mailman.7695.1393913099.18130.python-list@python.org> <53159540$0$2923$c3e8da3$76491128@news.astraweb.com> <mailman.7700.1393930643.18130.python-list@python.org> <87bnxloibq.fsf@elektro.pacujo.net> <mailman.7737.1393963318.18130.python-list@python.org>

Show all headers | View raw


Chris Angelico <rosuav@gmail.com>:

> On Wed, Mar 5, 2014 at 6:49 AM, Marko Rauhamaa <marko@pacujo.net> wrote:
>> public ConnectionPool(int maxConnections, String url) throws SQLException {
>>     try {
>>         super(() -> {
>>             try {
>>                 return DriverManager.getConnection(url);
>>             } catch ( SQLException ex ) {
>>                 throw new WrappedSqlException(ex);
>>             }
>>         }, maxConnections);
>>     } catch (WrappedSqlException wse) {
>>         throw wse.getSqlException();
>>     }
>> }
>>
>> ===JAVA END=============================================================
>
> You're not doing the same thing, though. The Java rigmarole is to
> ensure that an SQLException thrown in getConnection will propagate up,
> despite (presumably) something inside the equivalent of
> super().__init__ that swallows SQLExceptions. Of course it looks
> tidier when you don't do the messy bit.

See <URL: http://stackoverflow.com/questions/14039995/
java-8-mandatory-checked-exceptions-handling-in-lambd
a-expressions-for-standard>.

The "rigmarole" is trying to get around Java's mandatory exception
handling limitations, which Python doesn't have.

You are not allowed to pass a lambda to the super constructor that
throws an SQLException. To get around the limitation, a RuntimeException
wrapper is created to smuggle the SQLException through the compiler's
defenses. Any code is free to throw a RuntimeException.

I don't know, though, if this is that good of an example. I don't know
if the lambda gets called within the constructor or, as I would guess,
whenever a new connection is needed. The whole wrapping exercise would
be for nothing, then.

Here's a more prosaic example (probably contains errors):

===JAVA BEGIN===========================================================

private Map<TaxpayerIdNumber, Map<FormId, Form>> filings =
    new TreeMap<TaxpayerIdNumber, Map<FormId, Form>>();

class FormFiling {
    public FormFiling(TaxpayerIdNumber taxpayerId, Form form) {
        this.taxpayerId = taxpayerId;
        this.form = form;
    }

    public TaxpayerIdNumber getTaxpayerId() { return taxpayerId; }
    public Form getForm() { return form; }

    private TaxpayerIdNumber taxpayerId;
    private Form form;
};

List<FormFiling> getForms(FormId formId)
{
    List<FormFiling> forms = new LinkedList<FormFiling>();
    for (Map.Entry<TaxpayerIdNumber, Map<FormId, Form>> payerEntry :
         filings.entrySet()) {
        TaxpayerIdNumber taxpayerId = payerEntry.getKey();
        Map<FormId, Form> filing = payerEntry.getValue();
        if (filing.containsKey(formId))
            forms.add(new FormFiling(taxpayerId, filing.get(formId)))
    }
    return forms;
}

===JAVA END=============================================================

===PYTHON BEGIN=========================================================

filings = {}

def getForms(formId):
    forms = []
    for taxpayerId, filing in filings.iteritems():
         if formId in filing:
             forms.append((taxpayerId, filing[formId]))
    return forms

===PYTHON END===========================================================

or:

===PYTHON BEGIN=========================================================

filings = {}

def getForms(formId):
    return ((taxpayerId, filing[formId])
            for (taxpayerId, filing) in filings.iteritems()
            if formId in filing)

===PYTHON END===========================================================


Marko

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Functional programming Ned Batchelder <ned@nedbatchelder.com> - 2014-03-02 20:27 -0500
  Re: Functional programming Rustom Mody <rustompmody@gmail.com> - 2014-03-03 03:45 -0800
    Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-03 23:20 +1100
      Re: Functional programming Rustom Mody <rustompmody@gmail.com> - 2014-03-03 05:48 -0800
        Re: Functional programming Rustom Mody <rustompmody@gmail.com> - 2014-03-03 05:51 -0800
        Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-04 01:00 +1100
          Re: Functional programming Rustom Mody <rustompmody@gmail.com> - 2014-03-03 06:08 -0800
            Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-04 01:23 +1100
              Re: Functional programming Rustom Mody <rustompmody@gmail.com> - 2014-03-03 06:38 -0800
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-04 02:01 +1100
                Re: Functional programming Rustom Mody <rustompmody@gmail.com> - 2014-03-03 07:28 -0800
                Re: Functional programming Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-03 17:27 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-04 05:37 +1100
                Re: Functional programming Steven D'Aprano <steve@pearwood.info> - 2014-03-04 05:35 +0000
                Re: Functional programming Rustom Mody <rustompmody@gmail.com> - 2014-03-03 21:59 -0800
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-04 17:04 +1100
                Re: Functional programming Rustom Mody <rustompmody@gmail.com> - 2014-03-03 22:20 -0800
                Re: Functional programming Steven D'Aprano <steve@pearwood.info> - 2014-03-04 08:56 +0000
                Re: Functional programming Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-04 11:56 +0100
                Re: Functional programming Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-04 11:47 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 00:01 +1100
                OT Sine Rule [was Re: Functional programming] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-04 14:25 +0000
                Re: OT Sine Rule [was Re: Functional programming] Tim Chase <python.list@tim.thechases.com> - 2014-03-04 08:37 -0600
                Re: OT Sine Rule [was Re: Functional programming] Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-04 14:42 +0000
                Re: OT Sine Rule [was Re: Functional programming] Chris Angelico <rosuav@gmail.com> - 2014-03-05 02:06 +1100
                Re: OT Sine Rule [was Re: Functional programming] Tim Chase <python.list@tim.thechases.com> - 2014-03-04 09:21 -0600
                Re: Functional programming Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-05 09:59 +0100
                Re: Functional programming Marko Rauhamaa <marko@pacujo.net> - 2014-03-04 21:49 +0200
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 07:01 +1100
                Re: Functional programming Marko Rauhamaa <marko@pacujo.net> - 2014-03-04 22:50 +0200
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 08:06 +1100
                Re: Functional programming Marko Rauhamaa <marko@pacujo.net> - 2014-03-04 23:21 +0200
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 08:26 +1100
                Re: Functional programming Marko Rauhamaa <marko@pacujo.net> - 2014-03-04 23:43 +0200
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 08:52 +1100
                Re: Functional programming Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-05 12:57 +1300
                Re: Functional programming Marko Rauhamaa <marko@pacujo.net> - 2014-03-05 02:11 +0200
                Re: Functional programming "BartC" <bc@freeuk.com> - 2014-03-04 13:30 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 00:47 +1100
                Re: Functional programming Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-04 14:05 +0000
                Re: Functional programming Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-04 14:55 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 02:13 +1100
                Re: Functional programming MRAB <python@mrabarnett.plus.com> - 2014-03-04 17:07 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 01:17 +1100
                Re: Functional programming Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-04 15:18 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 02:28 +1100
                Re: Functional programming Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-04 15:45 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 03:04 +1100
                Re: Functional programming Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-05 10:09 +0100
                Re: Functional programming "BartC" <bc@freeuk.com> - 2014-03-05 11:28 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-05 23:04 +1100
                Re: Functional programming Marko Rauhamaa <marko@pacujo.net> - 2014-03-05 14:11 +0200
                Re: Functional programming Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-04 11:06 +1300
                Re: Functional programming Ben Finney <ben+python@benfinney.id.au> - 2014-03-04 09:31 +1100
                Re: Functional programming Grant Edwards <invalid@invalid.invalid> - 2014-03-04 14:59 +0000
                Re: Functional programming Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-04 15:22 +0000
                Re: Functional programming Chris Angelico <rosuav@gmail.com> - 2014-03-04 09:42 +1100
                Re: Functional programming Ben Finney <ben+python@benfinney.id.au> - 2014-03-04 10:52 +1100
                Re: Functional programming "BartC" <bc@freeuk.com> - 2014-03-04 09:41 +0000
            Re: Functional programming 88888 Dihedral <dihedral88888@gmail.com> - 2014-03-03 16:35 -0800

csiph-web