This page is no longer maintained — Please continue to the home page at www.scala-lang.org

How to make a partially applied ()=>a function from a multiple parameters function.

2 replies
keke
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.
Hi,

From a zero parameter function
  def a = 1
I can create a partially applied function by using a _, which is of type ()=>Int.

Is it also possible to create a ()=>Int from function has parameters, like
  def b(x:Int) = 1 + x

I want to create a ()=>a function from an existing multiple parameters function, but do not want to evaluate its value.

--
Cheers,
Keke
-----------------
We paranoid love life
keke
Joined: 2009-01-04,
User offline. Last seen 42 years 45 weeks ago.
Re: How to make a partially applied ()=>a function from a multip
PS.

I can do it using this weird carried function syntax:
  def c(x:Int)() = 1 + x

When using c(1) it gets evaluated and return 2, but when using c(1) _ it returns an function ()=>Int.



On Sun, Jan 4, 2009 at 4:38 PM, keke <iamkeke [at] gmail [dot] com> wrote:
Hi,

From a zero parameter function
  def a = 1
I can create a partially applied function by using a _, which is of type ()=>Int.

Is it also possible to create a ()=>Int from function has parameters, like
  def b(x:Int) = 1 + x

I want to create a ()=>a function from an existing multiple parameters function, but do not want to evaluate its value.

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: How to make a partially applied ()=>a function from a multi


On Sun, Jan 4, 2009 at 12:38 AM, keke <iamkeke [at] gmail [dot] com> wrote:
Hi,

From a zero parameter function
  def a = 1
I can create a partially applied function by using a _, which is of type ()=>Int.

Is it also possible to create a ()=>Int from function has parameters, like
  def b(x:Int) = 1 + x

scala> def b(x: Int) = x + 1b: (Int)Int
scala> val pab = () => b(42)pab: () => Int = <function>
scala> pab() res9: Int = 43
 


I want to create a ()=>a function from an existing multiple parameters function, but do not want to evaluate its value.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland