Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: spec: currying of arbitrary functions #4633

Closed
gopherbot opened this issue Jan 8, 2013 · 7 comments
Closed

proposal: spec: currying of arbitrary functions #4633

gopherbot opened this issue Jan 8, 2013 · 7 comments
Labels
FrozenDueToAge LanguageChange Thinking v2 A language change or incompatible library change
Milestone

Comments

@gopherbot
Copy link

by fjanon:

I asked the question at:

https://groups.google.com/group/golang-nuts/browse_thread/thread/ccc2b7dc7d6d7b8e/949385859868c008?lnk=gst&;q=fjanon#

Michael T. Jones CTA @ Google replied and I thought that maybe the Go team could have a
look and decide if it could be added to Go someday.

Thanks.

Original question:

I have been using function addresses for a long time in different 
languages but I miss an *easy* way of getting the address of a 
function with its arguments and then call it later on (lazy eval). 
Right now, storing the arguments and the address of the function in a 
data structure and then calling the function with the arguments stored 
is the only easy way to do it that I know of or currying with the 
extra code required. 
I think that something like 
pf := &somefunc("a str", 1) 
and then 
pf() 
would be useful. 
I know that with closure we can curry functions but it's a bit 
convoluted, there might be a simpler way or syntax to do it. 
Maybe something like that in Go 2.0? 
Fred 

==========================================
Notable difference: 
somefunc signature: 
func name(args) (T2,T3,...) 
f := &somefunc("a str", 1) 
: 
pf() 
compiler knows T2, T3, etc., and can simply do the right thing. 
pf := func () { somefunc("a str", 1) } 
is a perfect example for the asked-for case, but when T2, T3, ... exist 
then the function wrapper must be: 
pf := func () (T2,T3,...) { return somefunc("a str", 1) } 
and the programmer needs to know T2,T3,... and express them perfectly as 
somefunc changes. In the spirit of the C-world's typeof(x), Go could have 
TypeOf(x) and then one could write: 
pf := func () TypeOf(somefunc) { return somefunc("a str", 1) } 
: 
v1, v2, ...vn := pf() 
was would be the case in an expanded version of the original proposal: 
f := &somefunc("a str", 1) 
: 
v1,v2,...vn := pf() 
On Mon, Aug 13, 2012 at 7:06 AM, chris dollin <ehog.he...@googlemail.com>wrote: 

- Show quoted text -
-- 
Michael T. Jones | Chief Technology Advocate  | m...@google.com |  +1 
650-335-5765
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added priority-later, languagechange, removed priority-triage, go1.1.

Status changed to Thinking.

@rsc
Copy link
Contributor

rsc commented Jan 9, 2013

Comment 2:

Labels changed: added priority-someday, removed priority-later.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 3:

Labels changed: added repo-main.

@rsc
Copy link
Contributor

rsc commented Mar 3, 2014

Comment 4:

Adding Release=None to all Priority=Someday bugs.

Labels changed: added release-none.

@verhas
Copy link

verhas commented May 26, 2016

I think that currying partially is supported by the language when the curried parameter is the receiver:

package main

import (
    "fmt"
)

type curried struct {
    field int
}

func (c *curried) printSum(b int) {
    fmt.Printf("%d\n", c.field+b)
    c.field = c.field + 1
}

func main() {
    c := curried{13}
    z := c.printSum
    z(7)
    c.printSum(7)
    c.printSum(6)
}

https://play.golang.org/p/WWJVCBRzxU

@rsc rsc changed the title spec: currying of arbitrary functions proposal: spec: currying of arbitrary functions Jun 20, 2017
@rsc rsc added the v2 A language change or incompatible library change label Jun 20, 2017
@ghasemloo
Copy link

ghasemloo commented Jul 12, 2017

You can also do so using anonymous functions and capturing:

f := func() { somefunc("a str", 1) }
.
.
.
f()

@ianlancetaylor
Copy link
Contributor

This issue is basically about syntactic sugar for simple functions with arguments. As such I'm going to close this as a dup of #21498 as that issue has a lot more discussion on it.

@golang golang locked and limited conversation to collaborators Nov 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Thinking v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants