« first day (787 days earlier)      last day (106 days later) » 

12:09 AM
class ArrayBuffer[A](override protected val initialSize: Int)
  extends AbstractBuffer[A]
    with Buffer[A]
    with GenericTraversableTemplate[A, ArrayBuffer]
    with BufferLike[A, ArrayBuffer[A]]
    with IndexedSeqOptimized[A, ArrayBuffer[A]]
    with Builder[A, ArrayBuffer[A]]
    with ResizableArray[A]
    with CustomParallelizable[A, ParArray[A]]
    with Serializable
@райтфолд lol Scala collections
 
user1804599
How do you implement intersperse as a fold such that it also works for the empty input?
 
What is intersperse? Putting commas between elements, for example?
 
user1804599
Yes.
 
I don't think you can do it without a special case for the empty input.
 
user1804599
I don't like Any/Nothing.
 
user1804599
12:17 AM
It should be Any/None or Anything/Nothing.
 
user1804599
 
None is already taken, isn't it?
type Anything = Any   // problem solved
 
user1804599
@FredOverflow Not as a type. :)
 
user1804599
None is a value in Scala but not a type.
 
user1804599
The type of None is None.type.
 
5:18 AM
@FredOverflow :)
 
 
7 hours later…
12:10 PM
0
Q: Does a static final field ensure thread safety in case of a Singleton?

botIs it necessary for a Singleton class to have a static final reference to it's instance or just a static reference would suffice. Before you mark this question as a duplicate, take a look at these questions and answers : Final keyword is not necessary for Singleton Final keyword is necessary ...

 
 
2 hours later…
1:47 PM
0
Q: Avoid lazy loading by ensuring non static methods in Singleton

botDisclaimer : I am aware that using an enum is one of the most effective ways of creating a Singleton so let's keep this argument aside for now. The two basic use cases for a class to be loaded in the JVM are: An instance of the class is created using the new operator (e.g. new MyClass()) The c...

 
 
6 hours later…
7:39 PM
@райтфолд Any idea why @tailrec does not work here? Eclipse says "it contains a recursive call not in tail position", but I just don't see it.
 
user1804599
@fredoverflow token.withPosition(start) isn't a tail recursive call.
 
It isn't a recursive call at all?
 
user1804599
Indeed, so it's definitely not tail-recursive.
 
user1804599
 
@райтфолд I still don't get it. Please explain like I'm 5 years old.
 
user1804599
7:43 PM
@fredoverflow @tailrec only works when all tail calls are recursive.
 
user1804599
token.withPosition(start) is a tail call that is not a recursive call.
 
Oh, I thought it only works when all recursive calls are tail calls.
 
user1804599
Maybe there is an exception (pun not intended) for functions that have return type Nothing such as error.
 
user1804599
Let's look at the implementation to be sure!
 
user1804599
7:49 PM
@fredoverflow what happens when you make the method final?
 
@райтфолд same thing
 
user1804599
Try tail-call optimising it yourself and see whether that causes problems.
 
user1804599
Maybe it's just impossible.
 
0
Q: @tailrec annotated method contains a recursive call not in tail position

fredoverflowHere is an minimal example that demonstrates my problem: @tailrec def fun(x: Int): Int = { val y = x match { case 5 => return fun(6) case 7 => return fun(6) case 6 => 40 case _ => throw new AssertionError("only 5, 6 and 7 allowed") } y + 2 } Eclipse complains with the fol...

 
user1804599
What if you remove return? Maybe return causes it not to be a tail call.
 
7:56 PM
Without the return, it's no longer a tail call.
 
user1804599
Main.scala:12: error: could not optimize @tailrec annotated method fun: it contains a recursive call not in tail position
 
user1804599
@fredoverflow What does the disassembled code look like?
 
Without the @tailrec, here is what case 5 does:
  45: aload_0
  46: bipush        6
  48: invokevirtual #23                 // Method fun:(I)I
  51: ireturn
 
user1804599
Interesting.
 
Even if I make the method final, it still uses invokevirtual. Weird.
 
user1804599
8:01 PM
invokevirtual is for most non-static non-interface calls.
 
user1804599
The JIT compiler knows it's final and will not generate code that does a vtable lookup.
 
@райтфолд In Scala or Java?
 
user1804599
@fredoverflow JVM
 
I'm pretty sure Scala doesn't have a built-in countChange function. — fredoverflow 12 secs ago
 
user1804599
lol
 
user1804599
8:05 PM
# Currently, the following Mill code causes the VM to crash! :D
use std::io;
proc f(x: String) { io::writeln(x); }
MAIN { f(); }
 
user1804599
Because the compiler doesn't generate code to verify the argument count contract.
 
Couldn't the compiler check that statically?
 
user1804599
No, it's a dynamically typed language.
 
user1804599
Although static analysis is on my todo list.
 
user1804599
8:25 PM
@fredoverflow in Mill you can refer to the match topic using %_!
 
user1804599
match list(1, 2) {
    case list(_, _) =>
        assert(count(%_) == 2)

    case _ =>
        assert(count(%_) != 2)
}
 
@райтфолд Nice, I have longed for that in Scala more than once.
 
user1804599
And in for loop!
 
user1804599
for list(1, 2, 3, 4, 5) {
    io::writeln(%_)
}
 
user1804599
And, more fun, io::writeln has %_ as a default argument!
 
user1804599
8:27 PM
for list(1, 2, 3, 4, 5) {
    io::writeln()
}
 
user1804599
This is possible because %_ is dynamically scoped. :)
 
user1804599
proc writeln(line: String = %_) { … }
 
private def makeDiagnostic(description: String): Diagnostic = {
  var line = lineBeginnings.size
  var begin = 0
  do {
    line -= 1
    begin = lineBeginnings(line)
  } while (begin > previous)
  val column = previous - begin
  val snippet = input.substring(begin, endOfLine(previous))
  new Diagnostic("foobar.c", line, column, description, snippet)
}
 
user1804599
:D
 
user1804599
The diagnostics of my compiler are very confusing since they report the source position of the compiler source, not of the input. :D
 
8:37 PM
Mine is a mixture of both:
Exception in thread "main" language.Diagnostic: foobar.c:6:42: identifier s is not in scope
printf("The annual rate is %d%%.\n", s);
                                     ^
at language.Lexer.makeDiagnostic(Lexer.scala:189)
at language.Lexer.error(Lexer.scala:177)
at language.Lexer.error(Lexer.scala:6)
at language.SymbolTable.checkInScope(SymbolTable.scala:17)
at language.Parser.parsePrimaryExpression(Parser.scala:152)
at language.Parser.parseAssignmentExpression(Parser.scala:128)
at language.Parser.parseExpression(Parser.scala:124)
 
user1804599
lol
 
user1804599
> name groot not in scope at ../millc/lib/Millc/Name.pm line 83.
 
Exception in thread "main"
at main.Main$.main(Main.scala:8)
at main.Main.main(Main.scala)
Something tells me "main" is key here...
 
user1804599
let ($a, %b) = (1, 2)
{
    $a = 3
    %b = 4
}
p($a) # prints 3
p(%b) # prints 2
 
user1804599
This is exceptionally useful.
 
8:40 PM
What is p? previous? print?
 
user1804599
func p(x: Any): typeof(x)
ensure out is x
{
    errln(repr(x))
    x
}
 
user1804599
@fredoverflow Useful for debugging.
 
user1804599
It prints the debug-friendly representation of the input and then returns the input.
 

« first day (787 days earlier)      last day (106 days later) »