« first day (604 days earlier)      last day (289 days later) » 

10:53 AM
Is it an anti-pattern to throw a solution to get out of a recursive algorithm? :)
case class Solution(karel: Karel) extends Exception
5
Q: Using Throwable for Things Other than Exceptions

delmetI have always seen Throwable/Exception in the context of errors. But I can think of situations where it would be really nice to extend a Throwable just to break out of a stack of recursive method calls. Say, for example, you were trying to find and return some object in a tree by the way of a rec...

96
Q: Why not use exceptions as regular flow of control?

PeterMy question boils down to : “Why not use exception (or error) handling for regular program flow?" To avoid all standard-answers I could have Googled on, I will provide an example you all can attack at will. C# and Java (and too many others) have with plenty of types some of ‘overflow’ behaviour...

> I wish I could just have launched the program and wait for exceptions to occur, but there were around 200 exceptions during the start-up in the normal course of operations
lol
 
user1804599
@FredOverflow wrap the function.
 
Oh sure, the client does not know anything about the "Expection". He just gets a normal solution of type Karel.
 
user1804599
Also, just return.
 
user1804599
And make the recursive call a tail call.
 
That would only return to the previous activation of the recursive function.
@rightfold That's pretty hard when you have up to 4 recursive calls ;)
 
user1804599
10:59 AM
This is what a recursive function looks like:
 
user1804599
def f(x: T): R =
  if (condition) x else f(g(x))
 
user1804599
I don't see any need for exceptions here.
 
I have optimized the code for speed. Now it's practically unmaintainable, but I can finally get 8x8 labyrinths :) THE UGLY CODE
Wait, I think there is a bug in line 25.
 
user1804599
I hope tearDownWall returns a new Karel.
 
sure
Karel is immutable
 
user1804599
11:04 AM
Nice.
 
Oh my god, I fixed line 25, and now the algorithm is blazingly fast!
I'm gonna try 10x10 again...
 
user1804599
Now you can de-optimise it!
 
I didn't notice the bug before, because the functions destinationOpen and destinationFound used to be called foo and bar :D
 
user1804599
Speaking of optimisations.
 
user1804599
I think I will always inline functions that are used only once, and that use is a call.
 
user1804599
11:09 AM
I don't have to worry about recursion because I don't support recursion yet. :D
 
28 messages moved from Lounge<C++>
 
user1804599
This is how you do recursion in Styx:
 
user1804599
let y = fn f => (fn g => fn x => (f (g g)) x) (fn g => fn x => (f (g g)) x) in …
 
user1804599
:D
 
It looks unpenetrable. You should rename Styx to Virgin.
 
user1804599
11:17 AM
let y = fn f => (fn g => fn x => (f (g g)) x) (fn g => fn x => (f (g g)) x) in
let fac = y (fn recur => fn n => if n = 1 then 1 else n * recur (n - 1)) in
debug.trace (fac 10)
 
Does this stuff compile and work yet? Awesome!
 
user1804599
* and - don't yet, as doesn't ..
 
user1804599
Everything else does. :)
 
user1804599
Here is some function composition fun: ideone.com/f8k1aU
 
I had to change a constant from 99 (10^2-1) to 63 (8^2-1). When I looked at the source code, the number was already 63, and I was confused as fuck.
 
user1804599
11:19 AM
Lines 2 to 6 are Styx code, the rest is the compiler output.
 
Turns out 99 dec is 63 hex :)
 
user1804599
Nice. :P
 
I don't really understand the output, but it seems to work. Congratz :)
 
user1804599
I will rewrite the Lua generator in C++, though.
 
speed?
 
user1804599
11:21 AM
I want Lua to be an implementation detail of my interpreter.
 
user1804599
And my interpreter will be in C++.
 
user1804599
I want to swap Lua VM out for a custom multithreaded VM later.
 
user1804599
brb; shower
 
I already took a shower today.
0
Q: Execute competing calculations concurrently

FredOverflowI wrote a function that generates a labyrinth based on randomness. Most of the time, this function is very fast. But every once in a while, due to bad luck with the random numbers, it takes a couple of seconds. I would like to start this function multiple times concurrently and let the fastest f...

 
user1804599
11:42 AM
Doing calculations concurrently is pointless.
 
user1804599
You want to do them in parallel.
 
@rightfold Shall I edit accordingly, or do you want to do it?
 
user1804599
If I do it people will probably complain.
 
user1804599
Doing calculations concurrently will almost always slow them down. :P
 
@rightfold done
 
user1804599
11:56 AM
In college we got taught how to spawn threads but nothing more. :P
 
user1804599
I wonder how many bugs were the result of students not knowing about race conditions.
 
