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


Groups > comp.lang.python > #80785 > unrolled thread

Array of Functions

Started byRichard Riehle <rriehle@itu.edu>
First post2014-11-14 14:17 -0800
Last post2014-11-17 13:56 -0800
Articles 9 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Array of Functions Richard Riehle <rriehle@itu.edu> - 2014-11-14 14:17 -0800
    Re: Array of Functions Marko Rauhamaa <marko@pacujo.net> - 2014-11-15 00:21 +0200
    Re: Array of Functions Sam Raker <sam.raker@gmail.com> - 2014-11-14 14:44 -0800
    Re: Array of Functions Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-14 15:50 -0700
    Re: Array of Functions MRAB <python@mrabarnett.plus.com> - 2014-11-14 23:02 +0000
    Re: Array of Functions Terry Reedy <tjreedy@udel.edu> - 2014-11-14 19:11 -0500
    Re: Array of Functions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-14 19:13 -0500
      Re: Array of Functions Richard Riehle <rriehle@itu.edu> - 2014-11-16 23:52 -0800
    Re: Array of Functions Anton <anschatten@gmail.com> - 2014-11-17 13:56 -0800

#80785 — Array of Functions

FromRichard Riehle <rriehle@itu.edu>
Date2014-11-14 14:17 -0800
SubjectArray of Functions
Message-ID<42b534f0-aeef-411f-80c5-db5d20c80b55@googlegroups.com>
In C, C++, Ada, and functional languages, I can create an array of functions, albeit with the nastiness of pointers in the C family.   For example, an array of functions where each function is an active button, or an array of functions that behave like formulae in a spreadsheet.  I am finding this a bit challenging in Python.   

Example:

            r1c1   r1c2  r1c3
            r2c1   r2c2  r2c3
            r3c1   r3c2  r3c3

where r1 is row 1 and c1 is column 1.  Suppose I want an array where the colum three is a set of functions that operates on the other two columns, depending on the values I set for those rows and columns?    As noted, I can do this pretty easily in most languages (well, except for Java which does not support any kind of functional programming capability), even if I have to use pointers.

I think my difficulty is related to the REPL nature of Python.  However, I am sure some clever Pythonista has found a way to do this.   

Thanks in advance for any suggestions,

Richard Riehle, PhD, International Technological University, San Jose, CA

[toc] | [next] | [standalone]


#80787

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-11-15 00:21 +0200
Message-ID<871tp5mnja.fsf@elektro.pacujo.net>
In reply to#80785
Richard Riehle <rriehle@itu.edu>:

> Example:
>
>             r1c1   r1c2  r1c3
>             r2c1   r2c2  r2c3
>             r3c1   r3c2  r3c3
>
> where r1 is row 1 and c1 is column 1. Suppose I want an array where the
> colum three is a set of functions that operates on the other two
> columns, depending on the values I set for those rows and columns? As
> noted, I can do this pretty easily in most languages (well, except for
> Java which does not support any kind of functional programming
> capability), even if I have to use pointers.

Show some code in, say, Scheme (which is among the most functional
programming languages in existence), and we'll see how we could
translate that into Python.


Marko

[toc] | [prev] | [next] | [standalone]


#80789

FromSam Raker <sam.raker@gmail.com>
Date2014-11-14 14:44 -0800
Message-ID<b62aa333-8aa4-486d-b140-0042f809a39f@googlegroups.com>
In reply to#80785
I second the call for a more concrete implementation, but if you want the results of the functions in c3 to be responsive to the values of c1 and c2 (i.e., if you change r1c1, r1c3 returns a different value), it might be worth encapsulating the whole thing in an object and making the c3 functions properties, which is Python for 'preprocessed attributes.' 

On Friday, November 14, 2014 5:17:38 PM UTC-5, Richard Riehle wrote:
> In C, C++, Ada, and functional languages, I can create an array of functions, albeit with the nastiness of pointers in the C family.   For example, an array of functions where each function is an active button, or an array of functions that behave like formulae in a spreadsheet.  I am finding this a bit challenging in Python.   
> 
> Example:
> 
>             r1c1   r1c2  r1c3
>             r2c1   r2c2  r2c3
>             r3c1   r3c2  r3c3
> 
> where r1 is row 1 and c1 is column 1.  Suppose I want an array where the colum three is a set of functions that operates on the other two columns, depending on the values I set for those rows and columns?    As noted, I can do this pretty easily in most languages (well, except for Java which does not support any kind of functional programming capability), even if I have to use pointers.
> 
> I think my difficulty is related to the REPL nature of Python.  However, I am sure some clever Pythonista has found a way to do this.   
> 
> Thanks in advance for any suggestions,
> 
> Richard Riehle, PhD, International Technological University, San Jose, CA

