Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

HArray

git-svn-id: http://svn.assembla.com/svn/metascala@12 e0e46c49-be69-4f5a-ad62-21024a331aea
  • Loading branch information...
commit a504ced203d422699c69fa29411c4b09c3f7f53a 1 parent eeb3af8
mayhem authored
View
36 MetaScala.iml
@@ -1,18 +1,18 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<module relativePaths="true" type="JAVA_MODULE" version="4">
- <component name="FacetManager">
- <facet type="Scala" name="Scala">
- <configuration />
- </facet>
- </component>
- <component name="NewModuleRootManager" inherit-compiler-output="true">
- <exclude-output />
- <content url="file://$MODULE_DIR$">
- <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
- </content>
- <orderEntry type="library" name="scala-2.7.2.r16050-b20080907010137" level="application" />
- <orderEntry type="inheritedJdk" />
- <orderEntry type="sourceFolder" forTests="false" />
- </component>
-</module>
-
+<?xml version="1.0" encoding="UTF-8"?>
+<module relativePaths="true" type="JAVA_MODULE" version="4">
+ <component name="FacetManager">
+ <facet type="Scala" name="Scala">
+ <configuration />
+ </facet>
+ </component>
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
+ <exclude-output />
+ <content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+ </content>
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntry type="library" name="Scala 2.8" level="application" />
+ </component>
+</module>
+
View
20 src/metascala/HArrays.scala
@@ -2,12 +2,12 @@ package metascala
object HArrays {
- import Utils._
import Nats._
import TLists._
import HCollections._
final case class HArray[L <: TList](value : Array[Any]) extends Tuple[L] {
+ type List = L
type This[L <: TList] = HArray[L]
def ::[T](v : T) = {
@@ -52,13 +52,17 @@ object HArrays {
HArray[L#Insert[N, E]](a)
}
- def replaceSameType[N <: Nat, E](n : N, elem : E) = null
-/* def getByType[N <: Nat, E](implicit fn : GetByType[N, E]) : E = fn(this) */
+// def replaceSameType[N <: Nat, E](n : N, elem : E) = null
+// def getByType[N <: Nat, E](implicit fn : GetByType[N, E]) : E = fn(this)
}
- def arrayTuple[T1](v1 : T1) = HArray[T1 :: TNil](Array(v1))
- def arrayTuple[T1, T2](v1 : T1, v2 : T2) = HArray[T1 :: T2 :: TNil](Array(v1, v2))
- def arrayTuple[T1, T2, T3](v1 : T1, v2 : T2, v3 : T3) = HArray[T1 :: T2 :: T3 :: TNil](Array(v1, v2, v3))
- def arrayTuple[T1, T2, T3, T4](v1 : T1, v2 : T2, v3 : T3, v4 : T4) = HArray[T1 :: T2 :: T3 :: T4 :: TNil](Array(v1, v2, v3, v4))
- def arrayTuple[T1, T2, T3, T4, T5](v1 : T1, v2 : T2, v3 : T3, v4 : T4, v5 : T5) = HArray[T1 :: T2 :: T3 :: T4 :: T5 :: TNil](Array(v1, v2, v3, v4, v5))
+ val HArrayNil = HArray[TNil](new Array[Any](0))
+
+ private def createArray(elems : Any*) = elems toArray
+
+ def arrayTuple[T1](v1 : T1) = HArray[T1 :: TNil](createArray(v1))
+ def arrayTuple[T1, T2](v1 : T1, v2 : T2) = HArray[T1 :: T2 :: TNil](createArray(v1, v2))
+ def arrayTuple[T1, T2, T3](v1 : T1, v2 : T2, v3 : T3) = HArray[T1 :: T2 :: T3 :: TNil](createArray(v1, v2, v3))
+ def arrayTuple[T1, T2, T3, T4](v1 : T1, v2 : T2, v3 : T3, v4 : T4) = HArray[T1 :: T2 :: T3 :: T4 :: TNil](createArray(v1, v2, v3, v4))
+ def arrayTuple[T1, T2, T3, T4, T5](v1 : T1, v2 : T2, v3 : T3, v4 : T4, v5 : T5) = HArray[T1 :: T2 :: T3 :: T4 :: T5 :: TNil](createArray(v1, v2, v3, v4, v5))
}
View
2  src/metascala/HCollections.scala
@@ -13,7 +13,7 @@ object HCollections {
def reverse : This[L#Reverse]
def removeNth[N <: Nat](n : N) : This[L#RemoveNth[N]]
def insert[N <: Nat, E](n : N, elem : E): This[L#Insert[N, E]]
- def replaceSameType[N <: Nat, E](n : N, elem : E) : This[L]
+// def replaceSameType[N <: Nat, E](n : N, elem : E) : This[L]
}
}
View
12 src/metascala/TLists.scala
@@ -5,7 +5,12 @@ object TLists {
import Nats._
import Utils._
- trait TList {
+ trait TListVisitor extends TypeVisitor {
+ type VisitNil <: ResultType
+ type VisitCons[H, T <: TList] <: ResultType
+ }
+
+ sealed trait TList extends Visitable[TListVisitor] {
type Head
type Tail <: TList
type Append[L <: TList] <: TList
@@ -14,9 +19,11 @@ object TLists {
type Nth[N <: Nat]
type RemoveNth[N <: Nat] <: TList
type Insert[N <: Nat, E] <: TList
+ type Reverse = ReverseAppend[TNil]
}
final class TNil extends TList {
+ type Accept[V <: TListVisitor] = V#VisitNil
type Head = Nothing
type Tail = TNil
type Append[L <: TList] = L
@@ -35,6 +42,7 @@ object TLists {
}
final class TCons[H, T <: TList] extends TList {
+ type Accept[V <: TListVisitor] = V#VisitCons[H, T]
type This = TCons[H, T]
type Head = H
type Tail = T
@@ -89,7 +97,7 @@ object TLists {
implicit def isSubSetFn[T <: TList] = IsSubSetFn[TNil, T]()
- implicit def isSubSetFn2[H, T, T2 <: TList](implicit fn1 : ContainsFn[H, T2], fn2 : IsSubSetFn[T, T2]) = IsSubSetFn[H :: T, T2]()
+ implicit def isSubSetFn2[H, T <: TList, T2 <: TList](implicit fn1 : ContainsFn[H, T2], fn2 : IsSubSetFn[T, T2]) = IsSubSetFn[H :: T, T2]()
case class IsSubSetFn[H, T <: TList]()
View
59 src/metascala/test/HArrayTest.scala
@@ -1,10 +1,68 @@
package metascala.test
+import metascala.Utils.Equal
+
object HArrayTest {
import metascala.TLists._
import metascala.Nats._
import metascala.HArrays._
+ // Cons
+ val l1 : HArray[Int :: Boolean :: TNil] = 10 :: true :: HArrayNil
+ val l2 : HArray[Double :: String :: TNil] = 10.1 :: "hello" :: HArrayNil
+ val l3 : HArray[(Int, Boolean) :: Int :: Boolean :: TNil] = (10, false) :: l1
+
+ object Append {
+ val a1 : HArray[Int :: TNil] = (10 :: HArrayNil) ::: HArrayNil
+ val a2 : HArray[Int :: TNil] = HArrayNil ::: (10 :: HArrayNil)
+ val a3 : HArray[Boolean :: Int :: TNil] = (true :: HArrayNil) ::: (10 :: HArrayNil)
+ val a4 : HArray[(Int, Boolean) :: Int :: Boolean :: Double :: String :: Int :: Boolean :: TNil] = l3 ::: l2 ::: l1
+ }
+
+ object Nth {
+ val n1 = l1(_0) * 3
+ val n2 = l1(_1) && false
+ val n3 = l3(_2) && true
+ val n4 = l2(_1).length
+ val n5 = l3(_0)._1 * 3
+ }
+
+ object Reverse {
+ val r1 = HArrayNil.reverse
+ val r2 : HArray[Int :: TNil] = (10 :: HArrayNil).reverse
+ val r3 : HArray[Boolean :: Int :: TNil] = l1.reverse
+ val r4 : HArray[Boolean :: Int :: (Int, Boolean) :: TNil] = l3.reverse
+ }
+
+ object Length {
+ type L1 = Equal[l1.List#Length, _2]
+ type L2 = Equal[l3.List#Length, _3]
+ }
+
+ object Remove {
+ val r1 : HArray[Boolean :: TNil] = l1.removeNth(_0)
+ val r2 : HArray[Int :: TNil] = l1.removeNth(_1)
+ val r3 : HArray[TNil] = l1.removeNth(_0).removeNth(_0)
+ val r4 : HArray[(Int, Boolean) :: Int :: TNil] = l3.removeNth(_2)
+ }
+
+ object Insert {
+ val i1 : HArray[String :: Int :: Boolean :: TNil] = l1.insert(_0, "Hello")
+ val i2 : HArray[Int :: String :: Boolean :: TNil] = l1.insert(_1, "Hello")
+ val i3 : HArray[Int :: Boolean :: String :: TNil] = l1.insert(_2, "Hello")
+ val i4 : HArray[Int :: String :: TNil] = l1.removeNth(_1).insert(_1, "Hello")
+ }
+
+ object GetByType {
+// val g1 = l1.getByType[_0, Int]
+// val g2 = l1.getByType[_0, Boolean]
+ }
+
+ object ReplaceByType {
+// val r1 = l1.replaceByType(_0, 1)
+// val r2 = l1.replaceByType(_0, false)
+ }
+
val t : HArray[Int :: Boolean :: String :: TNil] = arrayTuple(10, true, "Hello")
val t2 : HArray[Double :: Int :: Boolean :: String :: TNil] = 10.1 :: t
val t3 : HArray[Double :: Int :: Boolean :: String :: Int :: Boolean :: String :: TNil] = t2 ::: t
@@ -14,6 +72,7 @@ object HArrayTest {
val t7 : HArray[Int :: String :: TNil] = t5 ::: t6
val t8 : Boolean = t(_1)
val t9 : HArray[String :: Boolean :: Int :: TNil] = t.reverse
+ val t9b : HArray[String :: Boolean :: Boolean :: Int :: TNil] = t9.insert(_2, false)
val t10 : HArray[Double :: Int :: Boolean :: String :: Boolean :: String :: TNil] = t3.removeNth(_4)
val t11 : HArray[Double :: Int :: Boolean :: Boolean :: String :: Boolean :: String :: TNil] = t10.insert(_3, false)
}
View
3  src/metascala/test/NatsTest.scala
@@ -3,7 +3,6 @@ package metascala.test
object NatsTest {
import metascala.Nats._
import metascala.Utils._
- import metascala.Booleans._
type T1 = Equal[_0, _0 + _0]
type T2 = Equal[_1, _1 + _0]
@@ -11,7 +10,7 @@ object NatsTest {
type T4 = Equal[_2, _1 + _1]
type T5 = Equal[_7, _3 + _4]
- trait Fib {
+ trait Fib extends NatVisitor {
type ResultType = Nat
type Visit0 = _0
type VisitSucc[Pre <: Nat] = Pre#Is0#If2[Nat, _1, Pre#Accept[Fib] + Pre#Pre#Accept[Fib]]
View
4 src/metascala/test/OneOfsTest.scala
@@ -6,9 +6,9 @@ object OneOfsTest {
import metascala.Utils._
val ts : OneOf[Int :: Float :: TNil] = 10
- val ts2 : OneOf[Float :: Int :: Boolean :: TNil] = ts
+// val ts2 : OneOf[Float :: Int :: Boolean :: TNil] = ts
- val l : Int :: TNil = removeFirst(1f, value[Int :: Float :: TNil])
+// val l : Int :: TNil = removeFirst(1f, value[Int :: Float :: TNil])
// val x = doMatch(ts) doIf ((s : Int) => 1) apply()
}
View
2  src/metascala/test/TListsTest.scala
@@ -10,4 +10,6 @@ object TListsTest {
type T3 = T1 ::: T2
type T4 = Equal[T3, Int :: Boolean :: Float :: (Int, Double) :: TNil]
type T5 = Equal[T3#RemoveNth[_2], Int :: Boolean :: (Int, Double) :: TNil]
+ type T6 = Equal[T3#RemoveNth[_1], Int :: Float :: (Int, Double) :: TNil]
+ type t7 = Equal[T3#Reverse, (Int, Double) :: Float :: Boolean :: Int :: TNil]
}
Please sign in to comment.
Something went wrong with that request. Please try again.