Re: Functional way to compare things inside a list

2012-09-22 Thread Ramchandra Apte
On Saturday, 22 September 2012 12:50:08 UTC+5:30, Andrew Berg  wrote:
> On 2012.09.22 02:08, Jamie Paul Griffin wrote:
> 
> > I find this intriguing, I had no idea bots existed to post to mailing
> 
> > lists in this way. What's the point of them?
> 
> 
> 
> To amuse their owners is my guess.
> 
> -- 
> 
> CPython 3.3.0rc2 | Windows NT 6.1.7601.17835

The bot could be used for automatic replies for duplicate posts (common 
questions are quite often repeated)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-22 Thread Andrew Berg
On 2012.09.22 02:08, Jamie Paul Griffin wrote:
> I find this intriguing, I had no idea bots existed to post to mailing
> lists in this way. What's the point of them?

To amuse their owners is my guess.
-- 
CPython 3.3.0rc2 | Windows NT 6.1.7601.17835
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-22 Thread Jamie Paul Griffin
[ Ian Kelly wrote on Sat 22.Sep'12 at  0:22:43 -0600 ]

> On Fri, Sep 21, 2012 at 7:25 PM, Steven D'Aprano
>  wrote:
> > On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:
> >
> >> On Fri, Sep 21, 2012 at 1:54 PM, 8 Dihedral
> >>  wrote:
> >>> I don't think functional aspects are only marked as lazy programming.
> >>
> >> He wrote "lazy evaluation", not "lazy programming".  Two entirely
> >> different things.
> >
> >
> > For the record, the consensus here is that 8 Dihedral is probably a
> > bot. It appears to be a pretty good bot, I haven't spotted it making any
> > egregious or obvious grammatical mistakes, but the semantics of its posts
> > don't seem quite human.
> 
> I'm aware of that, although sometimes the posts seem coherent enough
> that I think maybe it's not.  Especially the ones where it posts
> almost-working code snippets, complete with obvious typos.
> 
> Then it posts a complete non sequitur like the reply to my reply in
> this thread, and the illusion is shattered.

I find this intriguing, I had no idea bots existed to post to mailing
lists in this way. What's the point of them?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread 88888 Dihedral
Ian於 2012年9月22日星期六UTC+8下午2時23分43秒寫道:
> On Fri, Sep 21, 2012 at 7:25 PM, Steven D'Aprano
> 
>  wrote:
> 
> > On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:
> 
> >
> 
> >> On Fri, Sep 21, 2012 at 1:54 PM, 8 Dihedral
> 
> >>  wrote:
> 
> >>> I don't think functional aspects are only marked as lazy programming.
> 
> >>
> 
> >> He wrote "lazy evaluation", not "lazy programming".  Two entirely
> 
> >> different things.
> 
> >
> 
> >
> 
> > For the record, the consensus here is that 8 Dihedral is probably a
> 
> > bot. It appears to be a pretty good bot, I haven't spotted it making any
> 
> > egregious or obvious grammatical mistakes, but the semantics of its posts
> 
> > don't seem quite human.
> 
> 
> 
> I'm aware of that, although sometimes the posts seem coherent enough
> 
> that I think maybe it's not.  Especially the ones where it posts
> 
> almost-working code snippets, complete with obvious typos.
> 
> 
> 
> Then it posts a complete non sequitur like the reply to my reply in
> 
> this thread, and the illusion is shattered.

Do you want to use the lisp way for implementing functional programming?

 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Ian Kelly
On Fri, Sep 21, 2012 at 7:25 PM, Steven D'Aprano
 wrote:
> On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:
>
>> On Fri, Sep 21, 2012 at 1:54 PM, 8 Dihedral
>>  wrote:
>>> I don't think functional aspects are only marked as lazy programming.
>>
>> He wrote "lazy evaluation", not "lazy programming".  Two entirely
>> different things.
>
>
> For the record, the consensus here is that 8 Dihedral is probably a
> bot. It appears to be a pretty good bot, I haven't spotted it making any
> egregious or obvious grammatical mistakes, but the semantics of its posts
> don't seem quite human.

I'm aware of that, although sometimes the posts seem coherent enough
that I think maybe it's not.  Especially the ones where it posts
almost-working code snippets, complete with obvious typos.

Then it posts a complete non sequitur like the reply to my reply in
this thread, and the illusion is shattered.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Steven D'Aprano
On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:

> On Fri, Sep 21, 2012 at 1:54 PM, 8 Dihedral
>  wrote:
>> I don't think functional aspects are only marked as lazy programming.
> 
> He wrote "lazy evaluation", not "lazy programming".  Two entirely
> different things.


For the record, the consensus here is that 8 Dihedral is probably a 
bot. It appears to be a pretty good bot, I haven't spotted it making any 
egregious or obvious grammatical mistakes, but the semantics of its posts 
don't seem quite human.

8 Dihedral, if you're not a bot, you can go a long way towards 
proving that by telling us what colour a purple elephant is.


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread 88888 Dihedral
A

Ian於 2012年9月22日星期六UTC+8上午4時50分49秒寫道:
> On Fri, Sep 21, 2012 at 1:54 PM, 8 Dihedral
> 
>  wrote:
> 
> > I don't think functional aspects are only marked as lazy
> 
> > programming.
> 
> 
> 
> He wrote "lazy evaluation", not "lazy programming".  Two entirely
> 
> different things.
> 
> 
> 
> > It just means when one is experimenting something
> 
> > the efficient execution in speed is not on focus
> 
> > yet.
> 
> 
> 
> No, what you're describing is a "prototype".  It has nothing to do
> 
> with functional programming at all.

A function with varaible arguments can be stored as a variable
to functions called decorators in python to return  enhanced functions.

A function mapps a decorator to another decorator can be called 
a decorator map or a decorator maker in python.

The closure level is guaranteed for decorators to be mapped by 
multi-levels of decorator mappers trivially in python.

What do you want else for functional prgramming in python?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Joshua Landau
On 21 September 2012 21:49, Ian Kelly  wrote:

> On Fri, Sep 21, 2012 at 1:54 PM, 8 Dihedral
>  wrote:
> > I don't think functional aspects are only marked as lazy
> > programming.
>
> He wrote "lazy evaluation", not "lazy programming".  Two entirely
> different things.
>
> > It just means when one is experimenting something
> > the efficient execution in speed is not on focus
> > yet.
>
> No, what you're describing is a "prototype".  It has nothing to do
> with functional programming at all.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I am pretty sure dihedral8 is a bot.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Ian Kelly
On Fri, Sep 21, 2012 at 1:54 PM, 8 Dihedral
 wrote:
> I don't think functional aspects are only marked as lazy
> programming.

He wrote "lazy evaluation", not "lazy programming".  Two entirely
different things.

> It just means when one is experimenting something
> the efficient execution in speed is not on focus
> yet.

No, what you're describing is a "prototype".  It has nothing to do
with functional programming at all.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread 88888 Dihedral
Ulrich Eckhardt於 2012年9月21日星期五UTC+8下午5時15分03秒寫道:
> Am 21.09.2012 00:58, schrieb thorso...@lavabit.com:
> 
> > list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
> 
> >
> 
> > I want to check for a value (e.g. '4'), and get the key of the dictionary
> 
> > that contains that value.
> 
> 
> 
> Note:
> 
> 1. list is a built-in type, who's name is rebound above
> 
> 2. The list above contains dictionaries that all only contain a single key?
> 
> 3. You have strings containing decimal representations of numbers?
> 
> 
> 
>  > (Yep, this is bizarre.)
> 
> 
> 
> The data are really stored in a strange way and you might be able to 
> 
> make things clearer by reorganizing them a bit.
> 
> 
> 
> 
> 
> > some_magic(list, '4')
> 
> > => '3'
> 
> >
> 
> > What's the functional way to do it?
> 
> 
> 
> Functional as in functional programming and an emphasis on lazy 
> 
> evaluation? In that case I'd write a generator that emits the keys where 
> 
> the values contain the requested string.
> 
> 
> 
> 
> 
> > Is it possible to do it with a one-liner?
> 
> 
> 
> Yep, filter(), lambda and the 'in' operator. Question remains if this is 
> 
> readable. Note that you can use a local function, too, if you just want 
> 
> to reduce the scope/visibility.
> 
> 
> 
> 
> 
> Good luck!
> 
> 
> 
> 
> 
> Uli

I don't think functional aspects are only marked as lazy
programming. 

It just means when one is experimenting something 
the efficient execution in speed is not on focus 
yet. 

 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Ulrich Eckhardt

Am 21.09.2012 00:58, schrieb thorso...@lavabit.com:

list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]

