Skip to content

Instantly share code, notes, and snippets.

@souravdatta
souravdatta / v1.cc
Created February 7, 2013 15:58
Mini-virtual function implementation.
#include <stdio.h>
#include <stdlib.h>
typedef void (*void_func)(void);
typedef struct tagaClass
{
void_func *vptr;
int a;
} aClass;
var cini = require('cinister');
var make_message = function (name, type) {
type = type.toUpperCase();
var html = name + ' is ' + type.toUpperCase();
if (type === 'GOOD') {
return '<h1>' + html + '</h1>';
}
else if (type === 'BAD') {
return '<h2>' + html + '</h2>';
var cini = require('cinister');
var make_message = function (name, type) {
type = type.toUpperCase();
var html = name + ' is ' + type.toUpperCase();
if (type === 'GOOD') {
return '<h1>' + html + '</h1>';
}
else if (type === 'BAD') {
return '<h2>' + html + '</h2>';
//
// main.cpp
// printf
// A crude but safe printf in C++11.
// Created by Sourav Datta on 17/05/13.
// Copyright (c) 2013 Sourav Datta. All rights reserved.
//
#include <iostream>
#include <string>
@souravdatta
souravdatta / monadic_kungfu.scala
Last active August 29, 2015 13:58
On my way to Monadic Inner Peace (not quite there yet)
object ProdDiscount {
// The state monad
class StateMonad[T](val candidate: T) {
// The combinator method, << operator overloaded!
def <<(fn: (T) => StateMonad[T]): StateMonad[T] = fn(this.candidate)
}
object StateMonad {
def apply[T](candidate: T): StateMonad[T] = new StateMonad[T](candidate)
#include <iostream>
#include <utility>
template <bool C, class T>
struct enable_when
{
};
template <class T>
struct enable_when<true, T>
#include <iostream>
#include <type_traits>
#include <utility>
template <class T, class ... ArgT>
struct has_arg_ctor
{
private:
using yes = char[2];
using no = char[1];
@souravdatta
souravdatta / ask.lisp
Created June 27, 2018 05:19
Ask n things with Common Lisp
(defun ask (&optional (what "name"))
(let ((data nil))
(progn
(format t ">>> ~A~%" (progn (format t "What is your ~A? " what)
(setf data (read-line))))
data)))
(defun ask-n (n)
(let ((data nil))
(defn find-while
[things thing in-things]
(cond
(match-star-var thing) [in-things things]
(match-var thing) [(concat in-things (butlast things)) [(last things)]]
(not (seq things)) (if (nil? thing)
[in-things []]
:fail)
(= (first things) thing) [in-things things]
:else (recur (rest things) thing (conj in-things (first things)))))
@souravdatta
souravdatta / conv.c
Last active July 26, 2021 20:54
A number converter program in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int value(char c)
{
switch (c) {
case 'A':