« first day (582 days earlier)      last day (111 days later) » 

user1804599
1:40 PM
Let's write something in Scala.
 
user1804599
Something totally unrelated to game development and web development.
 
Ell
@rightfold what shall we write?
 
user1804599
A program that displays the current Moon phase with ASCII art.
 
Ell
I'm writing a java/scala wrapper atm
 
user1804599
What for?
 
Ell
1:56 PM
well it started because I wanted to write my own XMPP server in scala
but there aren't any asynchronous xml parsing libraries (that I can find)
so I was wrapping expat manually
but it's meh, I figure I should just try writing something to make wrapping easier and safer
 
Ell
2:21 PM
@rightfold can you tell me if the following code is idiomatic scala?
 
user1804599
Yes.
 
Ell
class Point[T : Numeric](var x: T, var y: T) {
    def transpose(dx: T, dy: T)(implicit num: Numeric[T]) = {
        x = num.plus(x, dx)
        y = num.plus(y, dy)
    }
}
I'm not sure of a better way to make it generic over numeric types besides using the implicit
 
user1804599
Why var instead of val?
 
user1804599
Why not a case class?
 
lol scala
 
Ell
2:23 PM
@rightfold because I want to be able to transpose the point
 
user1804599
case class Point[T](x: T, y: T) {
    def transpose(dx: T, dy: T)(implicit num: Numeric[T]) =
        Point(num.plus(x, dx), num.plus(y, dy))
}
 
Ell
@rightfold why would I use a case class here?
 
user1804599
@Ell Mainly for toString, equals, hashCode and pattern matching.
 
user1804599
@Ell return a new point.
 
user1804599
@FredOverflow do you do static analysing on the AST or the byte code?
 
2:26 PM
@rightfold AST
 
user1804599
Oh wait you interpret the AST directly, right?
 
nope
 
user1804599
Oh. :P
 
I used to do that, but I have a VM now. For about half a year or something.
@rightfold But since FindBugs manages to do static analysis on the bytecode just fine, maybe one could look into that area as well...
 
user1804599
def isDefinitelyReferentiallyTransparent(expr: ast.Expression) = false this is a great start for my static analyser.
 
2:30 PM
@rightfold You should leave it as that and ship it.
 
Ell
@rightfold I'm terrible aren't I:
case class Point[T : Numeric](val x: T, val y: T) {
    def transpose(dx: T, dy: T) =
        Point[T](evidence$1.plus(x, dx), evidence$1.plus(y, dy))
}
 
user1804599
val is implicit on case classes.
 
@Ell What is evidence$1?
 
user1804599
And don't use those magic names directly. Use an explicit implicit.
 
implicitly[T] or something?
 
user1804599
2:31 PM
@FredOverflow case class Point[T: Numeric](…) is equivalent to case class Point[T](…)(implicit evidence$1: Numeric[T]).
 
"evidence" sounds inspired by SPJ.
Oh wait, actually, he'd call it "witness".
@rightfold So T: Numeric means T lies in the Numeric typeclass? Is it typeclass or type class?
 
user1804599
  def isDefinitelyReferentiallyTransparent(expr: ast.Expression): Boolean =
    expr match {
      case ast.IntegerLiteral(_) => true
      case ast.TupleLiteral(elements @ _*) =>
        elements.forall(isDefinitelyReferentiallyTransparent)
      case _ => false
    }
 
user1804599
@FredOverflow Now shipping is more fun!
 
@FredOverflow Yes.
 
user1804599
@FredOverflow Yes.
 
user1804599
2:36 PM
@FredOverflow But if you want to use members of said type class then you need an explicit implicit parameter.
 
user1804599
 
user1804599
That's nice. :)
 
48 messages moved from Lounge<C++>
 
Ell
oh hey
 
user1804599
def isDefinitelyPure(fn: ast.Function) = isDefinitelyReferentiallyTransparent(fn.body) :D
 
Ell
2:45 PM
@rightfold why Definitely? :P
 
user1804599
Because I cannot automatically detect purity for every expression.
 
user1804599
For example:
 
user1804599
let f g = g 42
 
user1804599
g 42 could be referentially transparent, but it might not be. It depends on what was passed in at runtime.
 
user1804599
But I cannot detect that at compile time.
 
Ell
2:54 PM
ohhh
I see
 
user1804599
However, 42 + 89 is definitely referentially transparent.
 
user1804599
4:34 PM
object ConstantFoldingPass extends Pass {
  override def apply(instructions: Vector[Instruction]) = {
    val result = new VectorBuilder[Instruction]
    instructions.foreach {
      case AddInstruction(id, IntegerInstruction(_, a), IntegerInstruction(_, b)) =>
        result += IntegerInstruction(id, a + b)
      case instruction => result += instruction
    }
    result.result()
  }
}
 
user1804599
This should work!
 
user1804599
  "ConstantFoldingPass" should "add integers" in {
    val a = IntegerInstruction(InstructionID(), 1)
    val b = IntegerInstruction(InstructionID(), 2)
    val add = AddInstruction(InstructionID(), a, b)
    val instructions = Vector(a, b, add)
    val optimizedInstructions = ConstantFoldingPass(instructions)
    optimizedInstructions shouldBe Vector(a, b, IntegerInstruction(add.id, 3))
  }
 
user1804599
It works!
 
user1804599
However, it doesn't propagate the replacement of the add instruction through.
 
@rightfold Oh, I didn't know about VectorBuilder. Nice!
 
user1804599
4:48 PM
Oh, fuck. Cancer. This doesn't work.
 
user1804599
I need more indirection.
 
