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.
 
  • Post
  • Reply
crazypenguin
Mar 9, 2005
nothing witty here, move along

motedek posted:

is there an ide to write all the scala boilerplate? i'm curious about it but gently caress if i'm going to write stuff like this:

as much as I hate scala and implicits, a lot of that boilerplate is stuff written in libraries so you don't have to write boilerplate to use them.

Adbot
ADBOT LOVES YOU

Damiya
Jul 3, 2012

motedek posted:

is there an ide to write all the scala boilerplate? i'm curious about it but gently caress if i'm going to write stuff like this:

Scala doesn't really have a lot of boilerplate; that complex signature actually usually ends up getting expressed as:

code:
map { Foo =>
  bar 
} 
its a bit intimidating but once you learn to read the generic lingo and put it into context it's pretty easy to work with.

For example in a project I'm working on I have this:

code:
class AuthenticatedRequest[A](val userSession: UserSession, request: Request[A]) extends WrappedRequest[A](request)

object Secured extends ActionBuilder[AuthenticatedRequest] with Results {
  def invokeBlock[A](request: Request[A], block: AuthenticatedRequest[A] => Future[SimpleResult]): Future[SimpleResult] = {
    request.headers.get("X-Auth-Token") match {
      case Some(authToken) =>
        Cache.getAs[UserSession](authToken) match {
          case Some(userSession) => block(new AuthenticatedRequest(userSession, request))
          case None => resolve(Forbidden("You must log in to access that resource."))
        }
      case None => resolve(Forbidden("You must log in to access that resource."))
    }
  }
}
Which is like "holy cow look at how crazy complex that is"

It looks intimidating due to some of the generic types but ultimately it's just a function that takes two parameters:

A Request and a function (whose signature is output:Future[SimpleResult] = f(AuthenticatedRequest)).

So it's actually just looking for a function that can ingest an AuthenticatedRequest and return a future, such as:

code:
def destroy(): Action[JsValue] = Secured(parse.json) { request =>
    Cache.remove(request.userSession.authToken)
    Logger.info(s"${request.userSession.user.username} logged out.")
    Ok(Json.obj(
      "result" -> "Session destroyed"
    ))
  }
Edit: Also to be clear the code I wrote is actually layered on top of a library implementation that actually cranks the gears that translate something like

Secured(parse.json) { request => ... }

into the actual invocation of the function invokeBlock above, but that's beyond the scope of this post.

Damiya fucked around with this message at 20:23 on Feb 11, 2014

Condiv
May 7, 2008

Sorry to undo the effort of paying a domestic abuser $10 to own this poster, but I am going to lose my dang mind if I keep seeing multiple posters who appear to be Baloogan.

With love,
a mod


motedek posted:

is there an ide to write all the scala boilerplate? i'm curious about it but gently caress if i'm going to write stuff like this:

You don't usually have to write defs like that for map. That one is the way it is so map can be defined generically in some real low level traits (like GenTraversableOnce) and still build and return the exact collection type the function was called from without reflection.

On a regular collection that wasn't part of the standard libs, I would write something like this for map:

code:
  trait Datastructure[A] {
    def map[B](fn: A => B): Datastructure[B]
  }

Malcolm XML
Aug 8, 2009

I always knew it would end like this.
scala: http://www.scala-graph.org/guides/core-initializing.html

WkLkDiHyperEdge ~%#+#> key-weighted key-labeled directed hyperedge

welp

prefect
Sep 11, 2001

No one, Woodhouse.
No one.




Dead Man’s Band

Malcolm XML posted:

scala: http://www.scala-graph.org/guides/core-initializing.html

WkLkDiHyperEdge ~%#+#> key-weighted key-labeled directed hyperedge

welp

i'm sure it's not as frightening if you actually know scala, but this was a bit of a shock :)

FamDav
Mar 29, 2008
takeaway: scala is all of the problems of c++ with none of the bennies

Deus Rex
Mar 5, 2005

now you're complaining about the same thing you defended in Haskell. these operators are even 'composed' like the Lens ones, like "%" seems to mean "weighted"; "#" is "key-<character before it>", "+" is "labeled". what's the difference?

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Deus Rex posted:

