Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Sedro
Dec 31, 2008

KernelSlanders posted:

I was trying to see today if there's a subtle difference between ++ and ::: on List today. Google doesn't like ::: as a search query it seems.

That said, being able to write val list = item1 :: item2 :: item3 :: subList1 ++ subList2 is pretty convenient.
You can just look at the definition of List.++. List overrides ++ to call ::: if both sides are lists. List also overrides +: to call ::

These are equivalent
code:
a ::: b
b.:::(a)
which is important because it's faster to prepend to a list.

Adbot
ADBOT LOVES YOU

Sedro
Dec 31, 2008
Here's an example of slick's "lifted embedding", which basically lets you use SQL as a monad

Sedro
Dec 31, 2008
JDBC isn't async so slick can't be async (of course you can still wrap the code in Future { ... }). They're working on something for 3.0 which involves replacing JDBC.

Sedro
Dec 31, 2008

b0lt posted:

scalaz owns
code:
sealed abstract class ==>>[A, B] {
    def \\(other: ==>>[A, B])(implicit o: Order[A]): ==>>[A, B]
}
I don't use scalaz and I expected a horror, but it's actually straightforward and intuitive.

The symbolic class name is nice because you can use use infix e.g. Int ==>> String. Or if you prefer: type KeyValuePair[A, B] = A ==>> B

The symbolic method is just an alias for a named method:
code:
sealed abstract class ==>>[A, B] {

  def \\(other: A ==>> B)(implicit o: Order[A]): A ==>> B =
    difference(other)

  def difference(other: A ==>> B)(implicit o: Order[A]): A ==>> B = {
    ...
  }

}

Sedro
Dec 31, 2008

Ruzihm posted:

Argh. I am trying to do a "manual install" of sbt, but it's still trying to reach out to the internet. What gives?
SBT will check for updates to things, especially if you depend on SNAPSHOTs.

You could try offline mode

Sedro
Dec 31, 2008
Play does support slick.

https://www.playframework.com/documentation/2.4.x/PlaySlick

There's also Anorm which is a lightweight Slick.

Sedro
Dec 31, 2008
You can tell slick how to map types when you define your table. You can also extend the Driver and put this stuff in there.
code:
import driver.api._
implicit val dateTimeMapping = MappedColumnType.base[DateTime, Time](dt => new Time(dt.getMillis), t => new DateTime(t.getTime))

Sedro
Dec 31, 2008
Sure, just change the default value
code:
val m = Map[Int, Int]().withDefault(_ => throw new NoSuchElementException("key not found: wut"))
val m = Map[Int, Int]().withDefault(_ => Random.nextInt())

Sedro
Dec 31, 2008
That is odd. I guess I'd prefer if mapValues was eager and Map.view could be used for laziness

Sedro
Dec 31, 2008
Foo() is short for Foo.apply(), but there's no function with that signature. There's a Foo.apply and a Foo.apply(Seq[Int]).

Sedro
Dec 31, 2008
The equivalent to IEnumerable<T> is probably TraversableOnce[T]. You generally don't deal with TraversableOnce though because collections APIs return the same type of collection (in C# you always get IEnumerable). If you want the lazy LINQ behavior from C# you can call .view on your collection.

Seq[T] has an ordering and a length, and it might be mutable (unless you use immutable.Seq[T]). It's closer to IList<T>.

Sedro fucked around with this message at 17:52 on Nov 4, 2015

Sedro
Dec 31, 2008
Plain old Java reflection will work: x.getClass.getSuperclass

Sedro
Dec 31, 2008
Right, you would have to repeat all the parameters. You baked in all the defaults as soon as you partially applied Foo. Only methods (Foo) can have default arguments; functions (myFoo) can't.

I think you could write a macro.

Sedro
Dec 31, 2008
Are you storing the database in memory? Try using H2 in persistent mode, or use sqlite.

Sedro
Dec 31, 2008
You can do sbt x/run -jvm-debug 5005 then attach your remote debugger from Intellij.

Sedro
Dec 31, 2008
You can use @ to get the whole object for the thing matching the pattern-- the "have your cake and eat it too operator"
code:
def doStuff(light: Option[Light]) = light match {
  case Some(rl @ RedLight(t)) => doSomething(rl)
  case Some(gl @ GreenLight(t)) if t > 0 => doSomethingElse(gl)
  case _ => thatOtherThing
}

Sedro
Dec 31, 2008
Do you need persistence? Reliable delivery? Throttling/backpressure in case of slow consumers? If you don't need the features of a message queue product, don't use one. You can always add it later.

There's an example project using akka's distributed pub/sub and sources on github.

Sedro
Dec 31, 2008
I would avoid infix notation in general and use it only for simple binary expressions where it improves readability:
code:
val myTuple = "foo".->("bar")
val myTuple = "foo" -> "bar" // better

if (myList.contains(3)) { ... }
if (myList contains 3) { ... } // better

if (a.==(b)) { ... }
if (a == b) { ... } // better

if (!myList.contains(3)) { ... }
if (!(myList contains 3)) { ... } // better?
I would also go against the style guide in that map/filter example. I find that infix notation makes the code harder to edit. If you want to break the expression onto multiple lines you need to surround it with parenthesis:
code:
val result = (things
  filter (_.someCondition) 
  map (_.toOtherThing))

val filename: String = (getClass
  getResource "/sample.html"
  getFile)

Adbot
ADBOT LOVES YOU

Sedro
Dec 31, 2008

Good Will Hrunting posted:

So I have a list of tuples that looks like (a, (b, x)). I want as a result a map of a to (b, x) where x is the greatest x for that given a and b combo. Basically, if the x I'm examining is greater than the greatest x so far for that a as well as that b, update the result map with the a, (b, x) combo. I have legitimately no idea how to approach this. It seems like I should be able to do this with foldLeft, am I crazy?

foldLeft is a good approach. You can use a map as an accumulator, keyed by (a, b) tuples. You would start with an empty map, and compare the value of X for each element in your list. If X is greater, add it to the map, otherwise continue.

code:
val list = List[(A, (B, X))] = ???

list.foldLeft(Map.empty[(A, B), X]) { case (accum, (a, (b, x))) =>
  if (accum.get((a, b)).forall(_ < x)) {
    // found a greater value for X
    accum + ((a, b) -> x)
  } else {
    accum
  }
}
This will get you a map of (A, B) -> X, which is not quite what you asked for. Since you want a map of A -> (B, X) you will need to decide how to resolve a conflict where two (A, B) pairs have the same A and a different B.

  • Locked thread