Skip to main content
added 240 characters in body
Source Link
Beat Richartz
  • 9.7k
  • 1
  • 35
  • 51

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an arbitrary number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is arbitrary (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(n) This way, the currying gets applied as if the proc would take n arguments. For example:

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with an arbitrary number of arguments? That means, if n is not given?

One way that comes into my mind is to collect the arguments via proxying call and then resolve the proc via method_missing (if any method other than call is used / call is used without arguments, call the proc with the collected arguments), but I'm still looking for other ways to achieve it.

Update

As Andy H stated, the problem is when to stop currying. For my purposes, it would be ok if the currying stops / the proc evaluates when either any method besides call is invoked or call is invoked without arguments.

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an arbitrary number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is arbitrary (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(n) This way, the currying gets applied as if the proc would take n arguments. For example:

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with an arbitrary number of arguments? That means, if n is not given?

One way that comes into my mind is to collect the arguments via proxying call and then resolve the proc via method_missing (if any method other than call is used / call is used without arguments, call the proc with the collected arguments), but I'm still looking for other ways to achieve it.

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an arbitrary number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is arbitrary (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(n) This way, the currying gets applied as if the proc would take n arguments. For example:

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with an arbitrary number of arguments? That means, if n is not given?

One way that comes into my mind is to collect the arguments via proxying call and then resolve the proc via method_missing (if any method other than call is used / call is used without arguments, call the proc with the collected arguments), but I'm still looking for other ways to achieve it.

Update

As Andy H stated, the problem is when to stop currying. For my purposes, it would be ok if the currying stops / the proc evaluates when either any method besides call is invoked or call is invoked without arguments.

Reformulate & clarify
Source Link
Beat Richartz
  • 9.7k
  • 1
  • 35
  • 51

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an arbitrary number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is arbitrary (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(3n) This way, the currying gets applied as if the proc would take threen arguments. For example:

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with an arbitrary number of arguments? That means, if n is not given?

One way that comes into my mind is to collect the arguments via proxying call and then resolve the proc via method_missing (if any method other than call is used / call is used without arguments, call the proc with the collected arguments), but I'm still looking for other ways to achieve it.

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an arbitrary number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is arbitrary (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(3) This way, the currying gets applied as if the proc would take three arguments.

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with an arbitrary number of arguments?

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an arbitrary number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is arbitrary (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(n) This way, the currying gets applied as if the proc would take n arguments. For example:

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with an arbitrary number of arguments? That means, if n is not given?

One way that comes into my mind is to collect the arguments via proxying call and then resolve the proc via method_missing (if any method other than call is used / call is used without arguments, call the proc with the collected arguments), but I'm still looking for other ways to achieve it.

corrected use of indefinite vs arbitrary
Source Link
Beat Richartz
  • 9.7k
  • 1
  • 35
  • 51

Proc#curry and the splat operator: Currying with indefinitearbitrary number of arguments

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an indefinitearbitrary number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is indefinitearbitrary (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(3) This way, the currying gets applied as if the proc would take three arguments.

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with indefinitean arbitrary number of arguments?

Proc#curry and the splat operator: Currying with indefinite number of arguments

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an indefinite number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is indefinite (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(3) This way, the currying gets applied as if the proc would take three arguments.

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with indefinite number of arguments?

Proc#curry and the splat operator: Currying with arbitrary number of arguments

Ruby 1.9's built in support of currying supports two ways to deal with a proc taking an arbitrary number of arguments:

my_proc = proc {|*x| x.max } 
  1. curry without arguments: my_proc.curry. You pass the comma-separated arguments to the curried proc like you would to the normal proc. This does not achieve proper currying if the number of arguments is arbitrary (it is useful if some of the arguments are not splat)

  2. curry with arguments: my_proc.curry(3) This way, the currying gets applied as if the proc would take three arguments.

    my_proc.curry(3).call(2).call(5).call(1) #=> 5

So, how would you achieve currying with an arbitrary number of arguments?

added 10 characters in body
Source Link
Beat Richartz
  • 9.7k
  • 1
  • 35
  • 51
Loading
edited tags
Link
eugen
  • 9.3k
  • 11
  • 61
  • 67
Loading
Source Link
Beat Richartz
  • 9.7k
  • 1
  • 35
  • 51
Loading