« first day (473 days earlier)      last day (805 days later) » 

6:09 PM
This isn't a tutorial, just a brief primer for Python programmers: docs.scala-lang.org/scala3/book/scala-for-python-devs.html
 
It's true that the Scala book does start off with a bunch of "Scala's type system has this great feature," which doesn't mean anything if you don't know a lot about type system theory (I don't). It might be best to start from this point instead.
 
lmao yeah
 
Scastie is really helpful for trying out small snippets. You don't have to figure out the boilerplate and compile process for a full program.
 
If you don't have Scala installed but you do have sbt, try sbt console to get the REPL
 
I think I know a bit about the console
but how does a .scala file work?
does it have a main class or smth like that?
 
6:13 PM
It only needs a main class if you're going to run that file
In Python, you'd check if the filename is main inside that. In Scala, you'd have @main def helloWorld = println("Hello World!") or an object with a main method
Only one Scala file in your entire project needs a main class (in Python too you don't run every single module as the main module)
 
nice thanks
 
6:36 PM
@main def HelloWorld = {
  var i = 2+2
  println(i)
}
^ What's the problem?
 
Are you trying to run this in Scastie?
What error are you getting?
That looks like a valid program (could be improved but that isn't important right now)
 
@user I'm running it is Main.scala
and with scalac in the terminal
Error is error: expected start of definition
 
Which version of Scala did you download? This won't work with Scala 2
If you installed Scala 2, you'd need object HelloWorld { def main(args: Array[String]) = { println(4) } }
 
oh
okay
so sbt version is 3
but scalac version is 2
 
People don't actually use scalac/scala directly in practice, they mostly just use sbt
 
6:45 PM
so using the console?
 
For the REPL, you can try running scala and type in var i = 2+2; i there
 
Can I run the file using sbt?
 
I think you can?
 
or is the only option the REPL?
 
Try Scastie for simple stuff tho
@mathcat The REPL won't let you run the file, just evaluate expressions one by one
 
6:46 PM
okay
 
I'd recommend uninstalling Scala 2 and installing Scala 3 instead if you want to run single file Scala 3 programs
Sorry, if you want to run a single file written in Scala 3, you don't have much recourse
 
uh
I actually have a whole directory
with src, prject and stuff
 
Oh cool
Did you create it with sbt?
 
I think so
 
Oh ok, then you just need to do sbt run or something
Or maybe sbt runMain HelloWorld if that doesn't work?
Here are instructions on a Hello World in sbt
 
6:50 PM
Working!
thanks
I'll try Fizz Buzz
 
np, happy to see it's working
 
7:02 PM
\o/
 
Why is this not working?
nvm
I got it
 
Did you mean to do s +=?
 
yeah
 
s+"Fizz" is just an expression
 
I am confused
 
7:11 PM
About s+?
 
I was confused
no
anywhat
 
(btw I'd do something like this)
 
I did FizzBuzz!
for (r <- 1 to 100) {
  var s = ""
  if (r % 3 == 0) s+="Fizz"
  if (r % 5 == 0) s+="Buzz"
  if (s.length == 0) s += r
  println(s)
}
 
Yay!
println(
  if (r % 15 == 0) "FizzBuzz"
  else if (r % 5 == 0) "Buzz"
  else if (r % 3 == 0) "Fizz"
  else s"$r"
)
I'd do something like ^ to avoid the mutation of s and the length check
 
mutation?
 
7:14 PM
Changing the value of s (s += foo does s = s + foo)
 
You're not allowed to do that?
 
Oh no you are
It's just not preferred
 
ok
o/ I have to learn
 
You can stick with the mutation, but I'd still suggest making the last check if (s % 3 != 0 && s % 5 != 0)
o/
 
 
4 hours later…
10:57 PM
@user Why are both _s evaluating to the first argument in 0.to(_).sum/2+_? ATO I'm sure I've seen some anonymous functions with two arguments that use _ for both of them.
 
Hrm
I don't see what's wrong there
The second _ does evaluate to the second argument
(a, b) => 0.to(a).sum/2+b gives the same results
 
Now I'm really confused
 
It makes complete sense to me lol
0.to(4).sum is 10, /2 is 5, +9 is 14
Did you want /(2+9) instead?
 
oy vey [headdesk]
No, I want 0.to(4).sum+9
 
Oh lol
 
11:08 PM
The /2 is from the closed-form formula for triangular numbers :P
 
Ah, makes sense
I wonder how you could use that with underscores. Probably very verbose
Seq(0,1) map _.+ reduce(_*_) gets you n*(n+1) but doing /2 is hard
Adding a `/` 2 at the end still has higher precedence than the alphanumeric methods :(
Ooh, Seq(0,1) map _.+ reduce(_*_) to (0, -1) map (_/2) apply 0 does the job
Seq(0, 1) map _.+ reduce(_*_) to (0, -1) map (_/2) map _.+ apply 0 if you want to add another number to that
Please ignore the above code Wezl :P
 
11:25 PM
what ok so what is it
 
It is an attempt to do the 0.to(_).sum+_ thing but using the n(n+1)/2+b formula, with underscores
As you can see, it didn't go well :P
 
you can add all the underscores you want in comments :P
 
val f: (Int, Int) => Int = ??? //_GO_****_YOURSELF_
 
@user O_O
 

« first day (473 days earlier)      last day (805 days later) »