Posts Tagged ‘lisp’

Announcement: flutter-decline-demo – validation and form generation on compojure

Saturday, April 2nd, 2011

Some people indicated they wanted some example code for my clj-decline
(validation) and flutter (form generation) libraries. So today I wrote
a simple demo application that uses both.

Get the code at github.

Talk @ amsterdam-clojurians, Wednesday March 9, 2011

Monday, March 7th, 2011

 ____________________ 
< Functional Clojure >
 -------------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

I’ll be doing a short presentation on the basic higher-order (sequence) functions in clojure.core at the Amsterdam Clojurians meeting next Wednesday. This talk should be understandable and useful for Clojure newbies. If you’re interested, just show up around 7.

Update: quite a few people showed up to the meeting. I’ve put the slides on github, and here is the elisp code I used to present the slides.

Choosing your clojure startup script from Emacs/SLIME

Thursday, December 3rd, 2009

The standard instructions for swank-clojure (the clojure backend to SLIME) imply you always use the same binary and/or classpath for starting your clojure code. The problem with that is that for different projects you generally want to specify at least additional classpaths and possibly even a completely different clojure version.

At the same time, most projects already contain some kind of startup/REPL script, so why not use that? swank-clojure only really needs some code that starts a repl and has the swank-clojure code in its classpath.

Below is my emacs code to do just that. Just put it somewhere in your emacs init scripts, type M-x clojure and select your startup script and away you go. The following is all my clojure-specific code, but the really “interesting” bit is the (defun clojure ...) bit.

;; this is needed because swank-clojure complains if you don't have any
;; value for this variable. When you use the clojure command it'll override
;; this value
(setq swank-clojure-binary "clj")