user1804599
In Android you aren't allowed to do networking on the main thread, so we simply did this:
 
user1804599
Thread thread = new Thread(new Runnable { … });
thread.start();
thread.join();
 
user1804599
:D
 
@rightfold I just answered my own question.
 
user1804599
11:58 AM
Your solution is really an ugly hack. XD
 
user1804599
@FredOverflow I posted a Scala answer.
 
@rightfold Can you include the necessary imports in your answer?
 
user1804599
@FredOverflow Done.
 
Now I wonder... if the fastest labyrinth generator wins, does that influence the shape of the average resulting labyrinth? :)
 
user1804599
I don't know.
 
user1804599
12:11 PM
Try it out!
 
Anyway, the program is now responsive as fuck. Awesome!
 
user1804599
I hope Java people will read my answer, think "that's short and awesome" and switch to Scala.
 
user1804599
I decided to give C++ a try when I was still a C programmer because I saw a Stack Overflow answer using std::string.
 
What is import scala.concurrent.ExecutionContext.Implicits.global good for, exactly?
 
user1804599
You need to pass an execution context to Future.apply.
 
user1804599
12:15 PM
If you don't it will use the implicit one, but you have to import it (it's not in Predef).
 
Your solution works great, I accepted the answer.
 
user1804599
No problem and thanks.
 
/*
 * Sometimes, due to "bad luck" with the random number generator,
 * the labyrinth generation causes a noticeable pause.
 * A practical fix is to run the function multiple times in parallel
 * and let the fastest function "win".
 * see stackoverflow.com/questions/26074182
 * Thank you very much, stackoverflow user rightfold!
 */
def walkTheLabyrinth: Karel = {
  import scala.concurrent._
  import scala.concurrent.ExecutionContext.Implicits.global

  val futures = for (i <- 1 to 4) yield Future { walkTheLabyrinth0 }
@rightfold Note the last comment line ;)
 
user1804599
What if they are all unlucky? :D
 
Should I start a thousand computations in parallel? :)
 
user1804599
12:22 PM
They won't run in parallel. :P
 
user1804599
Unless you wait a year or eight.
 
What if I told you I have 1024 cores?
 
user1804599
I would not believe you.
 
That is the correct answer. But some day I will!
 
user1804599
That said, I have 22 cores.
 
12:23 PM
On one machine?
 
user1804599
Two in my laptop, two in my desktop, one in my iPod, one in my phone, eight in my work computer and eight in my PlayStation.
 
user1804599
@FredOverflow I wish.
 
Are you sure iPhones only have a single core?
 
user1804599
iPhones have two cores, I think.
 
user1804599
If they have and if I had an iPhone I would still not lie, because my iPod would be my iPhone. :)
 
12:25 PM
Oh, I thought you wrote iPhone.
 
user1804599
I just realised we can kick Java-proposing trolls now. :D
 
How often does that happen?
 
user1804599
Rarely.
 
user1804599
I think this room is more active than the Java room.
 
user1804599
12:27 PM
Java programmers are too busy writing bad code working.
 
user1804599
@FredOverflow I think you want a non-deterministic Turing machine to generate labyrinths!
 
user1804599
Or solve P = NP with the outcome "true!"
 
What is the dynamic type of for (i <- 1 to 4) yield whatever? The IDE says IndexedSeq. Is it a Vector?
 
user1804599
Vector is a subtype of IndexedSeq.
 
user1804599
I don't know the dynamic type.
 
user1804599
12:31 PM
You can easily ask the Scala REPL, though.
 
println(futures.getClass)
class scala.collection.immutable.Vector
 
user1804599
scala> for (i <- 1 to 4) yield i
res0: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4)
 
@rightfold Or I could simply generate all possible 8x8 labyrinths and store them in a database.
> I want you to build me a database with the labyrinths already in it!
 
user1804599
@FredOverflow O(1) labyrinth generation algorithm!
 
How many 8x8 labyrinths do you suppose there are?
 
user1804599
12:34 PM
About 128.
 
Are you kidding? I was expecting billions.
 
user1804599
A corridor can either be | | or , so 8 * 8 * 2. :v
 
user1804599
Note that I am extremely bad at statistics.
 
I see lots of other wall combinations in that picture.
 
user1804599
Me too. :D
 
user1804599
12:37 PM
I see six different combinations.
 
Hm, but there are indeed mostly only 6 possibilities, right? Horizontal, vertical, or one of four corners.
 
user1804599
Hey wait, there is a dead end.
 
Plus 2 dead ends, but you can ignore those mostly.
So what is 6^64? :)
 
user1804599
Fewer than 384 then.
 
