« first day (406 days earlier)      last day (872 days later) » 

03:00
I made that by combining multiple snippets from the internet. Don't judge.
03:19
Lol we all do that
 
6 hours later…
 
4 hours later…
13:06
Good attempt though, you can’t expect to write perfectly idiomatic code on your first try
Brushing my teeth rn, I’ll idiomaticify it later :p
 
3 hours later…
15:45
@emanresuA First of all, immutability should be preferred to mutability unless it really hurts performance. The var is unnecessary because you're not assigning a new value to r at all, so you can use val r = ... instead (val is kinda Scala's equivalent of JS's const)
Second, for loops and comprehensions can have multiple variables inside them. You can either parentheses, in which case you'll need semicolons (link), or curly braces, which allow you to use newlines instead (link). In Scala 3, you can omit the parentheses/braces and use do instead (link)
You'll see that the output is an unintelligible mess. That's because Arrays don't have a nice toString method. You could use mkString like this (mkString is like join), but let's not use Arrays at all
Arrays are mutable collections (their size is fixed but you can change the values of its elements). We don't need to change values here, so it's better to use an immutable collection instead. List is a pretty good general option (it's a linked list, which isn't always the best for performance but is okay for small collections and easy to handle)
However, here we know that you're going to have exactly 2 elements in each of those pairs, a and b. In such situations, you can use a tuple instead. The syntax for Scala's tuples is the same as Python's (link). You'll notice we no longer need that mkString business because all tuple classes have a sane toString. However, you need an extra parentheses in r += ((a, b))
That's because r += (a, b) is taken to mean "add a and then b to r. This is a really annoying thing about infix methods in Scala, but it's not a huge deal, and we're going to ditch ArrayBuffers later anyway
A couple more unnecessary things: Scala can infer the return type of your method, so you don't need to explicitly write it, and you can write just r instead of return r because the last expression in a method is always returned. Using return is discouraged in Scala (see this article for why). That gets us this, but it's still unsatisfying
Like Arrays, ArrayBuffers should only be used when you absolutely need them. Here, we can just use a for comprehension like in Python to directly construct a collection. That gets us this, which is much simpler and doesn't force users to deal with the annoying ArrayBuffer type
Now suppose we also wanted to make cartesian product work for lists of strings and floats and other types. That's where generics comes in (link). In def cartesianProduct[A](..., think of A as another parameter to cartesianProduct just like list1 and list2, except it's a type instead of a value and the compiler can usually insert it for you
So instead of only handling List[Int] or List[String], you can now handle List[A] for any A
If you want to be really generic, then you can also take the type of the collection as a type argument, but that gets a little complicated
Whew, that's a lot of text :P
16:22
@emanresuA obtw you imported ArrayBuffer but forgot to actually take advantage of the import and use ArrayBuffer directly instead of fully qualifying it
 
4 hours later…
20:13
I'm probably never going to be golfing in Scala, but I'll try not to mess stuff up.
21:05
I'm sure you'll be golfing in Scala in no time, there's tons of sugar and a library with a decent assortment of methods to help you out
@emanresuA Also, it wasn't that bad, when I first started out with Scala I would've used two while loops and two indices i and j :P
The fact that you already have experience with Python and JS should make learning Scala a piece of cake, even with the types
C not-so-M C: Write a BF interpreter
 
2 hours later…
22:49
Ok :p
I never golf in Python tho :P
¯\_(ツ)_/¯ Time to get out of your comfort zone and fine a golf course with some snakes!
23:14
When you search up "scala match statement" and get dating websites
I got perfectly normal results lol
Probably depends on your search history though :P
Got normal results again, what did you get?
Might be 'cos I'm In NZ
obtw @DLosc the fact that HBLList isn't an actual linked list makes for some odd behavior. For example, length gives the actual length + 1 because it seems to be counting the nil at the end. Not sure if that's intended
@emanresuA Can't you turn off ads in DDG?
23:19
You can do that?
idk
To append an element to array, should I just go x = x +: 5 or something else?
You can do x +:= 5, but you shouldn't need to append to arrays at all in Scala
What're you trying to do? Maybe I can help
For the BF interpreter, I'm keeping a list of the start points of all loops currently running
Okay, if you need a growable collection, something like ArrayBuffer would probably be better (use x += 5 with that). Can I see your code?
You could use an ArrayBuffer like this
Cool! Is there a simple way to pop from an arraybuffer?
You could do loopIndices.remove(loopIndices.size - 1)
But I think treating it like a stack might help (you could use var loopIndices = List.empty[Int] instead
Scastie for ^. A disadvantage of this is that the stack (represented by a List) is kinda backwards, since you pop from the front instead of from the back. But it makes for nice pattern matching if you like that
Does Scastie run as JS, Java or something else?
Actually, you might want to preparse the code and have a HashMap[Int, Int] containing the start end indices of all the loops
@emanresuA It runs on a server, so JVM (basically Java), but there's also a Scala.JS mode
If you go to build settings, you'll see that you can choose between Scala 3, Scala 2, and Scala.js. I'm not sure why Scala.js is separated like that, though ¯\_(ツ)_/¯. It's also half broken, I remember println wasn't working properly when I tried it once before
There's also scalafiddle.io if you want to draw stuff with Scala.js, but it seems to be down right now
23:48
How do you turn an Int into a Char?
myint.toChar
Chars can be implicitly converted to Ints because fricking Java, but you can do it explicitly with mychar.toInt
Do you need myint.toChar() or will just myint.toChar work?
The second, I think the first is either a warning or an error

« first day (406 days earlier)      last day (872 days later) »