Skip to content

Instantly share code, notes, and snippets.

View kbridge's full-sized avatar

kq kbridge

  • Sunnydale
View GitHub Profile
@kbridge
kbridge / 00_ToBinary.hs
Last active September 9, 2022 17:45
A complex implementation of integer to binary conversion in Haskell
import Data.Bits (Bits (testBit), FiniteBits(countLeadingZeros, finiteBitSize))
import Data.Char (intToDigit)
toBinary :: FiniteBits a => a -> [Char]
toBinary n = [charOfBitAt i n | i <- reverse [0..(bitLength n - 1)]]
where
bitLength = (-) <$> finiteBitSize <*> countLeadingZeros
charOfBitAt i = intToDigit . fromEnum . flip testBit i
@kbridge
kbridge / input-password.clj
Created May 6, 2023 04:42
clojure style exercise
;; style 1
(let [console (System/console)
password-char-array (.readPassword console "Password (hidden): " (to-array ()))
password (String. password-char-array)
quoted (fn [s] (str \' s \'))]
(println (quoted password)))
;; style 2
@kbridge
kbridge / chrome-hide-scroll-bars.js
Created June 10, 2023 16:04
keep you focus on the content without worrying about progress
var style = document.createElement('style');
style.innerHTML = 'html::-webkit-scrollbar { display: none; }';
document.head.appendChild(style);
@kbridge
kbridge / ifaddrs.c
Created June 18, 2023 18:23
demonstrate getifaddrs()
#include <linux/if_packet.h>
#include <net/if.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
int
@kbridge
kbridge / main.cc
Created September 23, 2023 16:05
Query Installed Anti-Virus Software on Windows
// https://learn.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt
// https://source.chromium.org/chromium/chromium/src/+/main:base/win/scoped_com_initializer.h
// https://source.chromium.org/chromium/chromium/src/+/main:base/win/scoped_bstr.h
// https://source.chromium.org/chromium/chromium/src/+/main:chrome/services/util_win/av_products.cc (FillAntiVirusProductsFromWSC)
// C
#include <cassert>
#include <clocale>
@kbridge
kbridge / non-exist-paths.st
Created September 25, 2023 16:16
tested on Glamorous Toolkit
| path sep dirs |
sep := (Smalltalk os isWindows) ifTrue: [ $; ] ifFalse: [ $: ].
path := Smalltalk os environment at: 'path'.
dirs := sep split: path.
dirs reject: [ :each | File exists: each ].
#!/bin/bash
# the uninstall script corresponding to the install script here:
# https://clojure.org/guides/install_clojure#_linux_instructions
set -e
prefix_dir=/usr/local
rm -rfv $prefix_dir/lib/clojure
@kbridge
kbridge / winrt.c
Created December 3, 2023 18:38
demo: call windows runtime from pure c
// please link with "runtimeobject.lib"
#include <roapi.h>
#include <winstring.h>
#include <windows.foundation.h>
#include <stdio.h>
#include <stdlib.h>
@kbridge
kbridge / puzzle.ml
Last active December 14, 2023 17:51
ocaml implementation of a puzzle operator
module Puzzle = struct
let (+) a b =
let rec work accum base remain =
if remain = 0
then accum
else work
(Int.add accum (remain mod b * base))
(base * 10)
(remain / b)
in
@kbridge
kbridge / get_random_string.cpp
Created January 3, 2024 11:56
demonstrate the usage of c++ standard library random facilities
#include <iostream>
#include <random>
#include <string>
std::string get_random_string(size_t n)
{
std::random_device device;
std::default_random_engine::result_type seed = device();
std::default_random_engine engine(seed);
std::uniform_int_distribution<int> distribution('a', 'z');