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 / gist:5182719
Last active July 5, 2022 04:36
Example of using `yield', and related usages of Racket's GUI
#lang racket/gui
(define xmax 200)
(define ymax 200)
(define seconds-in-loop 3)
(define fr (new frame% [label ""]))
(define cv (new canvas% [parent fr] [min-width xmax] [min-height ymax]))
(define dc (send cv get-dc))
(send fr show #t)
@Metaxal
Metaxal / client.rkt
Last active December 16, 2015 11:19
Unix domain socket communication test
#lang racket
(require (planet shawnpresser/racket-unix-sockets)
"communicate.rkt")
(define-values (i o)
(unix-socket-connect socket-path))
(communicate i o)
@Metaxal
Metaxal / dabbrev.rkt
Last active December 16, 2015 20:59
dabbrev for DrRacket. Similar to dabbrev for emacs, a bit simplified.
#lang racket/base
(require racket/class
racket/list
racket/format
;racket/gui ;for message-box
)
(define non-word-str "\"'`,;\r\n\t (){}[]")
(define non-word-chars (string->list non-word-str))
(define non-word-re (regexp-quote non-word-str))
@Metaxal
Metaxal / copy-paste.rkt
Last active December 18, 2015 03:39
Copy/paste example for keymap and text editors
#lang racket/gui
(require framework) ; for keymap:get-editor
(define keymap (keymap:get-editor))
#| ; Or define them yourself:
(define keymap (new keymap%))
(add-text-keymap-functions keymap)
@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/
@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 / 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 / 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 / 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 / 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))