Skip to content

Instantly share code, notes, and snippets.

View tautologico's full-sized avatar

Andrei Formiga tautologico

  • UFPB
  • João Pessoa, Paraíba
View GitHub Profile
@tautologico
tautologico / adt.ts
Created October 17, 2022 00:23
Representing discriminated unions/ADTs in TypeScript
// Discriminated Union in TypeScript
interface Integer {
tag: 'integer';
value: number;
}
interface Sum {
tag: 'sum';
op1: Expression;
@tautologico
tautologico / chrpeekiter.rs
Created May 28, 2022 16:33
Peekable Char Iterator
use std::iter::Peekable;
use std::str::Chars;
fn main() {
let s = "Aussonderungsaxiom".to_string();
let mut bf = Buffer::new(&s);
println!("First character should be A: {}", bf.peek().expect("q"));
println!("First char: {}", bf.next().expect("q"));
println!("Next char: {}", bf.next().expect("q"));
#lang racket
;; Write a simple C64 assembly program to a PRG file, along with a line
;; of BASIC program to load the machine-language code.
;; The assembly program that will be run
(define program
(bytes #xA9 #x00 ; LDA #00
#x8D #x20 #xD0 ; STA $D020
#x8D #x21 #xD0 ; STA $D021
@tautologico
tautologico / Activity.res
Created December 6, 2021 23:30
Call a web API using promises and log the result
// Fetch an activity from the Bored API
type promise<+'a> = Js.Promise.t<'a>
@send external then: (promise<'a>, @uncurry ('a => promise<'b>)) => promise<'b> = "then"
@send external thenResolve: (promise<'a>, @uncurry ('a => 'b)) => promise<'b> = "then"
module Response = {
type t<'data>
@send external json: t<'data> => promise<'data> = "json"
@tautologico
tautologico / Nodesrv1.res
Last active December 5, 2021 19:07
A simple Node server in ReScript
// example ported from
// "Web Development with Node and Express", 2nd edition
// by Ethan Brown
// bindings to the node API
type res
type req
type server
@module("http") external createServer : ( @uncurry (req, res) => unit ) => server = "createServer"
@tautologico
tautologico / remacc.js
Created November 21, 2019 04:45
Remove acute accents in a string, if they're represented as separate characters
// removes acute accents represented in separate characters
// e.g. a + ' = á
function removeAcute(s) {
let res = ""
let i = 0
for (i = 0; i < s.length; ++i) {
if (s.codePointAt(i) != 769)
res = res + s.charAt(i)
}
return res
let py = document.querySelector('#playerFrame').
contentDocument.querySelectorAll('rt')
let escondePinYin = () => py.forEach( e => e.style.color = '#FFFFFF' )
let mostraPinYin = () => py.forEach( e => e.style.color = '#000000' )
@tautologico
tautologico / buildcrossgcc.sh
Last active March 18, 2024 11:43
Build gcc cross-compiler for armv7-a (Cortex-A)
#!/bin/sh
# Download binutils, gcc, the linux kernel, glibc
# define the prefix
export PREFIX=/opt/armhf
# change PATH to include the target directory
export PATH=$PREFIX/bin:$PATH
@tautologico
tautologico / sqrt_callcc.ss
Created August 7, 2017 01:01
Square root function using continuations
;; sqrt function using continuations
;;
;; a port from an example in the first Scheme memo (AIM-349)
;; to modern Scheme
(define sqrt
(lambda (x epsilon)
((lambda (ans looptag)
(call-with-current-continuation
(lambda (returntag)
@tautologico
tautologico / product.ss
Created July 23, 2017 22:40
Product with exit for 0
;; product with break and O(1) case for a zero operand
;; call/cc
(define (product-cc ls)
(call-with-current-continuation
(lambda (break)
(cond ((null? ls) 1)
((= (car ls) 0) (break 0))
(else (* (car ls) (product-cc (cdr ls))))))))