Most of those 6^64 combinations won't make any sense, of course.
 
user1804599
12:38 PM
You can easily generate all combinations, store them in an array and serialise that to disk.
 
Honestly, I don't think that all labyrinths would fit in RAM.
 
user1804599
Why not?
 
user1804599
If there are fewer 384 combinations it should easily fit.
 
user1804599
A cell is a number from 1 to 8.
 
user1804599
So you can store four cells in a byte.
 
12:39 PM
6^64 is 10^50, not "fewer than 384" :)
 
user1804599
You can have only two dead ends.
 
user1804599
Huh, oh. :v
 
I still don't understand where that number 384 comes from.
 
user1804599
8*8*6
 
user1804599
:P
 
12:41 PM
So there is 32*2 = 64 different ints?
 
user1804599
Yes! 0 to 63!
 
Then we only need 6 bits to store a 32 bit int!
You have found the holy grail of data compression!
 
user1804599
I made up a compression algorithm one day.
 
Did you implement it? Did it work?
All I got working was LZW or something.
 
user1804599
You take the array of bytes, convert it to a number and then calculate the number of times you would have to call std::next_permutation starting with 01234…N.
 
12:43 PM
And that number is guaranteed to have each numer from 0 to N in it exactly once?
 
user1804599
I implemented it and it was fast and worked, except the number of times you'd have to call std::next_permutation was a number of which any representation I could think of was larger than the original array of bytes. :D
 
user1804599
@FredOverflow Oh wait, no. It was the input array sorted (no duplicates removed).
 
I am hungry, but I am too lazy to buy groceries and cook :(
 
user1804599
The result would then be a 257-tuple (std::next_permutation count, 0 count, 1 count, 2 count, …).
 
12:46 PM
All I have been doing for the last 30 minutes is watch Karel go through labyrinths :)
 
user1804599
Nice. :P
 
user1804599
@FredOverflow Hire ten cooks and eat only the food of the one that finishes first.
 
But wouldn't that probably mean I end up eating garbage?
 
user1804599
Be sure to let the other ones continue until they're done.
 
user1804599
@FredOverflow At least it will still be warm!
 
12:51 PM
This parallel Future stuff is pretty amazing!
 
user1804599
Yeah, Future is a nice API.
 
user1804599
Futures are isomorphic to callbacks but the usage is much nicer.
 
user1804599
You can actually return using return values rather than calling parameters with the result.
 
user1804599
This is Scala, not C!
 
user1804599
Future is also a monad! (flatMap and Future.successful)
 
user1804599
12:56 PM
I want to implement a generic finite state machine in Scala that doesn't use mutability. It would be very useful for writing games.
 
@rightfold What game do you want to write?
 
user1804599
trait FSM {
  type Event
  type State

  def emit(e: Event): (this.type, State)
}
 
user1804599
@FredOverflow An RPG I've been thinking of for like a year.
 
user1804599
FSMs are useful for turn-based battles.
 
user1804599
this.type makes CRTP obsolete!
 
12:59 PM
Anyway, gotta go buy groceries now, or I'll starve over the weekend :(
cu l8r
 
user1804599
Goodbye.
 
user1804599
2:02 PM
0
Q: Why do I need an explicit type argument list here?

rightfoldThis is the shortest SSCCE I could craft: import scala.annotation.tailrec trait State { def isAcceptState: Boolean } trait FSM[T <: FSM[T, A, S], -A, +S <: State] { this: T => def state: S def resume(input: A): T } object FSM { @tailrec def resumeUntilAccept[T <: FSM[T, A, S], A, S <...

 
user1804599
@FredOverflow Scala Sucks!
 
5:09 PM
@rightfold Wanna make a room about it?
 
user1804599
@FredOverflow Nah. :P
 
user1804599
I got it working now! ideone.com/17UoXH
 
9:58 PM
0
Q: What's the point of final objects?

FredOverflowI just noticed that it's possible to declare objects as final in Scala: final object O What's the point of doing that? One cannot inherit from objects, anyway: object A object B extends A // not found: type A

 
user1804599
@FredOverflow Heh. :P
 
user1804599
I'm working on my language specification! gist.github.com/rightfold/5f8e380949a3c8a7db64
 
user1804599
@FredOverflow I answered it.
 
user1804599
10:20 PM
Today I learned:
 
user1804599
34
Q: What is the forSome keyword in Scala for?

FreewindI found the following code snippet: List[T] forSome { type T } The forSome looks like a method, but my friend told me it's a keyword. I googled it, but found few documents about forSome. What does it mean, and where can I get some documents about it?

 
10:42 PM
@rightfold I don't understand these forward reference errors. Am I not getting something, or is this a compiler bug?
Main.scala:65: error: forward reference extends over definition of value before
if (causesPartition(pos, dir)) return
All of these names are already defined at this point?
 
user1804599
Weird. :v
 
3
Q: Forward reference extends over definition of value shapes

Mikaël MayerSimilar problems 1. and 2. mention real problems of forward referencing in scala. But I found this particular simple case where there is no forward referencing. All blocks are perfecly stand-alone. def testFunction() = { def recursiveMethod(i: Int, j: Int = 3): Unit = i match { case 0 => p...

probably related
> This is the code with the comment [Martin] This is pretty ugly. So it's not unknown, just in need of someone with free time.
lol
 
user1804599
@FredOverflow lol
 
user1804599
_ catch do else extern fail false finally fn if go implement import
in interface let library make match method operator private struct
then true try use virtual with yield
 
user1804599
So many keywords. :[
 
10:50 PM
"try use virtual with yield" lol
 
user1804599
lol
 
 ____________________________
< try use virtual with yield >
 ----------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
 
user1804599
:v
 
user1804599
fail makes the current pattern match fail:
 
I want to measure my terrible code, but the stupid "forward reference error" keeps me from running it :(
 
user1804599
10:52 PM
let y = true in
1 match (
    1 => if (y) fail else 1
    _ => 3
)
 
No idea how to solve it or what the error even means.
 
user1804599
The result of this is 3.
 
user1804599
fail will try the next pattern.
 
a bit like guarded patterns
 
user1804599
Yes, except you can do multiple actions before going to the next one.
 
10:54 PM
1 match {
  case 1 if (!y) => 1
  _ => 3
}
 
user1804599
Kinda like fallthrough in Go or goto case in C#.
 
user1804599
1 match (
  case 1 =>
    f(); g(); fail
  _ => 3
)
 
user1804599
go is for the go pattern that is useful for writing tail-recursive functions:
 
user1804599
let map f xs (go result = list {}) = xs match (
  list [] => result
  hd :: tl => go f tl (f hd :: result)
)
 
user1804599
50
Q: Haskell: why the convention to name a helper function "go"?

Dan BurtonI see go a lot when reading Haskell material or source, but I've never been really comfortable about it - (I guess it has the negative connotation of "goto" in my mind). I started learning Haskell with LYAH, and that's where I picked up the tendency to use acc and step when writing folds. Where d...

 
user1804599
11:01 PM
IMO the only purpose for language features is to reduce boilerplate otherwise common in the domain in question.
 
user1804599
@FredOverflow
 ________________________________________
/ let library make match method operator \
\ private                                /
 ----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
 
@rightfold Oh my god, I am so stupid... I defined two local variables dir and n which are already the names of parameters :)
 
user1804599
That is not acceptable!
 
Not to the Scala compiler it isn't, but the error message is so misleading.
 
user1804599
That's what you get for writing large functions!
 
11:05 PM
My "optimized" code involving bit trickery instead of array access is actually slower :)
So many hours wasted "optimizing"...
 
user1804599
What if you write idiomatic Scala code?
 
for (i <- 0 until 4) is 25% slower than a simple while loop :(
 
user1804599
:v
 
user1804599
Write an optimising compiler!
 
user1804599
Oh wait, you can't, since class files can be loaded dynamically. :D
 
user1804599
 
user1804599
> brutal_kill
 
11:35 PM
I can finally create 10x10 labyrinths. Here are the timings of 8 computations in parallel:
17
44
48
77
89
3146
122346
 
user1804599
Which 8 computations?
 
Note that the 7th calculation took two minutes, and the 8th calculation still hasn't finished :)
 
user1804599
All 10x10?
 
val futures = for (i <- 1 to 8) yield Future { walkTheLabyrinth57 }
8 random computations
 
user1804599
lol.
 
user1804599
11:37 PM
If you could find a pattern in the unlucky random seeds, you can prevent those from happening!
 
Ah, the 8th calculation finally finished :)
584967
about 10 minutes
 
user1804599
lol
 
Hm, I could set a hard limit on the number of backtracks and let the job cancel itself if that limit is reached.
 
user1804599
Did your final object question get an answer yet? :D
 
You answered it.
 
user1804599
11:44 PM
@FredOverflow Make it non-tail recursive and use recursion and the JVM will handle that for you!
 
user1804599
Just catch StackOverflowException!
 
@rightfold The stack will never be deeper than 100 for a 10x10 maze.
 
user1804599
Make it!
 
It's not about stack depth, it's about the number of backtracks.
 
user1804599
Make backtracking recur. :P
 

« first day (604 days earlier)      last day (289 days later) »