09 decembrie 2014

Eopl3: chapter 9.4 solution to exercise 9.13

Adding final methods (which cannot be overridden).
--------------------------------------------
> lang.scm_____________________________________________________________

        (method-decl
            ("final" "method" identifier
                  "("  (separated-list identifier ",") ")" ; method formals
              expression)
        a-method-decl-final)

;; So we created a new type of method-decl, called a-method-decl-final

> classes.scm___________________________________________________________

;; First we add a new field for the a-method data structure

  (define-datatype method method?
    (a-method
      (vars (list-of symbol?))
      (body expression?)
      (super-name symbol?)
      (field-names (list-of symbol?))
      (isFinal boolean?)
    ))

;; We check for method-decl in 2 cases (regular one + the one added above).
;; In the first case, in case the super-class is not object, we check if the method already existed
;; If yes, we check its status: final or not. If final, don't override, else override;
;; when we override, we set the final field to #f
;; In the 2nd case, same steps basically, but when we override, we set the final field to #t (final)

  (define method-decls->method-env
    (lambda (m-decls super-name field-names)
      (map
        (lambda (m-decl)
          (cases method-decl m-decl
            (a-method-decl (method-name vars body)        ;; create a non-final method
                (if (eq? super-name 'object) (list method-name (a-method vars body super-name field-names #f))
                ;; else check if it didnt exist before in the super class, as a final
                (let ((possibleMethod (find-method super-name method-name)))
                    (if (eq? possibleMethod #f)
                        (list method-name (a-method vars body super-name field-names #f))    ;;  not found before, create the method
                        ;; found before so check if it's final
                        (cases method possibleMethod (a-method (vars body super fnames isFinal)   
                            (if (eq? isFinal #t)
                                (list 'xxx (a-method vars body super-name field-names #f))    ;; make a dummy, don't overwrite
                                (list method-name (a-method vars body super-name field-names #f))    ;; else overwrite
                            )))
                ))))
            (a-method-decl-final (method-name vars body)                ;; final method
                (if (eq? super-name 'object) (list method-name (a-method vars body super-name field-names #t))
                ;; else check if it didnt exist before in the super class, as a final
                (let ((possibleMethod (find-method super-name method-name)))
                    (if (eq? possibleMethod #f)
                        (list method-name (a-method vars body super-name field-names #t))    ;;  not found before, create the method
                        ;; found before so check if it's final
                        (cases method possibleMethod (a-method (vars body super fnames isFinal)   
                            (if (eq? isFinal #t)
                                (list 'xxx (a-method vars body super-name field-names #t))    ;; make a dummy, don't overwrite
                                (list method-name (a-method vars body super-name field-names #t))    ;; else overwrite
                            )))
                )))
        )))
        m-decls)))

;; a small change in find-method

  (define find-method
    (lambda (c-name name)
      (let ((m-env (class->method-env (lookup-class c-name))))
        (let ((maybe-pair (assq name m-env)))
          (if (pair? maybe-pair) (cadr maybe-pair)
            (begin
                (report-method-not-found name) #f)
    ;; ex. 9.13
    )))))


> interp.scm____________________________________________________________

;; a small change to accommodate the new field isFinal

  (define apply-method                   
    (lambda (m self args)
      (cases method m
        (a-method (vars body super-name field-names isFinal) ;; ex 9.13
          (value-of body
            (extend-env vars (map newref args)
              (extend-env-with-self-and-super
                self super-name
                (extend-env field-names (object->fields self)
                  (empty-env)))))))))


__________           _     Test... exemplu_____                ________________

class c1 extends object
    field x
    field y
    method initialize ()
        begin
         set x = 11;
         set y = 12
        end
    final method m1 (a)
        -(x,a)

class c2 extends c1
    method initialize ()
        begin
         super initialize()
        end
    method m1 (a)
        -(a,1)

let o2 = new c2() in send o2 m1(55)        % m1 cannot be overwritten, should write -44

Cursuri noi online care mi-au atras atentia

01 decembrie 2014

ssh fara parola (config & debug)

Presupunem ca avem 2 calc, x1@hostx si y1@hosty.

Pt hostx: in ~/.ssh stergem continutul (daca exista) din fisierele authorized_keys & known_hosts, pentru a o lua de la capat. Daca x1 != y1, se face acelasi lucru si pt hosty.

Rulam pe hostx:
su - x1
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa

Se transfera fisierul id_dsa.pub pe hosty (daca x1 != y1) si se pune continutul in authorized_keys. Daca x1 == y1, atunci se executa pe oricare dintre hostx sau
hosty:
cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

Rulam pe hosty:
su -y1
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa_y1

Idem ca mai sus (transferul id_dsa_y1.pub pe hostx), si se executa pe hostx ceva asemanator cu:
cat ~/.ssh/id_dsa_y1.pub >> ~/.ssh/authorized_keys

!! id_dsa_y1 nu trebuie suprascris peste id_dsa, daca x1 == x2

Acum x1 se poate conecta la y1 fara parola, cu:
ssh y1@hosty
Si vice-versa.

Debug:
* verificare permisiuni pe x1 si y1:
- home directory, directorul .ssh si fisierul authorized_keys sa poata fi scrise (w) doar de owner; citite cel putin de owner (r);
- cheile private trebuie sa poate fi citite si scrise doar de owner, iar cheile publice trebuie sa poata sa fie citite si din exterior (non-owner)
- permisiunile se modifica cu: chmod [value] [fisier]

* daca tot nu merge fara parola, se pot activa mesajele in ssh (verbose) prin:
ssh -vv y1@hosty
Acestea explica mecanismul de conectare si ce anume nu a mers.

09 noiembrie 2014

Grading situation

Cand si studentii s-au plictisit de atatea instructiuni si detalii de tinut cont pentru tema... 
Cand in sfarsit inteleg durerea asistentului care trebuie sa verifice de 30 ori aceleasi si aceleasi instructiuni....
Un student trimite o rezolvare impecabila cu urmatoarele comentarii:
______________________________

# Description: Today we'll be reading a data file of information on the sales of a movie before printing out the information in a tedious-for-the-programmer fashion
# for your viewing pleasure. Afterwards we kick things over to Mike and Stan on "Who Bought the Most Tickets", America's #1 Show. Hopefully I won't get docked.

------------------ urmeaza codul sursa, dupa care:

#Make it look nicer.......or less jumbled since technically that print statement up there already made it look nicer
print()
#I suppose you could say more more nicer, or aesthetic improvement if you want a reason to use the word aesthetic.
print()
#I don't see why you wouldn't. Say it once and see if just doesn't stick....aesthetic.
#Yes I realize the previous 3 comments and this one explaining those 3 comments have little to do with actual code.


#And this one here isn't even commenting on anything. It's just here......floating along...like clouds.

------------------ urmeaza obisnuitul print al rezultatelor -----------
------------------ sau nu:

#I hope I don't get docked for this, but here goes:
print("Statistics (Game Show in this case)")
print("_____________________________________________________________________________________________________________")
print("Total tickets sold: ", total)
print()
print('Stan: "Ladies and Gentlemen, it has been an excitig show so far. Now to announce our winners!!"')
print()
print('Mike: "First up, our award for the most purchased adult tickets goes to: ', maxAdult, 'with a total of', maxAdultTickets,'"')
print()
print('Stan: "And last, but definitely not least, our award for the most purchased child tickets goes to: "')
print('Mike: "Drumroll please!!!!"')
print()
print('Stan: "Ladies and gentlemen!!! YOUR NEW LIGHTWEIGHT HEAVYWEIGHT ADULT CHILDRENS CHAMP OF THE WORLD!!! THE ONE AND ONLY TRUE KING. THE GLORIOUS..."')
print()
print('Mike: "Umm...Stan?"')
print()
print('Stan: "Hmm?, Oh yes, congratulations', maxChild, 'for purchasing the most child tickets with a total of', maxChildTickets, '"')
print()
print('Mike: "Alright folks, that concludes the series finale of Who Bought the Most Tickets. Goodbye and good riddance"')
print("____________________________________________________________________________________________________________")

-------------------- studentul doreste interactiune criptata:

#I'm curious, and apparently I have so little to do in life that I'm about to make a commented out if statement that may or may not receive a reply.
#From you of course

#if (you read these comments):
    #if (You at least cracked a smile and I got a perfect score):
        #Put "Brilliant!!!" (yes, 3 exclamation points) in the comments when you grade

    #elif (You at least cracked a smile and I got close to a perfect score):
        #Write "So Close!!" (this time 2 exclamation points) in the comments when you grade

    #else:
        #Anything beyond the first two conditions is impossible.

#else:
    #Well then there isn't much point to this else statement now is there


---------------- feedback-ul meu ----------------------------------

+0p: output is correct, aesthetic
-0p: it would be great if you stick only with the relevant stuff to output, I appreciate the sense of humor

--------------- conflictul interior intre dorinta de a pastra lucrurile la obiect si incurajarea spicing up the boring grading time este castigat de primul