Skip to content

Instantly share code, notes, and snippets.

View Metaxal's full-sized avatar

Laurent Orseau Metaxal

  • Google DeepMind
  • London, UK
View GitHub Profile
@Metaxal
Metaxal / current-theme.rkt
Last active December 22, 2015 19:59
Displays a frame of the current color theme for DrRacket
#lang racket
(require framework
racket/gui/base)
(provide theme->frame)
; Call (theme->frame) to open a frame with the current style as an info.rkt file
(color-prefs:register-info-based-color-schemes)
@Metaxal
Metaxal / folder-tree.rkt
Created October 1, 2013 10:00
Draw a hierarchy tree like a folder structure
#lang slideshow
(require bazaar/slideshow/slideshow-tree)
(define t1 '(a (b1 (c1 d1 d2)
(c2 d3 d4))
(b2 (c3 d5)
c4)))
(draw-tree-top-left-right
(tree-map t1 (λ(x)(t (symbol->string x))))
@Metaxal
Metaxal / units-contract.rkt
Created October 29, 2013 11:11
units contracts
#lang racket
(require measures)
(define-syntax-rule (define-units-contract name m1)
(define name
(make-flat-contract
#:name 'name
#:first-order
(λ(m2)(measure-units-equal? m1 m2)))))
@Metaxal
Metaxal / condd4.rkt
Last active December 28, 2015 03:59
condd4: The Design Space of Conditionals with Embedded Defines
#lang racket
(require (for-syntax racket/base
syntax/parse)
racket/list
racket/match)
;; Related post: http://jeapostrophe.github.io/2013-11-12-condd-post.html
(define (f-condd4 l)
(condd4
@Metaxal
Metaxal / def-jambda-ex.rkt
Last active December 29, 2015 07:39
def-jambda with signatures next to the body
(defn
#:doc @list{Multiplies @racket[x] by @racket[y].
Use it fruitfully.}
#:ex [10 3 => 30]
#:ex [0 3 => 0]
#:ex [10 => 10]
#:ex [0 => 0]
;; The function signature
(mult [x number?][y number? 1] -> number?)
;; And the function body:
@Metaxal
Metaxal / big-bang-slowness.rkt
Last active August 29, 2015 14:00
Simple test showing big-bang being slow at rendering on screen in new version
#lang racket
;;; Run this file with `racket test-big-bang.rkt`
;;; then type quickly "asdf" to trigger several successive to-draw events.
;;; Drawing the scene is fast, but rendering it on-screen is very slow?
(require 2htdp/image
2htdp/planetcute
2htdp/universe)
@Metaxal
Metaxal / regex-golf-norvig.py
Last active August 29, 2015 14:03
regex-golf slowdown?
#!/usr/bin/python
# From: http://nbviewer.ipython.org/url/norvig.com/ipython/xkcd1313.ipynb
import re
def verify(regex, winners, losers):
"Return true iff the regex matches all winners but no losers."
missed_winners = {W for W in winners if not re.search(regex, W)}
matched_losers = {L for L in losers if re.search(regex, L)}
if missed_winners:
print "Error: should match but did not:", ', '.join(missed_winners)
#lang racket
;; proc : procedure? ; procedure to apply
;; dict : dict? ; dictionary of keywords and values
;; A key can be either a keyword or a symbol that is turned into a keyword.
;; largs : list? ; positional arguments
;; Returns the result of the application of proc to the positional arguments and the keyword values.
(define (keyword-apply/dict proc dict largs)
(define alist
(sort
@Metaxal
Metaxal / message-no-event.rkt
Last active August 29, 2015 14:24
Sample code showing that on-subwindow-event does not have correct receiver for message%
#lang racket/gui
(define my-frame%
(class frame%
(define/override (on-subwindow-event receiver event)
(when (send event button-down?)
(displayln (send receiver get-label)))
#f)
(super-new)))
@Metaxal
Metaxal / canvas-speed.rkt
Last active August 29, 2015 14:25
Simple stress test for canvas speed
#lang racket/base
(require racket/gui/base
racket/class
racket/format)
(define NUM-CELL-X 100)
(define NUM-CELL-Y 100)
(define CELL-SIZE 2)
(define XMAX (* NUM-CELL-X CELL-SIZE))