Skip to content

Instantly share code, notes, and snippets.

View ppsdatta's full-sized avatar

Sourav Datta ppsdatta

View GitHub Profile
@ppsdatta
ppsdatta / turtle.js
Created September 14, 2023 14:24
Turtle code in JS
class Turtle {
constructor(ox, oy, ctx) {
this.ctx = ctx;
this.ox = ox;
this.oy = oy;
this.reset();
}
reset() {
this.x = this.ox;
@ppsdatta
ppsdatta / weasel.lisp
Last active July 3, 2023 08:30
Weasel code in Common Lisp
(defconstant target "ME THINKS IT IS LIKE A WEASEL")
(defun rand-letter ()
(let ((r (random 27)))
(if (= r 26)
#\space
(code-char (+ (char-code #\A) r)))))
(defun rand-string (n)
(coerce (loop for i from 1 to n
@ppsdatta
ppsdatta / compositions.rkt
Last active June 28, 2023 07:28
Compositions
#lang racket
(define (arity f)
(let ([a (procedure-arity f)])
(if (number? a)
a
#f)))
(define (comp f g)
@ppsdatta
ppsdatta / client.py
Created June 22, 2023 13:26
Message passing between computers with sockets
import socket
import time
import re
import os
import sys
HOST = sys.argv[1]
PORT = int(sys.argv[2])
@ppsdatta
ppsdatta / server.pl
Last active June 20, 2023 14:08
Comm
use strict;
use warnings;
use IO::Socket;
sub handle {
my $c = shift;
my $command = <$c>;
$command = "" unless $command;
chomp $command;
@ppsdatta
ppsdatta / forthitude.py
Created June 16, 2023 13:16
A Forth like parser in Python
def get_next_token(s):
while s and s[0].isspace():
s = s[1:]
if not s:
return None, ""
if s[0] in "()":
return s[0], s[1:]
i = 0
while i < len(s) and not s[i].isspace() and s[i] not in "()":
i += 1
@ppsdatta
ppsdatta / networks.rkt
Created April 7, 2023 15:26
Perceptrons and Neural Networks in Racket - Functional + Composable
#lang racket
(require plot)
(define (sigmoid x)
(/ 1.0 (+ 1 (exp (- x)))))
(define (step x)
(if (>= x 0) 1 0))
@ppsdatta
ppsdatta / perceptron.scala
Created April 6, 2023 15:25
A simple Perceptron in Scala
import java.util.Random
class Perceptron(weights: Array[Double], learningRate: Double = 0.01, bias: Double = 1.0) {
def fit(input: Array[Array[Double]], labels: Array[Double]): Array[Double] =
fitWithBias(input, labels)
def fitWithBias(data: Array[Array[Double]], labels: Array[Double], epochs: Int = 1000): Array[Double] =
train(addBias(data), labels, epochs)
def train(data: Array[Array[Double]], labels: Array[Double], epochs: Int): Array[Double] =
@ppsdatta
ppsdatta / seq.lisp
Last active December 30, 2022 08:40
A sequence library for Common Lisp
(defclass seq () ())
(defmethod s-first ((s seq))
"The first element, if exists"
(error "Implement in child class"))
(defmethod s-rest ((s seq))
"The rest of the sequence, if exists"
(error "Implement in child class"))
@ppsdatta
ppsdatta / part1.cs
Created December 8, 2022 08:36
Live coding
using System;
using System.Collections.Generic;
namespace Console1
{
class Beverage
{}
class Flavor
{}