now you're complaining about the same thing you defended in Haskell. these operators are even 'composed' like the Lens ones, like "%" seems to mean "weighted"; "#" is "key-<character before it>", "+" is "labeled". what's the difference?

i'm not complaining it's just that scala has been touted as the common mans functional language and well

if %%~ was bad ...

FamDav
Mar 29, 2008

Deus Rex posted:

now you're complaining about the same thing you defended in Haskell. these operators are even 'composed' like the Lens ones, like "%" seems to mean "weighted"; "#" is "key-<character before it>", "+" is "labeled". what's the difference?

oh i thought these were types

like yeah let me iterate out all these fookin types mate real compositional in this bitch

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

motedek posted:

is there an ide to write all the scala boilerplate? i'm curious about it but gently caress if i'm going to write stuff like this:

yeah i wasnt posting that to imply you need to usually write stuff like that in scala, im just making fun of its type system for being strictly worse than haskell's. for reference, the type signature of map in haskell is:
code:
map :: (a -> b) -> [a] -> [b]

FamDav
Mar 29, 2008

MeramJert posted:

yeah i wasnt posting that to imply you need to usually write stuff like that in scala, im just making fun of its type system for being strictly worse than haskell's. for reference, the type signature of map in haskell is:
code:
map :: (a -> b) -> [a] -> [b]

only bitches uses map

code:
fmap :: (Functor f) => (a -> b) -> f a -> f b

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

you struck me as more of a <$> guy

MononcQc
May 29, 2007

-spec map(fun((A) -> B), [A]) -> [B].

:madmax:

FamDav
Mar 29, 2008

MeramJert posted:

you struck me as more of a <$> guy

FamDav fucked around with this message at 06:44 on Feb 12, 2014

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

now do fmap

motedek
Oct 9, 2012

MeramJert posted:

yeah i wasnt posting that to imply you need to usually write stuff like that in scala, im just making fun of its type system for being strictly worse than haskell's. for reference, the type signature of map in haskell is:
code:
map :: (a -> b) -> [a] -> [b]

cool thanks all. the scala examples tend to look awful but then so does most unfamiliar code.

Damiya
Jul 3, 2012
That wasn't even a particularly egregious Scala example. step up your game.

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

sorry i dont know enough scala, would you please provide worse examples of common functions?

MeruFM
Jul 27, 2010
most code look pretty terrible with 1 letter variables or if there's more than 4 variables in a single line.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

yo

tef
May 30, 2004

-> some l-system crap ->

get this as an av with "XML is like violence: if it doesn't solve your problem, you're not using enough of it."

gonadic io
Feb 16, 2011

>>=
http://www.java8.org/

code:
//Effectively final variables can be referenced in lambdas
// s is effectively final (not changed anywhere)
String s = "foo";

// s can be referenced in the lambda
Runnable r = () -> System.out.println(s);
code:
Instance method reference
// instance::instanceMethod syntax
items.forEach(System.out::print);

// equivalent to:
items.forEach((x) -> System.out.print(x));
the rest of this looks okay actually

Vanadium
Jan 8, 2005

What's the yospinion on steve yegge these days?

weird
Jun 4, 2012

by zen death robot
programming languages are a lot like political beliefs, let me explain

double sulk
Jul 2, 2010

VanillaKid posted:

programming languages are a lot like political beliefs

they're all bad

tef
May 30, 2004

-> some l-system crap ->
npm just deleted itself and took out most of my ~/prefix/lib/node_modules #4691

So now I've got to re-install npm without access to npm itself somehow. npm also took out nave in the fallout, so this will be super annoying.

Nomnom Cookie
Aug 30, 2009



tef posted:

npm just deleted itself and took out most of my ~/prefix/lib/node_modules #4691

So now I've got to re-install npm without access to npm itself somehow. npm also took out nave in the fallout, so this will be super annoying.

what u get for using javascript

HORATIO HORNBLOWER
Sep 21, 2002

