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

type currying

6 replies
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
I hacked on the parser just a very little bit to make this possible.  The changes are little more than stopping it from gratuitously failing on back-to-back bracketed lists.

import scala.collection.{ mutable, immutable, generic } import generic.CanBuildFrom
object Partial {  type KnownContainer[CC[K, V] <: collection.Map[K, V]] = {    def values[V] : KnownValues[CC, V]    def apply[K] : KnownKeys[CC, K]   }  type KnownKeys[CC[K, V] <: collection.Map[K, V], K] = {    def apply[V](implicit cbf: CanBuildFrom[_, (K, V), CC[K, V]]): CC[K, V]  }  type KnownValues[CC[K, V] <: collection.Map[K, V], V] = {     def apply[K](implicit cbf: CanBuildFrom[_, (K, V), CC[K, V]]): CC[K, V]  }
  def apply[CC[K, V] <: collection.Map[K, V]] : KnownContainer[CC] = new {    def values[V] : KnownValues[CC, V] = new {       def apply[K](implicit cbf: CanBuildFrom[_, (K, V), CC[K, V]]) = cbf().result    }    def apply[K] = new {      def apply[V](implicit cbf: CanBuildFrom[_, (K, V), CC[K, V]]) = cbf().result     }  }}
scala> val m = Partial[immutable.TreeMap]m: Partial.KnownContainer[scala.collection.immutable.TreeMap] = Partial$$anon$1@20beaacc
scala> val m1 = m[String]m1: Partial.KnownKeys[scala.collection.immutable.TreeMap,String] = Partial$$anon$1$$anon$2@13979163
scala> val m2 = m[Int][Int] m2: scala.collection.immutable.TreeMap[Int,Int] = Map()
scala> val mutableBippy = Partial[mutable.HashMap][String][Int]mutableBippy: scala.collection.mutable.HashMap[String,Int] = Map()
scala> mutableBippy("abc") = 55
scala> println(mutableBippy)Map(abc -> 55)
scala> val immutableBippy = Partial[immutable.HashMap].values[Int] immutableBippy: Partial.KnownValues[scala.collection.immutable.HashMap,Int] = Partial$$anon$1$$anon$3@6d02f1ee
scala> def make[T](xs: T*) = immutableBippy[T] ++ xs.zipWithIndexmake: [T](xs: T*)scala.collection.immutable.Map[T,Int]
scala> make('a' to 'z': _*)res7: scala.collection.immutable.Map[Char,Int] = Map(e -> 4, s -> 18, x -> 23, n -> 13, j -> 9, y -> 24, t -> 19, u -> 20, f -> 5, a -> 0, m -> 12, i -> 8, v -> 21, q -> 16, b -> 1, g -> 6, l -> 11, p -> 15, c -> 2, h -> 7, r -> 17, w -> 22, k -> 10, o -> 14, z -> 25, d -> 3)
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: type currying

On Tue, Dec 27, 2011 at 1:48 AM, Paul Phillips wrote:
> I hacked on the parser just a very little bit to make this possible.  The
> changes are little more than stopping it from gratuitously failing on
> back-to-back bracketed lists.

Is the patch available anywhere?

Cheers,

Miles

Anwar Rizal
Joined: 2011-07-05,
User offline. Last seen 42 weeks 5 days ago.
Re: type currying
Not sure to understand this.
Does this mean that we can now avoid 
({type L[a]=Either[String, a]})#L[A] 




On Tue, Dec 27, 2011 at 2:48 AM, Paul Phillips <paulp@improving.org> wrote:
I hacked on the parser just a very little bit to make this possible.  The changes are little more than stopping it from gratuitously failing on back-to-back bracketed lists.

import scala.collection.{ mutable, immutable, generic } import generic.CanBuildFrom
object Partial {  type KnownContainer[CC[K, V] <: collection.Map[K, V]] = {    def values[V] : KnownValues[CC, V]    def apply[K] : KnownKeys[CC, K]   }  type KnownKeys[CC[K, V] <: collection.Map[K, V], K] = {    def apply[V](implicit cbf: CanBuildFrom[_, (K, V), CC[K, V]]): CC[K, V]  }  type KnownValues[CC[K, V] <: collection.Map[K, V], V] = {     def apply[K](implicit cbf: CanBuildFrom[_, (K, V), CC[K, V]]): CC[K, V]  }
  def apply[CC[K, V] <: collection.Map[K, V]] : KnownContainer[CC] = new {    def values[V] : KnownValues[CC, V] = new {       def apply[K](implicit cbf: CanBuildFrom[_, (K, V), CC[K, V]]) = cbf().result    }    def apply[K] = new {      def apply[V](implicit cbf: CanBuildFrom[_, (K, V), CC[K, V]]) = cbf().result     }  }}