(require 'clojure-mode)
(require 'swank-clojure-autoload)
(require 'swank-clojure)
(require 'slime)

(add-hook 'clojure-mode-hook 'slime-mode)

(add-hook 'clojure-mode-hook 'start-paredit)

(slime-setup 
 '(slime-fancy ;; turns on fancy inspector, autodoc and other useful stuff
   slime-highlight-edits)) 

(defun clojure (binary)
  (interactive "fbinary: ")
  (setq swank-clojure-binary (or binary "clj"))
  (setq slime-lisp-implementations
        `((clojure (,swank-clojure-binary) :init swank-clojure-init))) 
  (slime))

My complete emacs init stuff is at http://github.com/joodie/emacs-d

Oh, before I forget: the code above does assume that you’re only using SLIME with clojure and not also with other lisps. If you do need that functionality, you can probably figure out how to make it work.

Javascript regex en string literal highlighting in Emacs

Wednesday, October 31st, 2007

De beste javascript mode voor Emacs is deze van Karl Landström. Hij heeft alleen moeite met het correct detecteren van strings en regexes:

Voor

Niet leuk als je de source van jQuery opent. (Waarschuwing, dit crasht emacs!)

Ook niet leuk is dat Emacs’ ingebouwde syntax-table niet overweg kan
met de complete overloading van / in Javascript (als deel-operator, regex quote, en 2 soorten commentaar marker). Boeh voor Emacs!

De oplossing is dan om alle auto-quoting uit te zetten en het zelf te doen. Kan je net zulke ingewikkelde syntax mee highlighten als je wilt. Hoera voor Emacs!

Relevante passages:

;; --- Syntax Table And Parsing ---

(defvar javascript-mode-syntax-table
  (let ((table (make-syntax-table)))
    (c-populate-syntax-table table)

    ;; switch off build-in quoted string detection
    ;; since that just makes it really hard to detect
    ;; regular expressions and comments
    ;;
    ;; this also has the benefit that multiline strings
    ;; are now not recognized as strings (since javascript does
    ;; not allow them)
    (modify-syntax-entry ?' "." table)
    (modify-syntax-entry ?\" "." table)

    ;; The syntax class of underscore should really be `symbol' ("_")
    ;; but that makes matching of tokens much more complex as e.g.
    ;; "\\" matches part of e.g. "_xyz" and "xyz_abc". Defines
    ;; it as word constituent for now.
    (modify-syntax-entry ?_ "w" table)

    table)
  "Syntax table used in JavaScript mode.")
(defconst js-quoted-string-re "\\(\".*?[^\\]\"\\|'.*?[^\\]'\\)")
  
(defconst js-quoted-string-or-regex-re "\\(/.*?[^\\]/\\w*\\|\".*?[^\\]\"\\|'.*?[^\\]'\\)")

(defconst js-font-lock-keywords-1
  (list 
   "\\" 
   (list js-function-heading-1-re 1 font-lock-function-name-face)
   (list js-function-heading-2-re 1 font-lock-function-name-face)

   ;; detect literal strings following a + operator
   (list (concat "+[ \t]*" js-quoted-string-re)  1 font-lock-string-face)

   ;; detect literal strings used in "literal object" keys
   (list (concat "[,{][ \t]*" js-quoted-string-re "[ \t]*:" ) 1 font-lock-string-face)

   ;; detects strings and regexes when assigned, passed, returned
   ;; used as an object key string (i.e. bla["some string"]), when used
   ;; as a literal object value (i.e. key: "string"), used as an array
   ;; element, or when they appear as the first expression on a line
   ;; and a few other hairy cases
   (list (concat "[=(:,;[][ \t]*" js-quoted-string-or-regex-re)  1 font-lock-string-face)
   (list (concat "^[ \t]*"      js-quoted-string-or-regex-re) 1 font-lock-string-face)
   (list (concat "return[ \t]*" js-quoted-string-or-regex-re) 1 font-lock-string-face)

   ;; detect "autoquoted" object properties... clases with "switch { ...  default: }"
   ;; may not be worth the trouble
   (list "\\(^[ \t]*\\|[,{][ \t]*\\)\\(\\w+\\):" 2 font-lock-string-face))

  "Level one font lock.")

En dan:
Na

Patch is onderweg naar Karl. In de tussentijd kun je hier mijn versie downloaden.

lispcast.com – Lisp videos

Wednesday, October 31st, 2007

LispCast is a series of screencasts of Common Lisp development.

Lispcast laat in een serie van videos zien hoe je in Common Lisp een web applicatie schrijft, test, refactored en verder… Interessant materiaal.

De komende sessie gaat over database persistence – ik ben benieuwd!

Ps: als je cl-who niet kent, dan is de 1e aflevering alleen al interessant om te zien hoe veel schoner html/xml er uit ziet als je ze met s-expressions schrijft.

Lisp, functies, macros en expressieve code

Tuesday, October 30th, 2007

Ik ben jaren geleden al eens begonnen aan een Perl module om (onder andere) modulaire synthesizers te emuleren. En dat werkte wel, maar mooi was het niet:

Maak wat processors….

    my $net = Audio::LADSPA::Network->new();
    my $sine = $net->add_plugin( label => 'sine_fcac' );
    my $delay = $net->add_plugin( label => 'delay_5s' );
    my $play = $net->add_plugin('Audio::LADSPA::Plugin::Play');

verbind ze met mekaar en zet parameters…

    $net->connect($sine,'Output',$delay,'Input');
    $net->connect($delay,'Output',$play,'Input');
    
    $sine->set('Frequency (Hz)' => 440); # set freq
    $sine->set(Amplitude => 1);   # set amp

    $delay->set('Delay (Seconds)' => 1); # 1 sec delay
    $delay->set('Dry/Wet Balance' => 0.2); # balance - 0.2

en draaien maar!

    for ( 0 .. 100 ) {
        $net->run(100);
    }

Allemaal leuk, maar veel te veel typ werk en behoorlijk complex.

Op vakantie heb ik laatst Practical Common Lisp doorgelezen en dan ga je natuurlijk denken dat dat beter kan als je functies gebruikt ipv complexe objecten en dat het er in Lisp veel beter uit kan zien.

Dat is dus ook zo :-)

; ik ga hier processors gebruiken
(with-processors
    ; maak een paar processors
    ((sine (sine-processor))   
     (delay (delay-processor))
     (m (stream-mixer))
     (mlt (stream-mult)))
  (with-audio-stream stream 0 1 ()  ; gebruik 1 audio output
    (dotimes (i 400)
      (write-audio stream
		   (<- m         ; output van m
		       :input-1 (<- sine :frequency 0.1)
		       :factor-1 0.2   ; specificeer vaste parameter
		       :input-2 (<- ^sine)  ; uitvoer van sine processor hierboven
		       :factor-2 0.0)))))

Ik gebruik

(<- ... )

als een macro die de juiste output van een processor selecteerd:
:output bij default, anders scrijf je

(<- :andere-output processor .... )

En definieren van een processor is ook erg versimpeld:

(defprocessor mult-processor
    ((output :stream)) 
    ((input :stream) (factor :scalar))
    ()
  (dotimes (i *num-frames*)
    (setf (aref output i) (* (aref input i) factor))))