Scala: Difference between revisions

From Bitpost wiki
No edit summary
No edit summary
Line 11: Line 11:
=== PROJECT CREATION ===
=== PROJECT CREATION ===


THE KEY HERE IS TO LET THE TOOL CREATE THE FOLDER STRUCTURE, THEN ADD A .scala MODULE:
# I FOUND IT IMPOSSIBLE to take an SBT project and get IntelliJ configured for it.  It only worked when I let IntelliJ set up the project.
# THE KEY HERE IS TO LET THE TOOL CREATE THE FOLDER STRUCTURE, THEN ADD A .scala MODULE:
  Hardcoded layout
  Hardcoded layout
  ├── project
  ├── project
Line 24: Line 25:
  │      └── scala          <===== MyModule.scala goes here
  │      └── scala          <===== MyModule.scala goes here
  └── target
  └── target
==== SBT ====
* Make a folder for your project, eg MyProject
* Create this path, it is HARDCODED and expected:
src/main/scala
* Create a first module, eg:
MyProject/src/main/scala/Scrap.scala
---
object Scrap extends App {
  println("Hello Scrap.  And away we go!")
}
* From a command line, run sbt, should compile and run:
MyProject$ sbt run
* If that doesn't work, kill yourself


==== INTELLIJ ====
==== INTELLIJ ====
Line 47: Line 34:
* It should have created that nasty smelly code layout (see above).  Add a module, eg:
* It should have created that nasty smelly code layout (see above).  Add a module, eg:
  MyProject/src/main/scala/Scrap.scala
  MyProject/src/main/scala/Scrap.scala
---
package com.bitpost.scala.scrap
object Scrap extends App {
  println("Hello Scrap.  And away we go!")
}
* Then magically after it settles down you should be able to Build->Module, and Run->{modulename}
* Then magically after it settles down you should be able to Build->Module, and Run->{modulename}
==== SBT ====
* Make a folder for your project, eg MyProject
* Create this path, it is HARDCODED and expected:
src/main/scala
* Create a first module, see above
* From a command line, run sbt, should compile and run:
MyProject$ sbt run
* Once you open the MyProject folder in IDEA, you need to select Project, choose File->Project Structure->Libraries, and add Scala SDK.
* NOPE, not enough to get it working.  Kill yourself.


=== CONTAINERS ===
=== CONTAINERS ===

Revision as of 18:43, 25 October 2016

Installation

Start with Java. Then sbt:

echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823
sudo apt-get update
sudo apt-get install sbt

Next, set up ~/.sbt/repositories with repos used by your team.

PROJECT CREATION

  1. I FOUND IT IMPOSSIBLE to take an SBT project and get IntelliJ configured for it. It only worked when I let IntelliJ set up the project.
  2. THE KEY HERE IS TO LET THE TOOL CREATE THE FOLDER STRUCTURE, THEN ADD A .scala MODULE:
Hardcoded layout
├── project
│   └── target
│       └── config-classes
├── src
│   ├── main
│   │   ├── resources
│   │   └── scala
│   └── test
│       ├── resources
│       └── scala           <===== MyModule.scala goes here
└── target

INTELLIJ

IntelliJ wins Worst-IDE by a long factor. Who needs to type when the IDE can churn out unusable code indexes in the background for you that peg the CPU for hours? Don't worry, once it gets done it will write the code for you! Quit complaining and buy the latest Macbook every 3 months to keep up.

  • From IntelliJ, File->New->Project... (NOT FROM EXISTING because INTELLIJ SUCKS MY DICK)... ->Scala->SBT
  • Use your folder for location, name, and module (under More Settings), eg MyProject
  • It should have created that nasty smelly code layout (see above). Add a module, eg:
MyProject/src/main/scala/Scrap.scala
---
package com.bitpost.scala.scrap

object Scrap extends App {
  println("Hello Scrap.  And away we go!")
}
  • Then magically after it settles down you should be able to Build->Module, and Run->{modulename}

SBT

  • Make a folder for your project, eg MyProject
  • Create this path, it is HARDCODED and expected:
src/main/scala
  • Create a first module, see above
  • From a command line, run sbt, should compile and run:
MyProject$ sbt run
  • Once you open the MyProject folder in IDEA, you need to select Project, choose File->Project Structure->Libraries, and add Scala SDK.
  • NOPE, not enough to get it working. Kill yourself.

CONTAINERS

Here are three ways to create a container from information in another.

Immutable map (best):

 val andClause = unverifiedTileSet.map { un =>
   and(
     Array(
       BSONDocument("x" -> BSONInteger(un.x)),
       BSONDocument("y" -> BSONInteger(un.y))
     )
   )
 }
 val orClause = BSONDocument("$or" -> andClause)
 val pUp = Promise[Unit]
 val fUp = uDao.col.update(orClause, updateSet, upsert = true, multi = true)
 fUp.onComplete {
   case Success(res) => pUp.success()
   case Failure(t) => pUp.failure(t)
 }
 pUp.future

For with yeild, good for multiple steps:

	/*
		// //2) way with for <- yield
 		// val orClause = for(
		// 	un <- unverifiedTileSet; 
		// 	andClause <- { 
		// 	 	and(
		//        		Array(
		//        			BSONDocument("x" -> BSONInteger(un.x)),
		//        			BSONDocument("y" -> BSONInteger(un.y))
		//      			)
		// 		)
		// 	}
		// )yield{
		//  		BSONDocument("$or" -> andClause)
		// }

		//2) way with for <- yield
		val orClause2 = for(
			andClause <- {
				unverifiedTileSet.map{un =>
					and(
						Array(
							BSONDocument("x" -> BSONInteger(un.x)),
							BSONDocument("y" -> BSONInteger(un.y))
						)
					)
				}
			}
		)yield{
			BSONDocument("$or" -> andClause)
		}

Third way, mutable oldskool:

		// method 3, oldskool for with mutable, not recommended
		import scala.collection.mutable.ArrayBuffer
		var andClause = ArrayBuffer.empty[BSONDocument]
		for (un <- unverifiedTileSet) {
      andClause += and(
	        Array(
            BSONDocument("x" -> BSONInteger(un.x)),
            BSONDocument("y" -> BSONInteger(un.y))
          )
	      )
    }
    val orClause = BSONDocument("$or" -> andClause)
	*/

CAUSAM setup

For Causam, set up an AWS VPN to get on the stash network, then add these:

[repositories]
local
artifactory-releases: http://artifactory.energynet.link:8081/artifactory/libs-release/
artifactory-snapshots: http://artifactory.energynet.link:8081/artifactory/libs-snapshot/
ivy-releases: http://artifactory.energynet.link:8081/artifactory/ivy-releases, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
anormcypher: http://artifactory.energynet.link:8081/artifactory/anormcypher

Then if you have a project already, use sbt and let that pull down all the dependencies:

cd ~/development/causam/git/causam-north-poller-scala
sbt compile