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 / 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 / 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 / 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 / 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 / logging.rkt
Last active September 10, 2023 09:52
Simple usage of Racket's logging facility
#lang racket/base
; One way to define a logger
(define lg (make-logger 'my-logger))
; Define a receiver for this logger, along with a log level
(define rc (make-log-receiver lg 'error)) ; also try with 'debug
; Another way to define a logger, with additional forms
(define-logger lg2)
(define rc2 (make-log-receiver lg2-logger 'debug))
@Metaxal
Metaxal / compile-racket-QNAP.sh
Last active December 21, 2015 15:29
Compile racket on the QNAP TS 121 (ARMv5)
# This is not really a script and should be run line by line instead.
# With the admin user, do the following.
# Install Optware (ipkg)
# and configure the admin's path.
# http://wiki.qnap.com/wiki/Install_Optware_IPKG
# Install correct versions of basic tools,
# otherwise the following will not work.
ipkg install coreutils make find grep sed gawk
@Metaxal
Metaxal / name-and-pos.rkt
Last active May 7, 2020 10:00
Both by-name and by-position per argument procedure calls.
#lang racket
(require (for-syntax syntax/parse))
;;; Allow every argument to be passed by-name or by-position in a procedure call.
;;; Keywords do not need to apper in procedure headers in definitions.
;;; Accepts rest and keyword-rest arguments.
;;; A new definition of instantiate is also given to resemble this procedure call style.
;;; Resources:
;;; - http://www.mail-archive.com/[email protected]/msg08846.html
;;; - https://gist.github.com/Metaxal/5851215
@Metaxal
Metaxal / struct-default-super.rkt
Last active December 18, 2015 20:28
Giving default/optional values for struct. Define the struct with opt-struct instead of struct, and use make-<struct-name> as the default constructor. struct-default.rkt is the simple version, but does not handle super structs as struct-default-super.rkt does (though not entirely correctly).
#lang racket
(require (for-syntax syntax/parse
racket/syntax))
;;; Recognizes super-id and keyword arguments of struct.
;;; Caveats:
;;; - does not recognize per-field options (like per-field mutability).
;;; - the hash table uses the symbol of the super-id, but should use the binding instead.
;;; - may not work if the default expr depends on bindings not available in the context of the
@Metaxal
Metaxal / and-def.rkt
Last active December 18, 2015 08:59
Allowing for internal definitions in `and'
#lang racket
(require (for-syntax syntax/parse))
(define-syntax (and/def stx)
(syntax-parse stx
#:literals (define define-values)
[(_) #'#t]
[(_ [define-values (var:id ...) val:expr] rest ...)
#'(let-values ([(var ...) val])
@Metaxal
Metaxal / prisoner-dilemma-read-code.rkt
Last active December 18, 2015 04:09
Iterated prisoner's dilemma where the code can be read by the opponent.
#lang racket
(require racket/sandbox)
#|
Self-link: http://gist.github.com/Metaxal/5723825
Original idea by Eliezer Yudkowski and Alex Mennen:
http://lesswrong.com/lw/7f2/prisoners_dilemma_tournament_results/4ru9
http://lesswrong.com/lw/hmx/prisoners_dilemma_with_visible_source_code/