Skip to content

Instantly share code, notes, and snippets.

View privet-kitty's full-sized avatar

Hugo Sansaqua privet-kitty

View GitHub Profile
@privet-kitty
privet-kitty / compare-exp-and-expt.lisp
Last active August 1, 2018 11:41
Comparison of efficiency of EXPT and EXP/LOG
;;;
;;; Comparison of efficiency of EXP and EXPT
;;; on SBCL 1.4.2 (x86-64) or Clozure CL 1.11.5 (x86-64)
;;;
(defconstant +base+ 180d0)
(defconstant +logbase+ (log +base+))
;; the followings are equivalent (except for floating-point error)
(defun test1 (r) (exp (* +logbase+ r)))
@privet-kitty
privet-kitty / map-literal.lisp
Last active March 2, 2023 20:46
Clojure's {} in Common Lisp
;; {}: Map literal like Clojure
;; Note:
;; 1. Equality operator is #'eql.
;; 2. {} is a literal here though it is a constructor in Clojure:
;; Clojure:
;; (:b {:a 1 :b (+ 1 2)}) => 3
;; CL:
;; (gethash :b {:a 1 :b (+ 1 2)}) => (+ 1 2)
;;;
;;; Detect a constant form in compiler-macro
;;;
;; Example: faster expt when base is fixed
(defun fast-expt (base power)
(expt base power))
(define-compiler-macro fast-expt (&whole form base power)
"Ver. 1 regards only a float literal as constant."
(if (and (floatp base) (> base 0))
@privet-kitty
privet-kitty / array-to-list.lisp
Last active September 25, 2018 00:44
Benchmark of array-to-list conversion
;;; -*- encoding: utf-8 -*-
;; When I rewrote a code for array-to-list conversion on Stack
;; Overflow, I noticed that a simple recursion that returned at the
;; end of the tree was slower than a recursion that looked CDRs and
;; returned immediately before the end of the tree. Then I examined it
;; in detail.
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter OPT '(optimize (speed 3) (safety 0)))
@privet-kitty
privet-kitty / clipboard-win32.lisp
Last active August 19, 2018 17:30
Handle clipboard on Windows with CFFI
;; -*- encoding: utf-8 -*-
;;;
;;; Get/Set a text from/to clipboard on Windows.
;;;
;;; sbcl --load clipboard-win32.lisp --quit
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload :cffi))
@privet-kitty
privet-kitty / temporary-file.lisp
Last active August 28, 2018 20:41
UIOP replaces cl-fad.
;;
;; Handle temporary file with UIOP
;;
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload :cl-ftp)
(ql:quickload :quri))
(defparameter *dat-url* (quri:uri "ftp://ftp.cs.joensuu.fi/pub/color/spectra/mspec/munsell380_800_1.asc.gz"))
(defparameter *dat-filename* "munsell-joensuu-matt1.dat")
@privet-kitty
privet-kitty / type-propagation.lisp
Last active September 26, 2018 12:07
Type propagation problem on SBCL
;;; A difference between DECLARE TYPE and CHECK-TYPE produces an
;;; optimization problem on SBCL.
(declaim (inline add-with-declaring-type!))
(defun add-with-declaring-type! (vector1 vector2)
(declare (vector vector1 vector2))
(loop for idx below (length vector1)
do (incf (aref vector1 idx) (aref vector2 idx))
finally (return vector1)))
@privet-kitty
privet-kitty / wininet.c
Last active September 2, 2018 14:43
Test for wininet
#include <windows.h>
#include <wininet.h>
#include <stdio.h>
/*
Roswell on Windows signals an error when download_simple() is called
for some HTTPS pages: HttpSendRequest() fails. (Error code is 12029.)
This code is a rough reproduction of download_windows.c
https://github.com/roswell/roswell/blob/master/src/download_windows.c
;; Get the download counts for all the released assets in a GitHub repo.
;; Usage example:
;; (count-downloads "froggey" "Mezzano")
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload '(:dexador :jsown)))
(defun count-downloads (user repository)
(let ((releases-dat (jsown:parse (dex:get (format nil "https://api.github.com/repos/~A/~A/releases" user repository)))))
#!/bin/sh
#|-*- mode:lisp -*-|#
#|
exec ros -Q -- $0 "$@"
|#
;;
;; Get the download counts for all the released assets in a GitHub repo.
;; Usage: ./query-download-count.ros <user> <repository>
;;