no ambition,
no talent,
no chance
fuckin a hibernate validator is a piece of poo poo. like i know we should have migrated to tomcat 7 by now but we haven't ok? was it really necessary to depend on el 2.2 for interpolating loving error messages? talk about overkill. too loving bad it's literally the only implementation of bean validation 1.1 which oh yeah jersey 2 has a dependency on so i couldn't use 1.0 if i wanted to. gently caress me

FamDav
Mar 29, 2008

Vanadium posted:

What's the yospinion on steve yegge these days?

he posts in coc and not yospos

Max Facetime
Apr 18, 2009

where it says

Java code:
// instance::instanceMethod syntax
items.forEach(System.out::print);

// equivalent to:
items.forEach((x) -> System.out.print(x));
it isn't completely equivalent, just practically equivalent

MeruFM
Jul 27, 2010
good

enough

Deus Rex
Mar 5, 2005

Kevin Mitnick P.E. posted:

what u get for using javascript

it's a copy paste from here

https://github.com/npm/npm/issues/4691

JewKiller 3000
Nov 28, 2006

by Lowtax

AlsoD posted:

http://www.java8.org/

code:
//Effectively final variables can be referenced in lambdas
// s is effectively final (not changed anywhere)
String s = "foo";

// s can be referenced in the lambda
Runnable r = () -> System.out.println(s);

nothing undecidable about that, no sir

Max Facetime
Apr 18, 2009

effectively final is the best thing ever because it's one less reason to write an actual final anywhere

Max Facetime
Apr 18, 2009

sure, immutability is goodish but monotonically increasing immutability is better

there should be some real support for that rather than trying to make immutability work better

hackbunny
Jul 22, 2007

I haven't been on SA for years but the person who gave me my previous av as a joke felt guilty for doing so and decided to get me a non-shitty av

Max Facetime posted:

where it says

Java code:
// instance::instanceMethod syntax
items.forEach(System.out::print);

// equivalent to:
items.forEach((x) -> System.out.print(x));
it isn't completely equivalent, just practically equivalent

doesn't it use (just guessing) the new dynamic dispatch opcode for both? what's the difference

Sapozhnik
Jan 2, 2005

Nap Ghost
first one passes a method as a lambda directly, second one creates a lambda that calls the method

i'm guessing

Max Facetime
Apr 18, 2009

Mr Dog posted:

first one passes a method as a lambda directly, second one creates a lambda that calls the method

i'm guessing

that's a start, but there's more to it

i'll give a hint and say that coming up with this was quite amusing:

Java code:
    PrintStream out = System.out;

    boolean replaceBriefly = ThreadLocalRandom.current().nextBoolean();
    System.setOut(replaceBriefly ? null : out);

    Consumer<String> action1 = System.out::println;
    Consumer<String> action2 = s -> System.out.println(s);

    System.setOut(out);

    Stream.of("a", "b", "c").forEach(action1);
    Stream.of("1", "2", "3").forEach(action2);
what is action1 going to do? what about action2?

SavageMessiah
Jan 28, 2009

Emotionally drained and spookified

Toilet Rascal

Max Facetime posted:

that's a start, but there's more to it

i'll give a hint and say that coming up with this was quite amusing:

Java code:
    PrintStream out = System.out;

    boolean replaceBriefly = ThreadLocalRandom.current().nextBoolean();
    System.setOut(replaceBriefly ? null : out);

    Consumer<String> action1 = System.out::println;
    Consumer<String> action2 = s -> System.out.println(s);

    System.setOut(out);

    Stream.of("a", "b", "c").forEach(action1);
    Stream.of("1", "2", "3").forEach(action2);
what is action1 going to do? what about action2?

i assume action1 fails randomly because it binds to the current value of System.out and action2 works because it gets the value when it is run?

Adbot
ADBOT LOVES YOU

QuantumNinja
Mar 8, 2013

Trust me.
I pretend to be a ninja.

SavageMessiah posted:

i assume action1 fails randomly because it binds to the current value of System.out and action2 works because it gets the value when it is run?

I'd expect it to do the opposite: action1 relies on the 'out' currently in scope, and action2 builds a closure where 'out' is the potentially-failing one. Otherwise they implemented lambdas incorrectly.

QuantumNinja fucked around with this message at 07:03 on Feb 16, 2014

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply