Skip to content

Instantly share code, notes, and snippets.

@Metaxal
Created September 21, 2021 13:00
Show Gist options
  • Save Metaxal/e85c19acd3de0069fbe4f84b01491049 to your computer and use it in GitHub Desktop.
Save Metaxal/e85c19acd3de0069fbe4f84b01491049 to your computer and use it in GitHub Desktop.
Hygienic 'threading' macro for Racket
#lang racket/base
(require (for-syntax racket/base
syntax/parse))
(provide with)
(define-syntax (with stx)
(syntax-parse stx
[(with it last)
#'last]
[(with it body1 body2 ...)
#'(let ([it body1])
(with it body2 ...))]))
;;; Example
(module+ drracket
(require racket)
(with it
"Hey\nThis is a nice little doggy you have here\nCome here doggy!\nNice doggy indeed\nSee ya!"
(string-split it "\n")
(filter (λ (line) (string-contains? line "doggy")) it)
(sort it string<?)
(remove-duplicates it)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment