SlideShare a Scribd company logo
1 of 12
Download to read offline
Метапрограммирование в CL
 "Lisp isn't a language, it's a building material."
                                       - Alan Kay




          ● Макросы
          ● Макросы чтения

          ● Макросы компилятора

          ● ...
Квазицитирование
(defmacro nif (expr positive zero negative)
    (let ((var (gensym)))
          `(let ((,var ,expr))
               (cond
                     ((plusp ,var) ,positive)
                     ((zerop ,var) ,zero)
                     (t ,negative)))))

(nif (- (* b b) (* 4 a c))
      2
      1
      0)

(LET ((#:G624 (- (* B B) (* 4 A C))))
 (COND ((PLUSP #:G624) 2) ((ZEROP #:G624) 1) (T 0)))
Анафорические макросы
(defmacro aif (condition then &optional else)
    `(let ((it ,condition))
         (if it
                ,then
                ,else)))

(aif (load-data))
      (pprint it)
      (print "No data loaded."))

awhen, awhile, aand, alambda...

(aand
    (load-data)
    (take-field it)
    (do-smth it))
Декораторы
(defmacro defun/decorated ((&rest decorator) name (&rest params)
                                     &body body)
    `(defun ,name (,@params)
             (,@decorator
                 (lambda () ,@body)
                 ,@params)))

(defun decorator (x f &rest params)
    (format t "decorator ~A ~{~A ~}" x params)
    (funcall f))

(defun/decorated
    (decorator "smth")
    square (x)
             (* x x))
Макросы пишут макросы: TCO
     (defun fact (n acc)
         (if (zerop n)
                   acc
                   (fact (- n 1) (* n acc))))

     (defmacro defun/tco (name (&rest params) &body body)
         `(defun ,name (,@params)
                  (macrolet ((,name (&rest args)
                     `(progn
                          (psetq
                               ,@(mapcan (lambda (p v)
                                   (list p v))
                                   ',params args))
                          (go :label))))
                     (tagbody
                          :label
                          (return-from ,name (progn ,@body))))))

     (defun/tco fact/tco (n acc)
         (if (zerop n)
                   acc
                   (fact/tco (- n 1) (* n acc))))
EDSL
● CLOS (Common Lisp Object System)
● ITERATE




    (iter (for i from 1)
          (for a in some-list)
          (collect (cons i a)))

● ContextL (AOP)
● Chtml-matcher



(<tbody nil
   (tr nil (<a ((name ?post-num)))
      (tr nil)
      (tr nil (?post-body <div ((id "post_message_?"))))))
Макросы чтения
(set-dispatch-macro-character ## #$
   #'(lambda (stream char arg)
       (parse-integer (coerce
          (loop
               for ch = (peek-char nil stream nil nil t)
               while (or (digit-char-p ch) (eql ch #_))
               do (read-char stream t nil t)
               if (digit-char-p ch)
                   collect ch)
          'string))))

#$1_000_000
Макросы чтения
(let ((foo 1))
    #Uhttp://www.example.com/widget/{foo}/parts)

"http://www.example.com/widget/1/parts"


(uri-template-bind (#Uhttp://www.example.com/{part}/{number})
    "http://www.example.com/widget/1"
 (list part (parse-integer number) %uri-host))

("widget" 1 "www.example.com")
Макросы чтения
(set-dispatch-macro-character ## #/
   (lambda (stream char arg)
       (let ((pattern (coerce
               (loop
                    for ch = (read-char stream t nil t)
                    until (eql ch #/)
                    collect ch)
               'string)))
           `(lambda (&rest args)
               (apply #'cl-ppcre:scan ,pattern args)))))

          (#/[a-z]+/ str)

          ((lambda (&rest args)
              (apply #'cl-ppcre:scan "[a-z]+" args)) str)

          (cl-ppcre:scan "[a-z]+" str)
Макросы компилятора
(format stream control-string arg1...)

(funcall (formatter control-string) stream arg1 ...)

(lambda (stream &rest arguments)
    (apply #'format stream control-string arguments))

(formatter "Hello, ~A")

(LAMBDA (STREAM #:FORMAT-ARG633)
   (WRITE-STRING "Hello, " STREAM)
   (PRINC #:FORMAT-ARG633 STREAM))

(formatter "~{~A~%~}")

(LAMBDA (STREAM #:FORMAT-ARG636)
   (LET ((ARGS #:FORMAT-ARG636))
       (LOOP
           (WHEN (NULL ARGS) (RETURN))
           (PRINC (POP ARGS) STREAM)
           (TERPRI STREAM))))
Макросы компилятора
                                      CL-PPCRE
(define-compiler-macro scan (&whole form
                                 &environment env
                                 regex target-string
                                 &rest rest)
    (cond
         ((constantp regex env)
               `(scan (load-time-value (create-scanner ,regex)) ,target-string ,@rest))
         (t form)))
Ссылки
● Paul Graham «On Lisp» http://www.paulgraham.com/onlisp.html
● Doug Hoyte «LOL» http://letoverlambda.com/

● CL-PPCRE http://weitz.de/cl-ppcre/

● Iterate http://common-lisp.net/project/iterate/

● ContextL http://common-lisp.net/project/closer/contextl.html

● chtml-matcher http://common-lisp.net/project/chtml-matcher/

● uri-template http://common-lisp.net/project/uri-template/

More Related Content

What's hot

Virtual machine and javascript engine
Virtual machine and javascript engineVirtual machine and javascript engine
Virtual machine and javascript engineDuoyi Wu
 
C c++-meetup-1nov2017-autofdo
C c++-meetup-1nov2017-autofdoC c++-meetup-1nov2017-autofdo
C c++-meetup-1nov2017-autofdoKim Phillips
 
Modern c++ Memory Management
Modern c++ Memory ManagementModern c++ Memory Management
Modern c++ Memory ManagementAlan Uthoff
 
When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)Sylvain Hallé
 
Gaucheで本を作る
Gaucheで本を作るGaucheで本を作る
Gaucheで本を作るguest7a66b8
 
Macroprocessor
MacroprocessorMacroprocessor
Macroprocessorksanthosh
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolvedtrxcllnt
 
Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Steffen Wenz
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
Cluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in PracticeCluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in PracticeSteffen Wenz
 
Javascript compilation execution
Javascript compilation executionJavascript compilation execution
Javascript compilation executionFanis Prodromou
 
VLSI Sequential Circuits II
VLSI Sequential Circuits IIVLSI Sequential Circuits II
VLSI Sequential Circuits IIGouthaman V
 
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)Sylvain Hallé
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015NAVER / MusicPlatform
 
Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Sylvain Hallé
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJSKyung Yeol Kim
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage CollectionEelco Visser
 

What's hot (20)

Virtual machine and javascript engine
Virtual machine and javascript engineVirtual machine and javascript engine
Virtual machine and javascript engine
 
C c++-meetup-1nov2017-autofdo
C c++-meetup-1nov2017-autofdoC c++-meetup-1nov2017-autofdo
C c++-meetup-1nov2017-autofdo
 
Modern c++ Memory Management
Modern c++ Memory ManagementModern c++ Memory Management
Modern c++ Memory Management
 
When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)
 
Gaucheで本を作る
Gaucheで本を作るGaucheで本を作る
Gaucheで本を作る
 
Macroprocessor
MacroprocessorMacroprocessor
Macroprocessor
 
Exploiting vectorization with ISPC
Exploiting vectorization with ISPCExploiting vectorization with ISPC
Exploiting vectorization with ISPC
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
 
Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
Cluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in PracticeCluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in Practice
 
Javascript compilation execution
Javascript compilation executionJavascript compilation execution
Javascript compilation execution
 
VLSI Sequential Circuits II
VLSI Sequential Circuits IIVLSI Sequential Circuits II
VLSI Sequential Circuits II
 
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)BeepBeep 3: A declarative event stream query engine (EDOC 2015)
BeepBeep 3: A declarative event stream query engine (EDOC 2015)
 
Clojure+ClojureScript Webapps
Clojure+ClojureScript WebappsClojure+ClojureScript Webapps
Clojure+ClojureScript Webapps
 
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
 
Scope and closures
Scope and closuresScope and closures
Scope and closures
 
Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings Activity Recognition Through Complex Event Processing: First Findings
Activity Recognition Through Complex Event Processing: First Findings
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJS
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 

Viewers also liked

Visualizing user experience
Visualizing user experienceVisualizing user experience
Visualizing user experiencePrasanna Revan
 
Refactor Yourself with Balalaika
Refactor Yourself with BalalaikaRefactor Yourself with Balalaika
Refactor Yourself with Balalaikadudarev
 
Donetsk Twitter
Donetsk TwitterDonetsk Twitter
Donetsk Twitterdudarev
 
Presentatie Xbiv5 1 0 Slide Share
Presentatie Xbiv5 1 0 Slide SharePresentatie Xbiv5 1 0 Slide Share
Presentatie Xbiv5 1 0 Slide ShareSvenConvenso
 
Mobile applications with HTML and Javascript
Mobile applications with HTML and JavascriptMobile applications with HTML and Javascript
Mobile applications with HTML and Javascriptdudarev
 
Functional Programming in Python
Functional Programming in PythonFunctional Programming in Python
Functional Programming in Pythondudarev
 
Who are we?
Who are we?Who are we?
Who are we?dudarev
 
Presentatie Xbiv5 1 0 Slide Share
Presentatie Xbiv5 1 0 Slide SharePresentatie Xbiv5 1 0 Slide Share
Presentatie Xbiv5 1 0 Slide ShareSvenConvenso
 
Bolzoni 360 Rotator Model RC
Bolzoni 360 Rotator Model RCBolzoni 360 Rotator Model RC
Bolzoni 360 Rotator Model RCBrian_Garner
 
Carton Clamp Model KS-Z
Carton Clamp Model KS-ZCarton Clamp Model KS-Z
Carton Clamp Model KS-ZBrian_Garner
 
360 Degree Rotating Paper Roll Clamps
360 Degree Rotating Paper Roll Clamps360 Degree Rotating Paper Roll Clamps
360 Degree Rotating Paper Roll ClampsBrian_Garner
 
Bolzoni Auramo Push Pulls 2010
Bolzoni Auramo Push Pulls 2010Bolzoni Auramo Push Pulls 2010
Bolzoni Auramo Push Pulls 2010Brian_Garner
 
Fork Presentation 01 2010
Fork Presentation 01 2010Fork Presentation 01 2010
Fork Presentation 01 2010Brian_Garner
 
Operator Training Paper Roll Handling General
Operator Training Paper Roll Handling GeneralOperator Training Paper Roll Handling General
Operator Training Paper Roll Handling GeneralBrian_Garner
 
Forcematic-Clamp Pressure Control
Forcematic-Clamp Pressure ControlForcematic-Clamp Pressure Control
Forcematic-Clamp Pressure ControlBrian_Garner
 

Viewers also liked (19)

Visualizing user experience
Visualizing user experienceVisualizing user experience
Visualizing user experience
 
Refactor Yourself with Balalaika
Refactor Yourself with BalalaikaRefactor Yourself with Balalaika
Refactor Yourself with Balalaika
 
Donetsk Twitter
Donetsk TwitterDonetsk Twitter
Donetsk Twitter
 
Prodinzova
ProdinzovaProdinzova
Prodinzova
 
Presentatie Xbiv5 1 0 Slide Share
Presentatie Xbiv5 1 0 Slide SharePresentatie Xbiv5 1 0 Slide Share
Presentatie Xbiv5 1 0 Slide Share
 
Ololog
OlologOlolog
Ololog
 
Mobile applications with HTML and Javascript
Mobile applications with HTML and JavascriptMobile applications with HTML and Javascript
Mobile applications with HTML and Javascript
 
Functional Programming in Python
Functional Programming in PythonFunctional Programming in Python
Functional Programming in Python
 
Who are we?
Who are we?Who are we?
Who are we?
 
Rat Pack
Rat PackRat Pack
Rat Pack
 
Presentatie Xbiv5 1 0 Slide Share
Presentatie Xbiv5 1 0 Slide SharePresentatie Xbiv5 1 0 Slide Share
Presentatie Xbiv5 1 0 Slide Share
 
Bolzoni 360 Rotator Model RC
Bolzoni 360 Rotator Model RCBolzoni 360 Rotator Model RC
Bolzoni 360 Rotator Model RC
 
Carton Clamp Model KS-Z
Carton Clamp Model KS-ZCarton Clamp Model KS-Z
Carton Clamp Model KS-Z
 
360 Degree Rotating Paper Roll Clamps
360 Degree Rotating Paper Roll Clamps360 Degree Rotating Paper Roll Clamps
360 Degree Rotating Paper Roll Clamps
 
Bolzoni Auramo Push Pulls 2010
Bolzoni Auramo Push Pulls 2010Bolzoni Auramo Push Pulls 2010
Bolzoni Auramo Push Pulls 2010
 
About playdocja
About playdocjaAbout playdocja
About playdocja
 
Fork Presentation 01 2010
Fork Presentation 01 2010Fork Presentation 01 2010
Fork Presentation 01 2010
 
Operator Training Paper Roll Handling General
Operator Training Paper Roll Handling GeneralOperator Training Paper Roll Handling General
Operator Training Paper Roll Handling General
 
Forcematic-Clamp Pressure Control
Forcematic-Clamp Pressure ControlForcematic-Clamp Pressure Control
Forcematic-Clamp Pressure Control
 

Similar to Метапрограммирование в CL: макросы, DSL и компилятор

Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lispkyleburton
 
The Magnificent Seven
The Magnificent SevenThe Magnificent Seven
The Magnificent SevenMike Fogus
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARDTia Ricci
 
Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議dico_leque
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate CompilersFunctional Thursday
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj TalkSpark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj TalkZalando Technology
 
ภาษา C โปรแกรมย่อยและฟังก์ชันมาตรฐาน
ภาษา C โปรแกรมย่อยและฟังก์ชันมาตรฐานภาษา C โปรแกรมย่อยและฟังก์ชันมาตรฐาน
ภาษา C โปรแกรมย่อยและฟังก์ชันมาตรฐานNoppanon YourJust'one
 
Introduction to R
Introduction to RIntroduction to R
Introduction to Ragnonchik
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Phil Calçado
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานknang
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1Little Tukta Lita
 
Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기JangHyuk You
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015Michiel Borkent
 
Overview of Apache SystemML by Berthold Reinwald and Nakul Jindal
Overview of Apache SystemML by Berthold Reinwald and Nakul JindalOverview of Apache SystemML by Berthold Reinwald and Nakul Jindal
Overview of Apache SystemML by Berthold Reinwald and Nakul JindalArvind Surve
 

Similar to Метапрограммирование в CL: макросы, DSL и компилятор (20)

Introduction To Lisp
Introduction To LispIntroduction To Lisp
Introduction To Lisp
 
The Magnificent Seven
The Magnificent SevenThe Magnificent Seven
The Magnificent Seven
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
Procesos
ProcesosProcesos
Procesos
 
Scala @ TomTom
Scala @ TomTomScala @ TomTom
Scala @ TomTom
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議Meta-objective Lisp @名古屋 Reject 会議
Meta-objective Lisp @名古屋 Reject 会議
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj TalkSpark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
Spark + Clojure for Topic Discovery - Zalando Tech Clojure/Conj Talk
 
ภาษา C โปรแกรมย่อยและฟังก์ชันมาตรฐาน
ภาษา C โปรแกรมย่อยและฟังก์ชันมาตรฐานภาษา C โปรแกรมย่อยและฟังก์ชันมาตรฐาน
ภาษา C โปรแกรมย่อยและฟังก์ชันมาตรฐาน
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Groovy
GroovyGroovy
Groovy
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
 
โปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐาน
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
 
Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기Programming Lisp Clojure - 2장 : 클로저 둘러보기
Programming Lisp Clojure - 2장 : 클로저 둘러보기
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
Overview of Apache SystemML by Berthold Reinwald and Nakul Jindal
Overview of Apache SystemML by Berthold Reinwald and Nakul JindalOverview of Apache SystemML by Berthold Reinwald and Nakul Jindal
Overview of Apache SystemML by Berthold Reinwald and Nakul Jindal
 

Recently uploaded

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 

Recently uploaded (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 

Метапрограммирование в CL: макросы, DSL и компилятор

  • 1. Метапрограммирование в CL "Lisp isn't a language, it's a building material." - Alan Kay ● Макросы ● Макросы чтения ● Макросы компилятора ● ...
  • 2. Квазицитирование (defmacro nif (expr positive zero negative) (let ((var (gensym))) `(let ((,var ,expr)) (cond ((plusp ,var) ,positive) ((zerop ,var) ,zero) (t ,negative))))) (nif (- (* b b) (* 4 a c)) 2 1 0) (LET ((#:G624 (- (* B B) (* 4 A C)))) (COND ((PLUSP #:G624) 2) ((ZEROP #:G624) 1) (T 0)))
  • 3. Анафорические макросы (defmacro aif (condition then &optional else) `(let ((it ,condition)) (if it ,then ,else))) (aif (load-data)) (pprint it) (print "No data loaded.")) awhen, awhile, aand, alambda... (aand (load-data) (take-field it) (do-smth it))
  • 4. Декораторы (defmacro defun/decorated ((&rest decorator) name (&rest params) &body body) `(defun ,name (,@params) (,@decorator (lambda () ,@body) ,@params))) (defun decorator (x f &rest params) (format t "decorator ~A ~{~A ~}" x params) (funcall f)) (defun/decorated (decorator "smth") square (x) (* x x))
  • 5. Макросы пишут макросы: TCO (defun fact (n acc) (if (zerop n) acc (fact (- n 1) (* n acc)))) (defmacro defun/tco (name (&rest params) &body body) `(defun ,name (,@params) (macrolet ((,name (&rest args) `(progn (psetq ,@(mapcan (lambda (p v) (list p v)) ',params args)) (go :label)))) (tagbody :label (return-from ,name (progn ,@body)))))) (defun/tco fact/tco (n acc) (if (zerop n) acc (fact/tco (- n 1) (* n acc))))
  • 6. EDSL ● CLOS (Common Lisp Object System) ● ITERATE (iter (for i from 1) (for a in some-list) (collect (cons i a))) ● ContextL (AOP) ● Chtml-matcher (<tbody nil (tr nil (<a ((name ?post-num))) (tr nil) (tr nil (?post-body <div ((id "post_message_?"))))))
  • 7. Макросы чтения (set-dispatch-macro-character ## #$ #'(lambda (stream char arg) (parse-integer (coerce (loop for ch = (peek-char nil stream nil nil t) while (or (digit-char-p ch) (eql ch #_)) do (read-char stream t nil t) if (digit-char-p ch) collect ch) 'string)))) #$1_000_000
  • 8. Макросы чтения (let ((foo 1)) #Uhttp://www.example.com/widget/{foo}/parts) "http://www.example.com/widget/1/parts" (uri-template-bind (#Uhttp://www.example.com/{part}/{number}) "http://www.example.com/widget/1" (list part (parse-integer number) %uri-host)) ("widget" 1 "www.example.com")
  • 9. Макросы чтения (set-dispatch-macro-character ## #/ (lambda (stream char arg) (let ((pattern (coerce (loop for ch = (read-char stream t nil t) until (eql ch #/) collect ch) 'string))) `(lambda (&rest args) (apply #'cl-ppcre:scan ,pattern args))))) (#/[a-z]+/ str) ((lambda (&rest args) (apply #'cl-ppcre:scan "[a-z]+" args)) str) (cl-ppcre:scan "[a-z]+" str)
  • 10. Макросы компилятора (format stream control-string arg1...) (funcall (formatter control-string) stream arg1 ...) (lambda (stream &rest arguments) (apply #'format stream control-string arguments)) (formatter "Hello, ~A") (LAMBDA (STREAM #:FORMAT-ARG633) (WRITE-STRING "Hello, " STREAM) (PRINC #:FORMAT-ARG633 STREAM)) (formatter "~{~A~%~}") (LAMBDA (STREAM #:FORMAT-ARG636) (LET ((ARGS #:FORMAT-ARG636)) (LOOP (WHEN (NULL ARGS) (RETURN)) (PRINC (POP ARGS) STREAM) (TERPRI STREAM))))
  • 11. Макросы компилятора CL-PPCRE (define-compiler-macro scan (&whole form &environment env regex target-string &rest rest) (cond ((constantp regex env) `(scan (load-time-value (create-scanner ,regex)) ,target-string ,@rest)) (t form)))
  • 12. Ссылки ● Paul Graham «On Lisp» http://www.paulgraham.com/onlisp.html ● Doug Hoyte «LOL» http://letoverlambda.com/ ● CL-PPCRE http://weitz.de/cl-ppcre/ ● Iterate http://common-lisp.net/project/iterate/ ● ContextL http://common-lisp.net/project/closer/contextl.html ● chtml-matcher http://common-lisp.net/project/chtml-matcher/ ● uri-template http://common-lisp.net/project/uri-template/