[toc] | [prev] | [next] | [standalone]


#80791

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-11-14 15:50 -0700
Message-ID<mailman.15832.1416005493.18130.python-list@python.org>
In reply to#80785
On Fri, Nov 14, 2014 at 3:17 PM, Richard Riehle <rriehle@itu.edu> wrote:
> In C, C++, Ada, and functional languages, I can create an array of functions, albeit with the nastiness of pointers in the C family.   For example, an array of functions where each function is an active button, or an array of functions that behave like formulae in a spreadsheet.  I am finding this a bit challenging in Python.
>
> Example:
>
>             r1c1   r1c2  r1c3
>             r2c1   r2c2  r2c3
>             r3c1   r3c2  r3c3
>
> where r1 is row 1 and c1 is column 1.  Suppose I want an array where the colum three is a set of functions that operates on the other two columns, depending on the values I set for those rows and columns?    As noted, I can do this pretty easily in most languages (well, except for Java which does not support any kind of functional programming capability), even if I have to use pointers.
>
> I think my difficulty is related to the REPL nature of Python.  However, I am sure some clever Pythonista has found a way to do this.

>>> list_of_functions = [lambda x=x: x + 4 for x in range(3)]
>>> list_of_functions[0]()
4
>>> list_of_functions[1]()
5
>>> list_of_functions[2]()
6

If you want more help, you'll have to be more specific about the
problem you're encountering. Since you didn't go into any detail, I'm
just going to assume that it's the common pitfall of variable scoping
in functions defined in a loop and point you to this FAQ answer:

https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result

[toc] | [prev] | [next] | [standalone]


#80792

FromMRAB <python@mrabarnett.plus.com>
Date2014-11-14 23:02 +0000
Message-ID<mailman.15833.1416006313.18130.python-list@python.org>
In reply to#80785
On 2014-11-14 22:17, Richard Riehle wrote:
> In C, C++, Ada, and functional languages, I can create an array of
> functions, albeit with the nastiness of pointers in the C family.
> For example, an array of functions where each function is an active
> button, or an array of functions that behave like formulae in a
> spreadsheet.  I am finding this a bit challenging in Python.
>
> Example:
>
>              r1c1   r1c2  r1c3
>              r2c1   r2c2  r2c3
>              r3c1   r3c2  r3c3
>
> where r1 is row 1 and c1 is column 1.  Suppose I want an array where
> the colum three is a set of functions that operates on the other two
> columns, depending on the values I set for those rows and columns?
> As noted, I can do this pretty easily in most languages (well, except
> for Java which does not support any kind of functional programming
> capability), even if I have to use pointers.
>
> I think my difficulty is related to the REPL nature of Python.
> However, I am sure some clever Pythonista has found a way to do
> this.
>
> Thanks in advance for any suggestions,
>
In C, you're not creating an array of functions, but an array of
_pointers_ to functions.

In Python, we don't have pointers, but we do have references.

For example, you can define a function:

 >>> def my_func():
...     print('my_func called')
...

The name of the function is just a reference to that function:

 >>> print(my_func)
<function my_func at 0x0000000002CBCBF8>

You call it using (...):

 >>> my_func()
my_func called

You can store the reference in a list or bind another name to it:

 >>> func_by_another_name = my_func

And call it by that other name:

 >>> func_by_another_name()
my_func called

Does that help?

[toc] | [prev] | [next] | [standalone]


#80794

FromTerry Reedy <tjreedy@udel.edu>
Date2014-11-14 19:11 -0500
Message-ID<mailman.15834.1416010316.18130.python-list@python.org>
In reply to#80785
On 11/14/2014 5:17 PM, Richard Riehle wrote:
> In C, C++, Ada, and functional languages, I can create an array of
> functions, albeit with the nastiness of pointers in the C family.
> For example, an array of functions where each function is an active
> button, or an array of functions that behave like formulae in a
> spreadsheet.

Googling 'python spreadsheet', the first hit is
https://manns.github.io/pyspread/
"Pyspread expects Python expressions in its grid cells,"
and expressions can reference other cells.

There may be other relevant programs.

> I am finding this a bit challenging in Python.

Since python functions are objects, they easy fit into any array of 
objects. They can also be the values in dicts.  Since strings can be 
passed to built-in runtime compile, eval, and exec functions, (not part 
of standard C), functions can also be represented by a return 
expression, as in Pyspread, or even quoted def statements.

A program that executes user-entered code is much easier in Python than 
in C or any other language where one would have to write or find lexer, 
parser, compiler, and execution functions.

