Announcement

Collapse
No announcement yet.

Write Some ****ing Code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Originally posted by loinburger View Post
    Actually, I take that back, there was one idiot who asked a bunch of Scala trick questions. Scala has a "map" function that, given a sequence, will apply a function to each element of the sequence and return a new sequence consisting of the function's outputs, for example Seq(1, 2, 3).map(i => i + 1) will return Seq(2, 3, 4). You can use a shorthand for this: Seq(1, 2, 3).map(_ + 1) will return the same thing.

    So here's what this dumbass asks me:

    Dumbass: What's the output of Seq(1, 2, 3).map(i => println("Hi"); i + 1)
    loin: Prints "Hi" three times, and returns Seq(2, 3, 4)
    Dumbass: How about Seq(1, 2, 3).map(println("Hi"); _ + 1)
    loin: I assume the same thing, but if that were the case you wouldn't be asking me, so...
    [puts the input into an interpreter]
    loin: Huh, it prints "Hi" once and returns Seq(2, 3, 4)
    Dumbass: Why?
    loin: I don't care
    Dumbass: Let me explain it to you anyway!
    loin: AHHHHHHHHHHHH
    Is that underscore the wildcard value like in ML?
    If there is no sound in space, how come you can hear the lasers?
    ){ :|:& };:

    Comment


    • Originally posted by Hauldren Collider View Post
      That's...a fairly complicated operation to make atomic. At least, if you have a non-primitive data structure you are comparing and swapping.
      To make a lock-free queue you basically just make a linked list with AtomicReferences, something like
      Code:
      Class LockFreeQueue {
          AtomicReference head
          Node tail
      
          Class Node {
            Object obj
            AtomicReference next = new AtomicReference(null)
          }
      
          Object dequeue {
              Node current = head.get
              Node next = current.next
              if(head.compareAndSwap(current, next)) {
                  return current.obj
              } else {
                  return dequeue
              }
          }
      
          void enqueue(Object obj) {
              Node node = new Node(obj)
              if(tail.next.compareAndSwap(null, node)) {
                 tail = node
              } else {
                  enqueue(obj)
              }
          }
      }
      The key is that the enqueue method requires two operations (update tail's next field, then update tail), but a second method invocation can't proceed until the first method invocation has executed both operations (at which point tail.next will be null and the compareAndSwap can succeed)
      Originally posted by Hauldren Collider View Post
      Is that underscore the wildcard value like in ML?
      Yeah, most of Scala's functional stuff comes from Haskell (which is why so many awful Haskell programmers flock to it, since it's impossible to find a job coding in Haskell)
      Last edited by loinburger; November 4, 2014, 20:46.
      <p style="font-size:1024px">HTML is disabled in signatures </p>

      Comment


      • OK, I still can't understand what that Scala code is doing. Is there some sort of implicit function there in the parentheses, without a rocket operator? That's wacky as ****.

        EDIT: It looks like what's happening is that it runs the print statement then it takes the second statement as the lambda argument, but that's just weird as **** to me because in my mind, that expression should not be a function. Actually I think it should typeerror, if Scala were being reasonable.
        If there is no sound in space, how come you can hear the lasers?
        ){ :|:& };:

        Comment


        • Seq(1, 2, 3).map(i => println("Hi"); i + 1) is saying "apply the function [println("Hi"); i + 1] to each element of Seq(1, 2, 3), where 'i' represents the current sequence element"
          So it prints "Hi" three times, once per element in the sequence; then it returns Seq(1 + 1, 2 + 1, 3 + 1) -> Seq(2, 3, 4)
          Why doesn't Seq(1, 2, 3).map(println("Hi"); _ + 1) do the same thing? I don't know and I don't care, you're not supposed to have side-effects in map functions anyway
          <p style="font-size:1024px">HTML is disabled in signatures </p>

          Comment


          • I'm guessing the reason is because it is actually evaluating the expression in the parentheses in the second one. The evaluation of the argument in the first one evaluations (i => println("Hi"); i + 1) as a function, whereas the second one evaluates (println("Hi"); _ + 1) as print, ignore return value of print, then create function i=> i+1. Obviously this is all ridiculous but the fact that _ + 1 returns a function value is weird as **** to me and is my main hangup.
            If there is no sound in space, how come you can hear the lasers?
            ){ :|:& };:

            Comment


            • Yeah, I googled "scala map underscore", and what's happening is that map(i => println("Hi"); i + 1) passes the [println("Hi"); i + 1] function to the map function, while map(println("Hi"); _ + 1) executes [println("Hi")] and then passes [_ + 1] to the map function. Which makes sense and at the same time is stupid as hell.

              It might be possible to do something like map(println("Hi"); i => i + 1), but I'm much too lazy to open up an interpreter to find out of this works
              <p style="font-size:1024px">HTML is disabled in signatures </p>

              Comment


              • Okay I lied, I found an online interpreter, and Seq(1, 2, 3).map({println("Hi"); i => i + 1}) has the same output as Seq(1, 2, 3).map({println("Hi"); _ + 1}) (this interpreter requires the braces, mine doesn't, go figure). So now if another douchebag asks me about this I can immediately hang up on him before he can waste any more of my time.
                <p style="font-size:1024px">HTML is disabled in signatures </p>

                Comment


                • It is my opinion that if you actually abuse the syntax that way for any reason you're a bad person. Why the **** does _ + 1 evaluate to a lambda? Jesus christ. Yes I get that it's pattern matching. Still ridiculous.
                  If there is no sound in space, how come you can hear the lasers?
                  ){ :|:& };:

                  Comment


                  • It's a very stupid shorthand - (i => i + 1) is not that much more verbose than (_ + 1), so why introduce the shorthand at all?
                    <p style="font-size:1024px">HTML is disabled in signatures </p>

                    Comment


                    • I guess the semicolon operator evaluates both operands and returns the value on the right.
                      Graffiti in a public toilet
                      Do not require skill or wit
                      Among the **** we all are poets
                      Among the poets we are ****.

                      Comment


                      • Yeah, a code block executes the statements and returns the result of the last statement, e.g.
                        Code:
                        val x: Int = {
                            val temp = 20
                            20 * 2
                        }
                        println(x)
                        will print "40" - this is the same as
                        Code:
                        val x: Int = {
                            val temp = 20; 20 * 2
                        }
                        println(x)
                        The terminating semicolon is optional if the statements are on separate lines
                        Last edited by loinburger; November 5, 2014, 12:31.
                        <p style="font-size:1024px">HTML is disabled in signatures </p>

                        Comment


                        • Starting my new job on Monday - back to Java
                          <p style="font-size:1024px">HTML is disabled in signatures </p>

                          Comment


                          • Do you guys think of me when you write semicolon?
                            DISCLAIMER: the author of the above written texts does not warrant or assume any legal liability or responsibility for any offence and insult; disrespect, arrogance and related forms of demeaning behaviour; discrimination based on race, gender, age, income class, body mass, living area, political voting-record, football fan-ship and musical preference; insensitivity towards material, emotional or spiritual distress; and attempted emotional or financial black-mailing, skirt-chasing or death-threats perceived by the reader of the said written texts.

                            Comment


                            • I never stop thinking of you
                              <p style="font-size:1024px">HTML is disabled in signatures </p>

                              Comment


                              • DISCLAIMER: the author of the above written texts does not warrant or assume any legal liability or responsibility for any offence and insult; disrespect, arrogance and related forms of demeaning behaviour; discrimination based on race, gender, age, income class, body mass, living area, political voting-record, football fan-ship and musical preference; insensitivity towards material, emotional or spiritual distress; and attempted emotional or financial black-mailing, skirt-chasing or death-threats perceived by the reader of the said written texts.

                                Comment

                                Working...
                                X