I want to check for a value (e.g. '4'), and get the key of the dictionary
that contains that value.


Note:
1. list is a built-in type, who's name is rebound above
2. The list above contains dictionaries that all only contain a single key?
3. You have strings containing decimal representations of numbers?

> (Yep, this is bizarre.)

The data are really stored in a strange way and you might be able to 
make things clearer by reorganizing them a bit.




some_magic(list, '4')
=> '3'

What's the functional way to do it?


Functional as in functional programming and an emphasis on lazy 
evaluation? In that case I'd write a generator that emits the keys where 
the values contain the requested string.




Is it possible to do it with a one-liner?


Yep, filter(), lambda and the 'in' operator. Question remains if this is 
readable. Note that you can use a local function, too, if you just want 
to reduce the scope/visibility.



Good luck!


Uli

--
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Alexander Blinne
On 21.09.2012 00:58, thorso...@lavabit.com wrote:
> Hi,
> 
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
> 
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.
> (Yep, this is bizarre.)
> 
> some_magic(list, '4')
> => '3'
> 
> What's the functional way to do it?
> Is it possible to do it with a one-liner?

simple, but possibly slow solution:

import itertools

def some_magic(list, search):
return (key for key, val in itertools.chain(*(d.iteritems() for d in
list)) if search in val).next()

