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


Groups > comp.lang.python > #30844

Re: fmap(), "inverse" of Python map() function

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.105
X-Spam-Level *
X-Spam-Evidence '*H*': 0.79; '*S*': 0.00; 'subject:Python': 0.05; 'result,': 0.05; 'def': 0.10; 'argument)': 0.16; 'lambda': 0.16; 'oct': 0.16; 'wrote:': 0.17; 'cheers,': 0.23; 'header:In-Reply- To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'fri,': 0.30; 'received:209.85.215.46': 0.30; 'url:2012': 0.30; 'could': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'functional': 0.36; 'subject:" ': 0.36; 'url:in': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'more': 0.63; 'url:blogspot': 0.64; 'url:10': 0.65; 'applying': 0.69; 'special': 0.73; 'compose': 0.84; 'to:name:python': 0.84; 'do:': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=EgYFpwcBochqWyButT4mivjVaOkLxxFzbbtxHJsRSPs=; b=yUjh3DRA3N3V5d8Z9g6+dvkWqofYq9rKCHcvX42G+D0q9i3YlxUot8JTnOwS2NT9GP lckWsxwlZUxARu/vRWmlFF4g3IIflWcBTW7wNSiHNAHxl49/+cxW/HZd4GXZ4/6H3Lad OFwZbFZnjACx06SL6NGIxLnZYLqO/rWn3em7RH11iDdUfDf7tfg3ziq9u0Ml72dI0k7x Kt+10gorFTK8w87gDq+/w13YBotyuI4uJyWqafqXtr7P3VyjFiUJDjkFkom8JYXUeS1+ XrIhmwW8BBkaiVlcgYIArvDYhIv5t2Qs77QSFAA6zsu6VYFqSJ3lE1xt9mzX0Ahov6G0 tcRg==
MIME-Version 1.0
In-Reply-To <CALwzidmE5_LyKfEGqAGeN0Uv+g1fFo+ztjhZdr3b3vbRRnTvJA@mail.gmail.com>
References <727ec5d8-d292-4012-ba87-da77a1040c6a@googlegroups.com> <CALwzidmE5_LyKfEGqAGeN0Uv+g1fFo+ztjhZdr3b3vbRRnTvJA@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 5 Oct 2012 15:43:31 -0600
Subject Re: fmap(), "inverse" of Python map() function
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1875.1349473443.27098.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349473443 news.xs4all.nl 6910 [2001:888:2000:d::a6]:57114
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30844

Show key headers only | View raw


On Fri, Oct 5, 2012 at 3:31 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Oct 5, 2012 at 2:19 PM, vasudevram <vasudevram@gmail.com> wrote:
>>
>> http://jugad2.blogspot.in/2012/10/fmap-inverse-of-python-map-function.html
>
> Your fmap is a special case of reduce.
>
> def fmap(functions, argument):
>     return reduce(lambda result, func: func(result), functions, argument)

In a more functional style, you could also use reduce to compose the
functions before applying them:

def compose(f, g):
    return lambda x: f(g(x))

def fmap(functions):
    return reduce(compose, reversed(functions))

# Allowing you to then do:
result = fmap(functions)(argument)

Cheers,
Ian

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


Thread

fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-05 13:19 -0700
  Re: fmap(), "inverse" of Python map() function Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-05 15:31 -0600
  Re: fmap(), "inverse" of Python map() function Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-05 15:43 -0600
  Re: fmap(), "inverse" of Python map() function Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-05 18:52 -0400
  Re: fmap(), "inverse" of Python map() function Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-05 17:24 -0600
  Re: fmap(), "inverse" of Python map() function Devin Jeanpierre <jeanpierreda@gmail.com> - 2012-10-05 19:30 -0400
    Re: fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-06 11:57 -0700
      Re: fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-06 15:07 -0700
      Re: fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-06 15:07 -0700
    Re: fmap(), "inverse" of Python map() function vasudevram <vasudevram@gmail.com> - 2012-10-06 11:57 -0700

csiph-web