summaryrefslogtreecommitdiffstats
path: root/Scala/forcomp/src/main/scala/common
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-05-11 22:30:17 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-10 18:03:24 +0100
commitf693c7f267e2668be5626413d4eee8600630b6be (patch)
tree10ede8ee23560a228175074a59bab72f9e52a143 /Scala/forcomp/src/main/scala/common
parent5c5087a4a7f8342946ffe0ccbf1a244d7d4790dc (diff)
downloadcoursera-f693c7f267e2668be5626413d4eee8600630b6be.zip
coursera-f693c7f267e2668be5626413d4eee8600630b6be.tar.gz
Scala : Scala : add forcomp assignment
Diffstat (limited to 'Scala/forcomp/src/main/scala/common')
-rw-r--r--Scala/forcomp/src/main/scala/common/package.scala40
1 files changed, 40 insertions, 0 deletions
diff --git a/Scala/forcomp/src/main/scala/common/package.scala b/Scala/forcomp/src/main/scala/common/package.scala
new file mode 100644
index 0000000..f1c74c3
--- /dev/null
+++ b/Scala/forcomp/src/main/scala/common/package.scala
@@ -0,0 +1,40 @@
+import java.io.File
+
+package object common {
+
+ /** An alias for the `Nothing` type.
+ * Denotes that the type should be filled in.
+ */
+ type ??? = Nothing
+
+ /** An alias for the `Any` type.
+ * Denotes that the type should be filled in.
+ */
+ type *** = Any
+
+
+ /**
+ * Get a child of a file. For example,
+ *
+ * subFile(homeDir, "b", "c")
+ *
+ * corresponds to ~/b/c
+ */
+ def subFile(file: File, children: String*) = {
+ children.foldLeft(file)((file, child) => new File(file, child))
+ }
+
+ /**
+ * Get a resource from the `src/main/resources` directory. Eclipse does not copy
+ * resources to the output directory, then the class loader cannot find them.
+ */
+ def resourceAsStreamFromSrc(resourcePath: List[String]): Option[java.io.InputStream] = {
+ val classesDir = new File(getClass.getResource(".").toURI)
+ val projectDir = classesDir.getParentFile.getParentFile.getParentFile.getParentFile
+ val resourceFile = subFile(projectDir, ("src" :: "main" :: "resources" :: resourcePath): _*)
+ if (resourceFile.exists)
+ Some(new java.io.FileInputStream(resourceFile))
+ else
+ None
+ }
+}