scala> val m = Partial[immutable.TreeMap]m: Partial.KnownContainer[scala.collection.immutable.TreeMap] = Partial$$anon$1@20beaacc
scala> val m1 = m[String]m1: Partial.KnownKeys[scala.collection.immutable.TreeMap,String] = Partial$$anon$1$$anon$2@13979163
scala> val m2 = m[Int][Int] m2: scala.collection.immutable.TreeMap[Int,Int] = Map()
scala> val mutableBippy = Partial[mutable.HashMap][String][Int]mutableBippy: scala.collection.mutable.HashMap[String,Int] = Map()
scala> mutableBippy("abc") = 55
scala> println(mutableBippy)Map(abc -> 55)
scala> val immutableBippy = Partial[immutable.HashMap].values[Int] immutableBippy: Partial.KnownValues[scala.collection.immutable.HashMap,Int] = Partial$$anon$1$$anon$3@6d02f1ee
scala> def make[T](xs: T*) = immutableBippy[T] ++ xs.zipWithIndex make: [T](xs: T*)scala.collection.immutable.Map[T,Int]
scala> make('a' to 'z': _*)res7: scala.collection.immutable.Map[Char,Int] = Map(e -> 4, s -> 18, x -> 23, n -> 13, j -> 9, y -> 24, t -> 19, u -> 20, f -> 5, a -> 0, m -> 12, i -> 8, v -> 21, q -> 16, b -> 1, g -> 6, l -> 11, p -> 15, c -> 2, h -> 7, r -> 17, w -> 22, k -> 10, o -> 14, z -> 25, d -> 3)

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: type currying

On Tue, Dec 27, 2011 at 2:14 AM, Miles Sabin <miles@milessabin.com> wrote:
Is the patch available anywhere?

No, but it's small enough to send by carrier pigeon.

diff --git c/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala i/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index e27d5cacda..00ac3976a9 100644--- c/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala+++ i/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala@@ -1534,9 +1534,10 @@ self =>            val t1 = stripParens(t)           t1 match {             case Ident(_) | Select(_, _) =>-              val tapp = atPos(t1.pos.startOrPoint, in.offset) {-                TypeApply(t1, exprTypeArgs()) -              }+              var tapp: Tree = t1+              while (in.token == LBRACKET)+                tapp = atPos(tapp.pos.startOrPoint, in.offset)(TypeApply(tapp, exprTypeArgs())) +               simpleExprRest(tapp, true)             case _ =>               t1
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: type currying
Also, I've convinced myself that this is a bugfix, so unless martin speaks up otherwise I'll commit it.
   A type application e[T[1],…,T[n]] ...

   If the function part e is of some value type, the type application is    taken to be equivalent to  e.apply[T[1],…, T[n]], i.e. the application of   an apply method defined by e.
Oh my god it's so nice to finally be able to paste from the spec.  Thanks again Erik.
Julien Richard-Foy
Joined: 2011-04-24,
User offline. Last seen 42 years 45 weeks ago.
Re: type currying
Maybe type currying should be explicit, as partial function application is, in order to avoid people to write code they did not intended to write?
E.g. something like the following:
val m1 = m[String] _
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: type currying


On Tue, Dec 27, 2011 at 7:33 AM, Julien Richard-Foy <julien.rf@gmail.com> wrote:
Maybe type currying should be explicit, as partial function application is, in order to avoid people to write code they did not intended to write?

This is really a stretch.  First, what else would they be intending? Secondly, partial function application works exactly the same in the analogous context.
scala>   object Foo {     |     def apply(x: Int) = new {     |       def apply(y: Int) = x + y     |     }     |   }defined module Foo
scala> Foo(5)(10)res0: Int = 15
scala> Foo(5)res1: Object{def apply(y: Int): Int} = Foo$$anon$1@4be0ef94

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