- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
type currying
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)
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)










Re: type currying
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 [at] improving [dot] org> wrote:
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
Re: type currying
On Tue, Dec 27, 2011 at 2:14 AM, Miles Sabin <miles [at] milessabin [dot] com> wrote:
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
Re: type currying
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.
Re: type currying
E.g. something like the following:
val m1 = m[String] _
Re: type currying
On Tue, Dec 27, 2011 at 7:33 AM, Julien Richard-Foy <julien [dot] rf [at] gmail [dot] com> wrote:
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