Skip to content

Instantly share code, notes, and snippets.

@carld
carld / app.rkt
Created July 2, 2020 10:52 — forked from RyanKung/app.rkt
Demo: A simple MVC-web-framework implementation with PLT Scheme (Racket)
;;By Jhen Kung, [email protected]
;;app.rkt
#lang racket(require web-server/servlet
web-server/servlet-env
"router.rkt")
(serve/servlet mordor
#:port 8080
#:servlet-path "/"
@carld
carld / tsort.g
Created June 8, 2020 06:27 — forked from hilverd/tsort.g
Topological sort in Graphviz's gvpr
BEGIN {
int visited[node_t];
int visit(node_t n, edge_t e) {
if (visited[n] == 0) {
visited[n] = 1;
for (e = fstin(n); e; e = nxtin(e)) {
visit(e.tail, NULL);
}
@carld
carld / lisp.c
Created February 7, 2020 08:50
LISP as a register machine
/* LISP as a register machine. Any obj not in a reg or on the stack may be
* collected at the end of eval (). */
#include <ctype.h>
#include <iso646.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum { no } truth;
enum type { nil_type, int_type, sym_type, cons_type, str_type };
typedef struct obj {
@carld
carld / gist:e1d0f7d24b81ba86d1fd41b82473b328
Created December 22, 2018 07:32
mini parser combinators
#include<stdio.h>
int lookahead = -1;
void getchar_skipwhitespace() { while( (lookahead = getchar()) == ' ') ; }
int success() { getchar_skipwhitespace(); return 1; }
int failure() { return 0; }
int lparen() { return lookahead == '(' ? success() : failure(); }
int rparen() { return lookahead == ')' ? success() : failure(); }
int sym() { return lookahead >= 'a' && lookahead <= 'z' ? success() : failure(); }
int star( int (*a) (void) ) { return a() ? star(a) : 1; }
int main(int argc, char *argv[]) {
@carld
carld / config.ru
Created April 6, 2018 01:44
rack up
run Proc.new { |env|
['200', {'Content-Type' => 'text/html'}, [env.to_s]]
}
@carld
carld / rack.rb
Created April 6, 2018 01:43
rack app
require 'rack'
app = Proc.new do |env|
['200', {'Content-Type' => 'text/html'}, [env.to_s]]
end
Rack::Handler::WEBrick.run app
@carld
carld / psql-ssh-tunnel.md
Last active March 29, 2022 09:15
SSH tunnel PSQL

Run a ssh tunnel to a database via jump / bastion host

  ssh -v -L 9999:[database host]:5432 jumphost

  psql -h localhost -p 9999 -U [user] --password -d [database]

If you get:

@carld
carld / reduce.rb
Created November 2, 2017 01:28
break out of reduce
run = ->(a,v) { a.reduce(v){|x,y| break unless y.call(v); x } }
run.call [
->(v) { puts "Hello"; true },
->(v) { puts "World"; false },
->(v) { puts "!"; true }
], 1
@carld
carld / cmd.rb
Created November 2, 2017 00:36
anonymous lambda to call a pipeline of lambdas
run = ->(p) do
f, *r = p
f.call(->() { run.call(r) } , ->() { false }) if f
end
run.call [ ->(s,e){puts "Hello"; s.call} ,
->(s,e){puts "World";s.call} ]
@carld
carld / lambda3.rkt
Last active August 15, 2017 11:13
self-evaluating functional Lisp / Scheme interpreter without define, letrec, let, set etc
#lang racket
;
; A self-evaluating Lisp interpreter implemented without define, letrec, let
;
; Copyright (C) 2017 A. Carl Douglas
;
; without current-namespace, this racket error occurred:
; ?: function application is not allowed;
; no #%app syntax transformer is bound in: