Skip to content

Instantly share code, notes, and snippets.

View mofas's full-sized avatar

Chung Yen Li mofas

View GitHub Profile
@mofas
mofas / interp.rkt
Created November 15, 2018 18:00
Piet interpreter
#lang racket
(require 2htdp/image)
(require (for-syntax syntax/parse))
; #FFC0C0 light red
; #FFFFC0 light yellow
; #C0FFC0 light green
; #C0FFFF light cyan
; #C0C0FF light blue
@mofas
mofas / struct_con.rkt
Last active September 18, 2018 18:08
struct/con
(require (for-syntax syntax/parse))
(define-syntax (struct/con stx)
(syntax-parse stx
[(_ id ((pa colon checkers) ... ))
#:when (and (map (lambda (x) (eqv? (syntax-e #'x) ':)) (syntax->datum #'(colon ...))))
(begin
(define make-id (string->symbol (string-append "make-" (symbol->string (syntax->datum #'id)))))
#`(begin
@mofas
mofas / q.sql
Last active September 10, 2018 18:52
For Sabina
SELECT B1.BookNo
FROM Book B1
WHERE B1.Price NOT IN (Select B2.Price FROM Book B2 ORDER BY B2.Price DESC LIMIT 1) ORDER BY B1.Price DESC LIMIT 1;
SELECT p.Sid, p.BookNo
FROM (SELECT Buys.Sid, Buys.BookNo, Book.Price FROM Buys, Book WHERE Buy.BookNo = Book.BookNo) AS p
WHERE p.Price NOT IN
(SELECT p1.Price
FROM (SELECT Book.Price FROM Buys, Book WHERE Buys.Sid = p.Sid AND Book.BookNo = Buys.BookNo) AS p1,
(SELECT Book.Price FROM Buys, Book WHERE Buys.Sid = p.Sid AND Book.BookNo = Buys.BookNo) AS p2
@mofas
mofas / macro.rkt
Last active September 3, 2018 20:23
Macro presentation
;; Let say we want to build a text adventure game like following.
;; You’re standing in a meadow.
;; There is a house to the north.
;; > north
;; You are standing in front of a house.
;; There is a door here.
;; > open door
@mofas
mofas / p1.md
Last active August 25, 2018 22:36
AFP - P1

Vue File

A Vue file is a file with .vue extension. It is composed by three parts, template, script, and style. We ignore style now for simplicity.

The template portion of a Vue file is almost like HTML, except it includes the ability to add embedded JavaScript and directives, among other things. The script portion describes the functionality of the application. It specifically does so with an object called data, which provides values that are directly accessible in the template. Updates to values in data will be automatically propagated on the user interface.

<template>
 
@mofas
mofas / CLI.md
Last active September 16, 2018 20:32
Setup the new linux server

Common Tool

apt install curl
apt install vim
apt install git

CLI

const COLORS = {
blue: ['#1E88E5', '#90CAF9'],
brown: ['#6D4C41', '#D7CCC8'],
gray: ['#212121', '#BDBDBD'],
green: ['#388E3C', '#A5D6A7'],
red: ['#E53935', '#EF9A9A'],
orange: ['#F4511E', '#FFAB91'],
purple: ['#8E24AA', '#E1BEE7'],
yellow: ['#FFD600', '#FFF59D'],
}
@mofas
mofas / stream.js
Created April 29, 2018 00:50
Try to implement simple stream
// Stream
const stream = f => (x => x(x))(y => init => [init, m => y(y)(f(m))]);
const add3Stream = stream(n => n + 3);
const mult2Stream = stream(n => n * 2);
const inspect = stream(n => {
console.log('inspect:' + n);
return 0;
});
const pipe = s1 => s2 => x => {
@mofas
mofas / lc.js
Last active May 9, 2018 06:30
Lambda calculus in JS
// We learn lots of functional "concept" when we coding, such as point-free, high order function, avoiding side effect
// function composition.
// We are told they are good and we should code like that way.
// We feel strugging when learn the advanced conecpt like monads, and have difficulty to apply them to our coding.
// Sometime I feel using "functional programming" create more problems or not realistic.
// Like Using Rambda
@mofas
mofas / y-combinator.js
Last active May 9, 2018 06:16
y-combinator
// basic idea
(f => f(f)(5))(f => n => n == 0 ? 1 : (n * f(f)(n-1)));
// This version doesn't work, because js is not lazy.
// const Y = f => (x => f(x(x)))(x => f(x(x)));
// const Y = f => (x => x(x))(x => f(a => x(x)(a)));
const Y = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y)));