Keynote presented at DevWeek (24th March 2015)
Trees. Both beautiful and useful. But we’re not talking about the green, oxygen-providing ones. As abstract structures we see trees all over the place – file systems, class hierarchies, ordered data structures, etc. They are neat and tidy, nested and hierarchical – a simple way of organising things; a simple way of breaking large things down into small things.
The problem is, though, that there are many things – from modest fragments of code up to enterprise-wide IT systems – that do not comfortably fit into this way of looking at the world and organising it. Software architecture, design patterns, class decomposition, performance, unit tests... all of these cut across the strict hierarchy of trees. This keynote will look at what this means for how we think and design systems, whether large or small.
Build a full-functioned virtual machine from scratch, when Brainfuck is used. Basic concepts about interpreter, optimizations techniques, language specialization, and platform specific tweaks.
Clean Code (Presentacion interna en Virtual Software)jmiguel rodriguez
Presentación en Virtual Software sobre el mítico libro Clean Code de Uncle Bob. Resumen de los primeros capitulos del libro para intentar concienciar sobre la necesidad de crear un buen código
Keynote presented at DevWeek (24th March 2015)
Trees. Both beautiful and useful. But we’re not talking about the green, oxygen-providing ones. As abstract structures we see trees all over the place – file systems, class hierarchies, ordered data structures, etc. They are neat and tidy, nested and hierarchical – a simple way of organising things; a simple way of breaking large things down into small things.
The problem is, though, that there are many things – from modest fragments of code up to enterprise-wide IT systems – that do not comfortably fit into this way of looking at the world and organising it. Software architecture, design patterns, class decomposition, performance, unit tests... all of these cut across the strict hierarchy of trees. This keynote will look at what this means for how we think and design systems, whether large or small.
Build a full-functioned virtual machine from scratch, when Brainfuck is used. Basic concepts about interpreter, optimizations techniques, language specialization, and platform specific tweaks.
Clean Code (Presentacion interna en Virtual Software)jmiguel rodriguez
Presentación en Virtual Software sobre el mítico libro Clean Code de Uncle Bob. Resumen de los primeros capitulos del libro para intentar concienciar sobre la necesidad de crear un buen código
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph.
Here I link up up some very useful material by Robert Norris (@tpolecat) and Martin Odersky (@odersky) to introduce Monad laws and reinforce the importance of checking the laws. E.g. while Option satisfies the laws, Try is not a lawful Monad: it trades the left identity law for the bullet-proof principle.
* ERRATA *
1) on the first slide the Kleisli composition signature appears three times, but one of the occurrences is incorrect in that it is (>=>)::(a->mb)->(b->mb)->(a->mc) whereas is should be (>=>)::(a->mb)->(b->mc)->(a->mc) - thank you Jules Ivanic.
This is the presentation on Syntactic Analysis in NLP.It includes topics like Introduction to parsing, Basic parsing strategies, Top-down parsing, Bottom-up
parsing, Dynamic programming – CYK parser, Issues in basic parsing methods, Earley algorithm, Parsing
using Probabilistic Context Free Grammars.
Its the first phase of the compiler,useful in generating lexemes ,tokens and matching of the pattern.Its helpful in solving GATE/ UGCNET problems.For more insight refer http://tutorialfocus.net/
From Word Embeddings To Document Distances
We present the Word Mover’s Distance (WMD), a novel distance function between text documents. Our work is based on recent results in word embeddings that learn semantically meaningful representations for words from local cooccurrences in sentences. The WMD distance measures the dissimilarity between two text documents as the minimum amount of distance that the embedded words of one document need to “travel” to reach the embedded words of another document. We show that this distance metric can be cast as an instance of the Earth Mover’s Distance, a well studied transportation problem for which several highly efficient solvers have been developed. Our metric has no hyperparameters and is straight-forward to implement. Further, we demonstrate on eight real world document classification data sets, in comparison with seven state-of-the-art baselines, that the WMD metric leads to unprecedented low k-nearest neighbor document classification error rates.
A Future for R: Parallel and Distributed Processing in R for Everyoneinside-BigData.com
In this deck from the 2018 European R Users Meeting, Henrik Bengtsson from the University of California San Francisco presents: A Future for R: Parallel and Distributed Processing in R for Everyone.
In this video from the European R Users Meeting, Henrik Bengtsson from the University of California San Francisco presents: A Future for R: Parallel and Distributed Processing in R for Everyone.
"The future package is a powerful and elegant cross-platform framework for orchestrating asynchronous computations in R. It's ideal for working with computations that take a long time to complete; that would benefit from using distributed, parallel frameworks to make them complete faster; and that you'd rather not have locking up your interactive R session."
Watch the video: https://wp.me/p3RLHQ-jJ4
Learn more: https://blog.revolutionanalytics.com/2019/01/future-package.html
Sign up for our insideHPC Newsletter: http://insidehpc.com/newsletter
The Bellman–Ford algorithm is an algorithm that computes shortest paths from a single source vertex to all of the other vertices in a weighted digraph.
Here I link up up some very useful material by Robert Norris (@tpolecat) and Martin Odersky (@odersky) to introduce Monad laws and reinforce the importance of checking the laws. E.g. while Option satisfies the laws, Try is not a lawful Monad: it trades the left identity law for the bullet-proof principle.
* ERRATA *
1) on the first slide the Kleisli composition signature appears three times, but one of the occurrences is incorrect in that it is (>=>)::(a->mb)->(b->mb)->(a->mc) whereas is should be (>=>)::(a->mb)->(b->mc)->(a->mc) - thank you Jules Ivanic.
This is the presentation on Syntactic Analysis in NLP.It includes topics like Introduction to parsing, Basic parsing strategies, Top-down parsing, Bottom-up
parsing, Dynamic programming – CYK parser, Issues in basic parsing methods, Earley algorithm, Parsing
using Probabilistic Context Free Grammars.
Its the first phase of the compiler,useful in generating lexemes ,tokens and matching of the pattern.Its helpful in solving GATE/ UGCNET problems.For more insight refer http://tutorialfocus.net/
From Word Embeddings To Document Distances
We present the Word Mover’s Distance (WMD), a novel distance function between text documents. Our work is based on recent results in word embeddings that learn semantically meaningful representations for words from local cooccurrences in sentences. The WMD distance measures the dissimilarity between two text documents as the minimum amount of distance that the embedded words of one document need to “travel” to reach the embedded words of another document. We show that this distance metric can be cast as an instance of the Earth Mover’s Distance, a well studied transportation problem for which several highly efficient solvers have been developed. Our metric has no hyperparameters and is straight-forward to implement. Further, we demonstrate on eight real world document classification data sets, in comparison with seven state-of-the-art baselines, that the WMD metric leads to unprecedented low k-nearest neighbor document classification error rates.
A Future for R: Parallel and Distributed Processing in R for Everyoneinside-BigData.com
In this deck from the 2018 European R Users Meeting, Henrik Bengtsson from the University of California San Francisco presents: A Future for R: Parallel and Distributed Processing in R for Everyone.
In this video from the European R Users Meeting, Henrik Bengtsson from the University of California San Francisco presents: A Future for R: Parallel and Distributed Processing in R for Everyone.
"The future package is a powerful and elegant cross-platform framework for orchestrating asynchronous computations in R. It's ideal for working with computations that take a long time to complete; that would benefit from using distributed, parallel frameworks to make them complete faster; and that you'd rather not have locking up your interactive R session."
Watch the video: https://wp.me/p3RLHQ-jJ4
Learn more: https://blog.revolutionanalytics.com/2019/01/future-package.html
Sign up for our insideHPC Newsletter: http://insidehpc.com/newsletter
Hierarchical free monads and software design in fpAlexander Granin
I invented the approach I call "Hierarchical Free Monads". It helps to build applications in Haskell with achieving all the needed code quality requirements. I tested this approach in several real world projects and companies, and it works very well.
This is an introductory SCALA workshop for a JAVA developer. Hence, we're going to explore 'functional' side of the language. SCALA is 50% OOP (Object-Oriented-Programming) and 50% FP (Functional-Programming). Main principles of FP are discussed, such as: tail-recursion, currying, pure-functions, lazy evaluation etc.) along with some examples in code.
We look at the basic keywords and constructs in SCALA.
In the end SCALA tools which are helpful for developers are listed.
Spark Training Institutes: kelly technologies is the best Spark class Room training institutes in Bangalore. Providing Spark training by real time faculty in Bangalore.
A short introduction to reproducible research, reproducibility with R, Docker, and all together for reproducible research using R and Docker containers. Includes demos of Rocker and containerit.
These are the outline slides that I used for the Pune Clojure Course.
The slides may not be much useful standalone, but I have uploaded them for reference.
This presentation is an introduction to Dotty / Scala 3.
It covers the features which I deem most important for Scala developers.
For detailed information see the [Dotty documentation](https://dotty.epfl.ch/docs/index.html).
This is an quick introduction to Scalding and Monoids. Scalding is a Scala library that makes writing MapReduce jobs very easy. Monoids on the other hand promise parallelism and quality and they make some more challenging algorithms look very easy.
The talk was held at the Helsinki Data Science meetup on January 9th 2014.
Similar to Metaprogramming and Reflection in Common Lisp (20)
Pharo is an open-source live-programming environment and programming language started in 2008. The official web site is http://www.pharo-project.org. By providing a stable and small core system, excellent dev tools, and maintained releases, Pharo is an attractive platform to build and deploy mission-critical applications.
Architecture-Driven Programming for Sense/Compute/Control ApplicationsDamien Cassou
Software architectures have long been used to design the structure of a software and to impose constraints on the software implementation. Existing approaches are capable of formally verifying properties on these constraints at the design stage. However, few approaches verify that a given implementation conforms to the software architecture constraints, and even fewer use these constraints to guide developers during implementation.
In this presentation, we propose a generative approach to enforce the software architecture constraints during implementation. Toward this goal, we focus on a specific application area, Sense/Compute/Control applications, which are applications that interact with a physical or virtual environment. Our work is based on a domain-specific architectural pattern dedicated to SCC applications. The data-flow of this pattern is restricted by interaction constraints. For specifying such constraints, we propose a dedicated language of behavioral contracts, adapted to our architectural pattern. These behavioral contracts are used for automatically generating a dedicated programming framework. This framework enforces the conformance of the resulting implementation with the software architecture constraints and provides guidance to developers.
El Puerto de Algeciras continúa un año más como el más eficiente del continente europeo y vuelve a situarse en el “top ten” mundial, según el informe The Container Port Performance Index 2023 (CPPI), elaborado por el Banco Mundial y la consultora S&P Global.
El informe CPPI utiliza dos enfoques metodológicos diferentes para calcular la clasificación del índice: uno administrativo o técnico y otro estadístico, basado en análisis factorial (FA). Según los autores, esta dualidad pretende asegurar una clasificación que refleje con precisión el rendimiento real del puerto, a la vez que sea estadísticamente sólida. En esta edición del informe CPPI 2023, se han empleado los mismos enfoques metodológicos y se ha aplicado un método de agregación de clasificaciones para combinar los resultados de ambos enfoques y obtener una clasificación agregada.
An astonishing, first-of-its-kind, report by the NYT assessing damage in Ukraine. Even if the war ends tomorrow, in many places there will be nothing to go back to.
04062024_First India Newspaper Jaipur.pdfFIRST INDIA
Find Latest India News and Breaking News these days from India on Politics, Business, Entertainment, Technology, Sports, Lifestyle and Coronavirus News in India and the world over that you can't miss. For real time update Visit our social media handle. Read First India NewsPaper in your morning replace. Visit First India.
CLICK:- https://firstindia.co.in/
#First_India_NewsPaper
Here is Gabe Whitley's response to my defamation lawsuit for him calling me a rapist and perjurer in court documents.
You have to read it to believe it, but after you read it, you won't believe it. And I included eight examples of defamatory statements/
‘वोटर्स विल मस्ट प्रीवेल’ (मतदाताओं को जीतना होगा) अभियान द्वारा जारी हेल्पलाइन नंबर, 4 जून को सुबह 7 बजे से दोपहर 12 बजे तक मतगणना प्रक्रिया में कहीं भी किसी भी तरह के उल्लंघन की रिपोर्ट करने के लिए खुला रहेगा।
01062024_First India Newspaper Jaipur.pdfFIRST INDIA
Find Latest India News and Breaking News these days from India on Politics, Business, Entertainment, Technology, Sports, Lifestyle and Coronavirus News in India and the world over that you can't miss. For real time update Visit our social media handle. Read First India NewsPaper in your morning replace. Visit First India.
CLICK:- https://firstindia.co.in/
#First_India_NewsPaper
03062024_First India Newspaper Jaipur.pdfFIRST INDIA
Find Latest India News and Breaking News these days from India on Politics, Business, Entertainment, Technology, Sports, Lifestyle and Coronavirus News in India and the world over that you can't miss. For real time update Visit our social media handle. Read First India NewsPaper in your morning replace. Visit First India.
CLICK:- https://firstindia.co.in/
#First_India_NewsPaper
1. Metaprogramming and Reflection
Common Lisp
Damien C ASSOU
Hasso-Plattner-Institut Potsdam
Software Architecture Group
Prof. Dr. Robert Hirschfeld
http://www.hpi.uni-potsdam.de/swa/
5. Metaprogramming and Reflection
Creating Functions
> (defun mult2 (x)
"Multiplies x by 2"
(* x 2))
mult2
defun is itself a function, it creates functions
> (mult2 3)
6
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 5 / 56
6. Metaprogramming and Reflection
Studying Functions
> #’mult2
#<FUNCTION mult2>
> (describe #’mult2)
(defun mult2 (x)
"Multiplies x by 2"
(* x 2))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 6 / 56
8. Metaprogramming and Reflection
Summary
In Lisp it is possible to:
define new functions,
retrieve a function by name,
reference a function from a variable,
call a function from a variable.
This is very similar to pointer manipulation in C
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 8 / 56
9. Metaprogramming and Reflection
Function Pointer Manipulation in C
int mult2 (int c) {
return c * 2;
}
int main(void) {
int (*fmult2) (int) = mult2;
(*fmult2)(3);
}
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 9 / 56
10. Metaprogramming and Reflection
Generating new Functions
> (get-source ’mult2)
(nil nil
(defun mult2 (x)
"Multiplies x by 2"
(* x 2)))
requires ibcl
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 10 / 56
11. Metaprogramming and Reflection
Generating new Functions
> (defvar smult2
(third (get-source ’mult2)))
smult2
> smult2
(defun mult2 (x)
"Multiplies x by 2"
(* x 2))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 11 / 56
12. Metaprogramming and Reflection
Generating new Functions
> (first smult2)
defun
> (second smult2)
mult2
> (third smult2)
(x)
> (fourth smult2)
"Multiplies x by 2"
> (fifth smult2)
(* x 2)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 12 / 56
13. Metaprogramming and Reflection
Generating new Functions
> (defvar smult10
(copy-list smult2))
smult10
> (nsubstitute 10 2 (fifth smult10))
nil
> smult10
(defun mult2 (x)
"Multiplies x by 2"
(* x 10))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 13 / 56
14. Metaprogramming and Reflection
Generating new Functions
> smult10
(defun mult2 (x)
"Multiplies x by 2"
(* x 10))
> (nsubstitute ’mult10 ’mult2
smult10)
(defun mult10 (x)
"Multiplies x by 2"
(* x 10))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 14 / 56
15. Metaprogramming and Reflection
Generating new Functions
> smult10
(defun mult10 (x)
"Multiplies x by 2"
(* x 10))
> (setf (fourth smult10)
(cl-ppcre:regex-replace "2"
(fourth smult10) "10"))
"Multiplies x by 10"
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 15 / 56
16. Metaprogramming and Reflection
Generating new Functions
> smult10
(defun mult10 (x)
"Multiplies x by 10"
(* x 10))
> (eval smult10)
mult10
> (mult10 3)
30
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 16 / 56
17. Metaprogramming and Reflection
Summary
A function definition in Lisp is a list.
This list can be studied like any list.
New functions can be created from a list.
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 17 / 56
18. Metaprogramming and Reflection
Beyond Functions
How would you implement while that executes its
body as long as its condition stays true?
> (while condition body)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 18 / 56
19. Metaprogramming and Reflection
The While Construct
> (setq i 10)
> (while (/= i 0)
(decf i)
(format t "i is now: ~s~%" i))
i is now: 9
i is now: 8
i is now: 7
...
i is now: 2
i is now: 1
i is now: 0
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 19 / 56
20. Metaprogramming and Reflection
The While Construct: Using Loop
> (while (/= i 0)
(decf i)
(format t "i is now: ~s~%" i))
> (loop
(if (not (/= i 0))
(return)
(progn
(decf i)
(format t "i = ~s~%" i))))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 20 / 56
21. Metaprogramming and Reflection
The While Construct: Function
> (defun while (test &rest body)
(loop
(if (not test)
(return)
(progn body))))
> (while (/= i 0)
(decf i)
(format t "i is now: ~s~%" i))
doesn’t work because parameters are evaluated
immediately
> (while t nil)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 21 / 56
22. Metaprogramming and Reflection
Function Evaluation in C
int f(int c){printf("fn");return c;}
int g(int c){printf("gn");return c;}
int h(int c){printf("hn");return c;}
int main(void) {
f(g(h(1)));
}
h
g
f
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 22 / 56
23. Metaprogramming and Reflection
The While Construct: Function
> (defun while (test &rest body)
(loop
(if (not test)
(return)
(progn body))))
> (while (/= i 0)
(decf i)
(format t "i is now: ~s~%" i))
doesn’t work because parameters are evaluated
immediately
> (while t nil)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 23 / 56
24. Metaprogramming and Reflection
The While Construct: Function
> (while ’(/= i 0)
’(decf i)
’(format t "i is now: ~s~%" i))
> (defun while (test &rest body)
(loop
(if (not (eval test))
(return)
(mapcar #’eval body))))
works, but using while is less readable than intended
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 24 / 56
25. Metaprogramming and Reflection
Summary
Arguments of functions are evaluated first.
To prevent evaluation, use quote (or ’ ).
Use eval to evaluate an expression.
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 25 / 56
26. Metaprogramming and Reflection
The While Construct: Macro
> (loop
(if (not (/= i 0))
(return)
(progn
(decf i)
(format t "i = ~s~%" i))))
> (defmacro while (test &body body)
(list ’loop
(list ’if (list ’not test)
(list ’return)
(cons ’progn body))))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 26 / 56
27. Metaprogramming and Reflection
The While Construct: Macro
> (loop
(if (not (/= i 0))
(return)
(progn
(decf i)
(format t "i = ~s~%" i))))
> (defmacro while (test &body body)
‘(loop
(if (not ,test)
(return)
(progn ,@body))))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 27 / 56
28. Metaprogramming and Reflection
Macros
Macros are programs that write programs
they return lists representing Lisp code.
they don’t evaluate their arguments.
they are evaluated at compile time.
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 28 / 56
29. Metaprogramming and Reflection
Creating an OO language
> (makeClass Speaker (name)
(makeMethod speak (sentence)
(format t
"Listen all of you: ~s~%"
sentence)))
> (defvar alex (new ’Speaker "Alex"))
> (call alex ’speak "Hello World!")
Listen all of you: "Hello World!"
> (getinstvar alex ’name)
Alex
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 29 / 56
30. Metaprogramming and Reflection
Creating an OO language
> (makeClass Speaker ()
(makeMethod "..."))
A class is composed of:
a name,
some instance variables,
and some method definitions.
> (defstruct cls
name
vars
mths)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 30 / 56
31. Metaprogramming and Reflection
Creating an OO language
> (makeClass Speaker ()
(makeMethod "..."))
> (defmacro makeClass (name iVars
&body meths)
‘(push
(make-cls
:name ’,name
:vars ’,iVars
:mths
’,(mapcar #’eval meths))
*classes*))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 31 / 56
32. Metaprogramming and Reflection
Creating an OO language
> (makeMethod speak (sentence)
(format t "..." sentence))
A method is composed of:
a name,
some parameters,
a body
> (defstruct mth
name
lmbd)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 32 / 56
33. Metaprogramming and Reflection
Creating an OO language
> (makeMethod speak (sentence)
(format t "..." sentence))
> (defmacro makeMethod (name
argNames &body body)
‘(make-mth
:name ’,name
:lmbd (lambda ,argNames
,@body)))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 33 / 56
34. Metaprogramming and Reflection
Creating an OO language
> (new ’Speaker "Alex")
An object is composed of:
a reference to its class,
some values for its instance variables
> (defstruct obj
cls
values)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 34 / 56
35. Metaprogramming and Reflection
Creating an OO language
> (call alex ’speak "Hello World!")
Listen all of you: "Hello World!"
A call is a function with:
the receiver object,
a method name,
and a list of parameters.
(defun call (obj name &rest params)
"...")
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 35 / 56
36. Metaprogramming and Reflection
Creating an OO language
(defun call (obj name &rest params)
(let* ((cls (obj-cls obj))
(mth (getMethod cls name)))
(apply (mth-lmbd mth)
params)))
(defun getMethod (cls name)
(find name (cls-mths cls)
:key #’mth-name))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 36 / 56
37. Metaprogramming and Reflection
Creating an OO language
> (getinstvar alex ’name)
Alex
Looking for an instance variable value from its name
involves:
getting the position of the name in the list of all
instance variables of the class,
taking the value at this position in the list of all
values of the object.
class: varname1 varname2 . . . varnamen
object: value1 value2 ... valuen
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 37 / 56
38. Metaprogramming and Reflection
Creating an OO language
class: varname1 varname2 . . . varnamen
object: value1 value2 ... valuen
(defun getInstVar (obj name)
(let* ((cls (obj-cls obj))
(vars (cls-vars cls))
(pos (position name vars)))
(nth pos (obj-values obj))))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 38 / 56
39. Metaprogramming and Reflection
Handling this
An object must be able to get its instance variables
and call methods by using this .
> (makeClass Speaker (name)
(makeMethod getName ()
(getInstVar ’this ’name)))
> (call alex ’getname)
Alex
This requires the system to keep track of the current
object.
> (defparameter *cur-obj* nil)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 39 / 56
40. Metaprogramming and Reflection
Handling this
(defun getInstVar (obj name)
(let* ((theObj
(if (equal obj ’this)
*cur-obj*
obj))
(cls (obj-cls theObj))
(vars (cls-vars cls))
(pos (position name vars)))
(nth pos (obj-values theObj))))
When is ∗cur−obj∗ updated? Before it is used!
As this is only used when a method is executed, the
method call needs to do the updating job.
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 40 / 56
41. Metaprogramming and Reflection
Handling this
The method call needs to do the updating job:
(defun call (obj name &rest params)
(let* ((cls (obj-cls obj))
(mth (getMethod cls name))
(*cur-obj* obj))
(apply (mth-lmbd mth)
params)))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 41 / 56
42. Metaprogramming and Reflection
Handling this
We also want to pass this as first argument to call :
(defun call (obj name &rest params)
(let* ((theObj
(if (equal obj ’this)
*cur-obj*
obj))
(cls (obj-cls theObj))
(mth (getMethod cls name)))
(setf *cur-obj* theObj)
(apply (mth-lmbd mth)
params)))
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 42 / 56
43. Metaprogramming and Reflection
Creating an OO language
Possible improvements:
setting of instance variables
inheritance
constructors
dedicated syntax
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 43 / 56
44. Metaprogramming and Reflection
Creating Domain-Specific Languages
(makeClass Speaker (name)
(makeMethod speak (s)
(format t "I say: ~a" s))
(makeMethod getName ()
(call ’this ’speak "hi!")
(getInstVar ’this ’name)))
(makeMethod getName ()
{c speak "hi!"}
{i name})
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 44 / 56
46. Metaprogramming and Reflection
Summary
Lisp has powerful functions to manipulate lists.
Lisp source code is made of lists.
As a result, meta-programming is made easy.
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 46 / 56
47. Metaprogramming and Reflection
Summary
Macros
can be used to create source code,
don’t evaluate their arguments,
are evaluated at compile time.
Macros are programs that write programs.
Macros can also be used to install a new syntax.
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 47 / 56
48. Metaprogramming and Reflection
A Glimpse at CLOS
> (defclass circle ()
(radius center))
#<standard-class circle>
> (make-instance ’circle)
#<circle {B9CD249}>
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 48 / 56
49. Metaprogramming and Reflection
A Glimpse at CLOS
> (defclass circle ()
((radius :accessor circle-radius)
(center :accessor circle-center)))
#<standard-class circle>
> (setf c (make-instance ’circle))
#<circle {ABC4629}>
> (setf (circle-radius c) 6)
6
> (circle-radius c)
6
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 49 / 56
50. Metaprogramming and Reflection
A Glimpse at CLOS
> (defclass circle ()
((radius :accessor circle-radius
:initarg :radius)
(center :accessor circle-center
:initarg :center)))
#<standard-class circle>
> (setf c (make-instance ’circle
:radius 6))
#<circle {AC30D31}>
> (circle-radius c)
6
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 50 / 56
51. Metaprogramming and Reflection
A Glimpse at CLOS
> (defmethod area ((c circle))
(* pi (expt (circle-radius c) 2)))
#<standard-method area (circle)>
> (setf c (make-instance ’circle
:radius 6))
#<circle {ACC00F1}>
> (area c)
113.09733552923255d0
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 51 / 56
52. Metaprogramming and Reflection
Auxiliary methods
> (defmethod area ((c circle))
(* pi (expt (circle-radius c) 2)))
> (defmethod area :before ((c circle))
(format t "I’m tired..."))
> (area c)
I’m tired...
113.09733552923255d0
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 52 / 56
53. Metaprogramming and Reflection
Auxiliary methods
(setf cache nil)
(defun from-cache (c)
(find (circle-radius c) cache
:key #’car))
(defun to-cache (c area)
(push (cons (circle-radius c) area)
cache)
area)
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 53 / 56
54. Metaprogramming and Reflection
Auxiliary methods
(defmethod area :around ((c circle))
(let ((value (from-cache c)))
(if value
(progn
(princ "Using the cache :-)")
(cdr value))
(progn
(princ "So tired...")
(to-cache c (call-next-method))))))
> (area c)
So tired...I’m tired...
113.09733552923255d0
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 54 / 56
55. Metaprogramming and Reflection
Auxiliary methods
> (area c)
So tired...I’m tired...
113.09733552923255d0
> (area c)
Using the cache :-)
113.09733552923255d0
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 55 / 56
56. Metaprogramming and Reflection
Acknowledgments
Thanks to #lisp for all their help:
akovalenko pjb
antifuchs prxb
H4ns ThomasH
nikodemus
These slides were created with a Common Lisp
DSL: https://github.com/DamienCassou/DSLides
Software Architecture Group (www.hpi.uni-potsdam.de/swa) 2006–present 56 / 56