user1804599
object ConstantFoldingPass extends Pass {
  override def apply(instructions: Seq[(InstructionID, Instruction)]) =
    instructions.zipWithIndex.foldLeft(instructions) { case (result, ((id, instruction), index)) =>
      instruction match {
        case AddInstruction(aID, bID) =>
          val aOp = result.find(_._1 == aID).get._2
          val bOp = result.find(_._1 == bID).get._2
          (aOp, bOp) match {
            case (IntegerInstruction(a), IntegerInstruction(b)) =>
              result.updated(index, id -> IntegerInstruction(a + b))
 
user1804599
This works better, but it's so fucking ugly.
 
user1804599
5:05 PM
@FredOverflow is there a way to eliminate the duplicate case _ => result?
 
user1804599
Hmm.
 
user1804599
  object FindIntegerInstruction {
    def unapply(id: InstructionID)
               (implicit instructions: Seq[(InstructionID, Instruction)]) =
      findInstruction(id, instructions) match {
        case i: IntegerInstruction => Some(i)
        case _ => None
      }
  }

  override def apply(instructions: Seq[(InstructionID, Instruction)]) =
    instructions.zipWithIndex.foldLeft(instructions) { case (result, ((id, instruction), index)) =>
      implicit val instructionList = result
      instruction match {
 
user1804599
Just a custom extractor! :D
 
Ell
@rightfold ahhh
 
5:29 PM
@rightfold Where? I don't see it.
 
case _ => result
}
case _ => result
The end of the code (other than closing braces).
 
6:02 PM
@JerryCoffin You could catch a MatchError ;)
 
user1804599
6:15 PM
eww
 
Ell
I might write a function differentiator
 
@Ell You mean like in the classic LISP book?
 
Ell
I haven't read the classic LISP book o.O
I feel like the problem would lend itsself to case classes
 
It's an example in one of the first chapters IIRC.
 
@FredOverflow That's really the classic for Scheme. FWIW, it's also available freely online (legitimately). mitpress.mit.edu/sicp
 
user1804599
6:29 PM
@FredOverflow One of the best books ever.
 
Ell
how do I pattern match on function parameters?
ie
 
user1804599
def f(x: T) = x match { … }
 
Ell
I meant something else, but it dm, I got it now :)
with pattern matching, do the higher patterns get higher precedence?
 
user1804599
Cases are tried from top to bottom.
 
Ell
kk cool
hmm
I wonder if I can apply traits to case classes
 
user1804599
6:43 PM
Case classes can extend traits.
 
user1804599
You can also mix them in upon construction when new is used, but beware that copy will not consider them in that case.
 
Ell
I have some case classes - sum/difference/product/quotient/exponent/variable/constant and I want a polynomial case class but I'm not sure how it'd be defined
so I was thinking I wonder if I can make a trait that will assert certain properties on the input and check if it is a polynomial or not
 
user1804599
trait Polynomial
case class Sum(addents: Seq[Polynomial]) extends Polynomial
case class Product(factors: Seq[Polynomial]) extends Polynomial
 
Ell
well. I'll show you what I have
abstract class function
case class sum(lhs: function, rhs: function) extends function
case class difference(lhs: function, rhs: function) extends function
case class product(lhs: function, rhs: function) extends function
case class quotient(numerator: function, denomenator: function) extends function
case class exponent(base: function, exponent: function) extends function
case class logarithm(base: function, exponent: function) extends function
case class variable(name: String) extends function
and then this:
  def differentiate(f: function): function = {
    f match {
      case sum(lhs, rhs) => sum(differentiate(lhs), differentiate(rhs))
      case difference(lhs, rhs) => difference(differentiate(lhs), differentiate(rhs))
      case product(lhs, rhs) => sum(product(lhs, differentiate(rhs)), product(rhs, differentiate(lhs)))
      case quotient(numerator, denomenator) => quotient(difference(product(denomenator, differentiate(numerator)), product(numerator, differentiate(denomenator))), exponent(denomenator, constant(2)))
 
user1804599
Do case object e extends function.
 
Ell
6:45 PM
(sorry for wall of code)
 
user1804599
Parameterless case classes are silly.
 
Ell
kk
@rightfold can I still match against that?
 
user1804599
Yes.
 
user1804599
The type of e is e.type.
 
user1804599
Also, you should really capitalise the names.
 
Ell
6:49 PM
I was just worried about clash with Function. but okay
 
user1804599
You would look like Ell.
 
Ell
@rightfold oops. Boy I'm tired :P
also I fix :D
 
7:01 PM
There are so many girls in the functional programming world.
 
user1804599
Me!
 
user1804599
And not you!
 
user1804599
Functional programming is great.
 
Ell
7:22 PM
My differentiator doesn't work :(
hmm
I need to write a simplifier too
 
@Ell ...then apply it to itself a few times until it's really easy to understand.
 
Ell
heh yeah
if only life was that simple
 
@Ell Oh! You need to apply the simplifier to your life too?
 
Ell
Yes!
Now... how on earth do I serialise my life onto a hard drive...
This simplifier is looking a little recursive
 
@Ell Can't be done. Life happens too fast. You need an SSD.
 
Ell
7:32 PM
I think I just found my error...
 
user1804599
Final Fantasy XIII graphics are impressive.
 
Ell
case classes automatically have value equality operators defined don't they?
the simplifier is also infinite recurring it seems
 
user1804599
8:05 PM
Structural equality.
 
Ell
I fixed
but my differentiator still doesn't work :(
Still. I like pattern matching a lot
 
user1804599
Pattern matching is a neat way of taking apart data.
 
user1804599
10:42 PM
Who needs x eq y when you can do x.isInstanceOf[y.type]? :D
 
11:00 PM
@rightfold You do. You need it now...are you done yet?
 
@rightfold Wait, you have a minecraft server?
 

« first day (582 days earlier)      last day (111 days later) »