one-liner, yeah...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Chris Rebert
On Thu, Sep 20, 2012 at 3:58 PM,   wrote:
> Hi,
>
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]

Are the dictionaries each guaranteed to only contain a single
key-value pair? (Or is your example just simplistic?)

> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.

And what if there is no such dictionary? Or what if there are multiple
such dictionaries?

> (Yep, this is bizarre.)
>
> some_magic(list, '4')
> => '3'
>
> What's the functional way to do it?

Why do you care about the paradigm used?

> Is it possible to do it with a one-liner?

Who cares? It's possible to implement more complicated things in one
line of APL, but most people probably wouldn't recommend it.

Regards,
Chris R.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Ivan@work

On 21.09.2012 00:58, thorso...@lavabit.com wrote:

Hi,

list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]

I want to check for a value (e.g. '4'), and get the key of the dictionary
that contains that value.
(Yep, this is bizarre.)

some_magic(list, '4')
=> '3'

What's the functional way to do it?
Is it possible to do it with a one-liner?




Yes:

[key for d in list for key in d if '4' in d[key]]

--
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Chris Angelico
On Fri, Sep 21, 2012 at 6:28 PM, Chris Rebert  wrote:
> On Fri, Sep 21, 2012 at 1:23 AM, Chris Angelico  wrote:
>> On Fri, Sep 21, 2012 at 8:58 AM,   wrote:
> 
>> That gets the result, but probably not in the cleanest way. I'm not
>> sure off-hand if Python has a convenient way to curry a function,
>
> http://docs.python.org/library/functools.html#functools.partial

Thanks, that'd be it; didn't come up in a quick search for 'curry'
(for obvious reason). So it would probably be possible to do it as a
one-liner with a honking big lambda, but if code clarity is what you
want, I'd go with the externally-defined function.

> Cheers,
> Chris of the Northern Hemisphere

:)

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Chris Rebert
On Fri, Sep 21, 2012 at 1:23 AM, Chris Angelico  wrote:
> On Fri, Sep 21, 2012 at 8:58 AM,   wrote:

> That gets the result, but probably not in the cleanest way. I'm not
> sure off-hand if Python has a convenient way to curry a function,

http://docs.python.org/library/functools.html#functools.partial

Cheers,
Chris of the Northern Hemisphere
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Functional way to compare things inside a list

2012-09-21 Thread Chris Angelico
On Fri, Sep 21, 2012 at 8:58 AM,   wrote:
> Hi,
>
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
>
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.
> (Yep, this is bizarre.)
>
> some_magic(list, '4')
> => '3'
>
> What's the functional way to do it?
> Is it possible to do it with a one-liner?

I'm thinking here of a list comprehension, filter(), and next() to
grab the first element. Let's see...

By the way, I wouldn't use 'list' as a variable name; you shadow the
built-in type.

lst = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
def find_n(n,dic):
for key,searchme in dic.items():
if n in searchme: return key

next(filter(None,[find_n('4',x) for x in lst]))

That gets the result, but probably not in the cleanest way. I'm not
sure off-hand if Python has a convenient way to curry a function, but
if so, you could make the filter call rather simpler.

Note that this is written for Python 3, where filter() returns an
iterator, thus the algorithm is lazy and thus efficient (a very
Australian way to do things).

ChrisA
Proudly Australian, proudly lazy!
-- 
http://mail.python.org/mailman/listinfo/python-list


Functional way to compare things inside a list

2012-09-21 Thread thorsopia
Hi,

list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]

I want to check for a value (e.g. '4'), and get the key of the dictionary
that contains that value.
(Yep, this is bizarre.)

some_magic(list, '4')
=> '3'

What's the functional way to do it?
Is it possible to do it with a one-liner?






-- 
http://mail.python.org/mailman/listinfo/python-list