Skip to content

Instantly share code, notes, and snippets.

@commander-trashdin
commander-trashdin / diofant.hs
Last active April 2, 2021 15:49
Diofant equation
module Main where
import Data.Maybe
import Text.Read
uncurry3 :: (a -> b -> c -> d) -> (a,b,c) -> d
uncurry3 foo (a,b,c) = foo a b c
get3Ints s = do
[a, b, c] <- traverse readMaybe $ words s
@commander-trashdin
commander-trashdin / template.lisp
Last active March 31, 2021 15:14
Doing some templates
(defun encode-type (typename)
(let ((str ""))
(labels ((rec (name)
(when name
(if (listp name)
(mapcar #'rec name)
(setf str (concatenate 'string str "-" (format nil "~s" name)))))))
(rec typename)
str)))
@commander-trashdin
commander-trashdin / qs.cpp
Created March 7, 2021 17:19
showing how to do this
template <class Iterator, class RandomGenerator>
void QuickSort(Iterator begin, Iterator end, RandomGenerator& generator) {
if (begin == end) {
return;
}
std::uniform_int_distribution<> dis(0, end - begin - 1);
auto pivot = *(begin + dis(generator));
auto first = begin;
auto last = end - 1;
if (begin == last) {
type Guest struct {
CheckInDate int
CheckOutDate int
}
type Load struct {
StartDate int
GuestCount int
}
(defstruct guest
(checkin 0 :type fixnum)
(checkout 0 :type fixnum))
(defstruct guestload
(start 0 :type fixnum)
(guestcount 0 :type fixnum))
(defstruct tdate
(date 0 :type fixnum)
@commander-trashdin
commander-trashdin / double-linked-list-dispatched.lisp
Created February 28, 2021 10:56
An example of datastructure with dispatch, very much alpha version
(ql:quickload 'adhoc-polymorphic-functions)
(use-package :adhoc-polymorphic-functions)
;(define-polymorphic-function make (type size)) ;;This one is harder that you might think
(define-polymorphic-function push-front (data container))
(define-polymorphic-function push-back (data container))
(define-polymorphic-function pop-front (container))
(define-polymorphic-function pop-back (container))
(define-polymorphic-function size (container))
@commander-trashdin
commander-trashdin / default-types.lisp
Last active March 2, 2021 17:26
Important part of my util, that is about sane defaults for all types, including parametrized. Let's try to cover all of them, can be very useful.
(defparameter *default-impl* (make-hash-table))
(Defun %dimensions-comp (dimensions)
(cond ((eql '* dimensions) 0)
((listp dimensions) (mapcar (lambda (x) (if (eql '* x) 0 x)) dimensions))
(t dimensions)))
@commander-trashdin
commander-trashdin / defun-doc.lisp
Created October 16, 2020 22:26
Let's force people to write docs.
(defparameter *type-policy* :docs)
(defparameter *doc-info* (make-hash-table :test #'equal))
(defun validate-ftype (ftype)
(labels ((check-symbols (list)
(every (lambda (token)
(if (atom token)
(symbolp token)
(check-symbols token)))
@commander-trashdin
commander-trashdin / word_count.cpp
Created September 24, 2020 13:43
Slow version
#include <vector>
#include <string>
#include <unordered_map>
#include <fstream>
#include <utility>
#include <algorithm>
#include <cassert>
#include <string_view>
@commander-trashdin
commander-trashdin / safe-map.lisp
Created September 22, 2020 00:08
Safely mapping with backup
(defun prompt-new-value (prompt)
(format *query-io* prompt) ;; *query-io*: the special stream to make user queries.
(force-output *query-io*) ;; Ensure the user sees what he types.
(list (read *query-io*)))
(defun safe-map (predicate sequence &optional copy)
(let ((backup (make-array 1 :adjustable t :fill-pointer 0)))
(loop :for i :from 0 :below (length sequence)