473,671 Members | 2,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I write code using FP

In Function Program£¬Langua ge can use like this:

define a function:
f = lambda x,y:x,y

then we use f to define another function:
f2 = f(1)
the f2 should equal to:
f2=lambda y:1,y

we should be able call f2 with one parameter:f2(2)
just return 1,2

but how can I implement the characteristic in Python??I think it could
do like this but HOW-TO?

thanks~~~
I'm a English beginner!
I wish somebody understand my meaning

Jan 17 '07 #1
3 1381
On Jan 16, 9:18 pm, "kernel1983 " <kernel1...@gma il.comwrote:
In Function Program£¬Langua ge can use like this:

define a function:
f = lambda x,y:x,y

then we use f to define another function:
f2 = f(1)
the f2 should equal to:
f2=lambda y:1,y

we should be able call f2 with one parameter:f2(2)
just return 1,2

but how can I implement the characteristic in Python??I think it could
do like this but HOW-TO?

thanks~~~
I'm a English beginner!
I wish somebody understand my meaning
If I understand correctly, you can do:

f = lambda x, y: (x, y)
f2 = lambda y: f(1, y)

Jan 17 '07 #2
kernel1983 wrote:
In Function Program?Languag e can use like this:

define a function:
f = lambda x,y:x,y

then we use f to define another function:
f2 = f(1)
the f2 should equal to:
f2=lambda y:1,y

we should be able call f2 with one parameter:f2(2)
just return 1,2

but how can I implement the characteristic in Python??I think it could
do like this but HOW-TO?
If you are using python 2.5, then you can use functools.parti al, otherwise
lookup one of the many curry implementations , like the ones found in
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52549

One found in the comments of that page by Scott David Daniels:

def curry(*args, **kwargs):
function, args = args[0], args[1:]
def result(*rest, **kwrest):
combined = kwargs.copy()
combined.update (kwrest)
return function(*args + rest, **combined)
return result
so, in your case:

f = lambda x,y: (x,y)
f2 = functools.parti al(f,1)
>>f2(3)
1,3

Jan 17 '07 #3
yeah! that's easy! althought no so clear

a = lambda x,y:(x,y)
b = lambda y:a(1,y)

a(1,2)
(1,2)

b(2)
(1,2)

On 1ÔÂ17ÈÕ, ÏÂÎç12ʱ10·Ö, Alejandro Dubrovsky
<dubrov...@phys ics.uq.edu.auwr ote:
kernel1983 wrote:
In Function Program?Languag e can use like this:
define a function:
f = lambda x,y:x,y
then we use f to define another function:
f2 = f(1)
the f2 should equal to:
f2=lambda y:1,y
we should be able call f2 with one parameter:f2(2)
just return 1,2
but how can I implement the characteristic in Python??I think it could
do like this but HOW-TO?If you are using python 2.5, then you can use functools.parti al, otherwise
lookup one of the many curry implementations , like the ones found inhttp://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/52549

One found in the comments of that page by Scott David Daniels:

def curry(*args, **kwargs):
function, args = args[0], args[1:]
def result(*rest, **kwrest):
combined = kwargs.copy()
combined.update (kwrest)
return function(*args + rest, **combined)
return result

so, in your case:

f = lambda x,y: (x,y)
f2 = functools.parti al(f,1)>>f2(3)1 ,3
Jan 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1751
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is correct, but giving error says " file is accessed by other process cannot access" I have some logic inbetween and write the info using response.write in to html format Ex: Response.Write("<td>" & "client_no" & "</td>"). I am getting the error at :...
8
8738
by: Sowen | last post by:
Hi, all I am wondering how to write bits by using ofstream? I have finished a huffman tree, but how can I write the bits to the file in order to gain compression? for example, 'A' returns a code '1101', what I should write? 13? no, I don't think so
13
9609
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
0
297
by: Bennett Haselton | last post by:
I've noticed that if you enter the following code in the codebehind page for an .aspx page, it won't compile because the call to Trace.Write() is not valid except in methods of a class derived from System.Web.UI.Page. Two questions: 1) I don't know much about C# but I was under the impression that if certain classes and functions were available in a namespace (as the result of a "using" statement at the top of a file), then they were...
5
2106
by: matthew | last post by:
Hi all, I am now writing a aspx that get a session variable (string) and then write it out using Response.Write. The string length is: 494710. But Response.Write only write the string partially! I tried using following code, but it doesn't work, the page is keep loading.
5
5098
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or Partition we are using ReadFile , WriteFile and setFilePointerEx to read and write the sectors and we are reading/writing 102400 sectors together, even we have reduced the sectors still it is not able to read or write, our program is working fine...
0
789
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
3
14049
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
2
20715
by: adypoly | last post by:
Hi guys... I am having a typical problem in using one of the native dll in C# I'll explain what am trying to do, I've a dll written in C language which i am trying to include in my C# project, the solutions builds fine but once i try to run the program it gives an exception as "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" I am passing two integer pointer variables as the...
4
5658
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3) On each FileSystemItem Element, make two child nodes, one with the file name, one with the file size. ie. <filesystemitems> <filesystemitem>
0
8472
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8909
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.