> I think my difficulty is related to the REPL nature of Python.

I do not understand this. The interpreter runs a REPL in interactive 
mode but can also run in batch mode.


-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#80796

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-11-14 19:13 -0500
Message-ID<mailman.15835.1416010397.18130.python-list@python.org>
In reply to#80785
On Fri, 14 Nov 2014 14:17:23 -0800 (PST), Richard Riehle <rriehle@itu.edu>
declaimed the following:

>In C, C++, Ada, and functional languages, I can create an array of functions, albeit with the nastiness of pointers in the C family.   For example, an array of functions where each function is an active button, or an array of functions that behave like formulae in a spreadsheet.  I am finding this a bit challenging in Python.   
>
>Example:
>
>            r1c1   r1c2  r1c3
>            r2c1   r2c2  r2c3
>            r3c1   r3c2  r3c3
>

	Personally (and tied to Windows) -- I'd likely load the win32
extensions package and, since I do have M$ Office, programmatically create
that spreadsheet in a hidden Excel instance.

	Caveat: I've never actually done that... M$'s API has never felt
comfortable to me (I was spoiled by ARexx and applications with ARexx ports
on the Amiga).

	I don't know of any way to "magically" link your column three to using
arguments from the other two columns. Creating a table where each row is
two values and a function is no problem -- but I'd need to use external
code to run each row:

	results = [ f(x, y) for (x, y, f) in table]


		
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#80964

FromRichard Riehle <rriehle@itu.edu>
Date2014-11-16 23:52 -0800
Message-ID<323fad4c-0a60-4b3e-9f08-fe3edc134424@googlegroups.com>
In reply to#80796
On Friday, November 14, 2014 4:13:28 PM UTC-8, Dennis Lee Bieber wrote:
> On Fri, 14 Nov 2014 14:17:23 -0800 (PST), Richard Riehle <rriehle@itu.edu>
> declaimed the following:
> 
> >In C, C++, Ada, and functional languages, I can create an array of functions, albeit with the nastiness of pointers in the C family.   For example, an array of functions where each function is an active button, or an array of functions that behave like formulae in a spreadsheet.  I am finding this a bit challenging in Python.   
> >
> >Example:
> >
> >            r1c1   r1c2  r1c3
> >            r2c1   r2c2  r2c3
> >            r3c1   r3c2  r3c3
> >
> 
> 	Personally (and tied to Windows) -- I'd likely load the win32
> extensions package and, since I do have M$ Office, programmatically create
> that spreadsheet in a hidden Excel instance.
> 
> 	Caveat: I've never actually done that... M$'s API has never felt
> comfortable to me (I was spoiled by ARexx and applications with ARexx ports
> on the Amiga).
> 
> 	I don't know of any way to "magically" link your column three to using
> arguments from the other two columns. Creating a table where each row is
> two values and a function is no problem -- but I'd need to use external
> code to run each row:
> 
> 	results = [ f(x, y) for (x, y, f) in table]
> 
> 
> 		
> -- 
> 	Wulfraed                 Dennis Lee Bieber         AF6VN
>     wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Thanks to everyone for their suggestions.   I got this to work, and even created a very useful dictionary with it.

Richard Riehle, PhD

[toc] | [prev] | [next] | [standalone]


#80983

FromAnton <anschatten@gmail.com>
Date2014-11-17 13:56 -0800
Message-ID<6bd391b1-c20c-4fb0-97b9-5ae7aa32b2c4@googlegroups.com>
In reply to#80785
On Friday, November 14, 2014 2:17:38 PM UTC-8, Richard Riehle wrote:
> In C, C++, Ada, and functional languages, I can create an array of functions, albeit with the nastiness of pointers in the C family.   For example, an array of functions where each function is an active button, or an array of functions that behave like formulae in a spreadsheet.  I am finding this a bit challenging in Python.   
> 
> Example:
> 
>             r1c1   r1c2  r1c3
>             r2c1   r2c2  r2c3
>             r3c1   r3c2  r3c3
> 
> where r1 is row 1 and c1 is column 1.  Suppose I want an array where the colum three is a set of functions that operates on the other two columns, depending on the values I set for those rows and columns?    As noted, I can do this pretty easily in most languages (well, except for Java which does not support any kind of functional programming capability), even if I have to use pointers.
> 
> I think my difficulty is related to the REPL nature of Python.  However, I am sure some clever Pythonista has found a way to do this.   
> 
> Thanks in advance for any suggestions,
> 
> Richard Riehle, PhD, International Technological University, San Jose, CA

Could you share a code snippet in C/C++ that would do what you need?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web