Skip to content

Instantly share code, notes, and snippets.

View shayelkin's full-sized avatar

Shay Elkin shayelkin

View GitHub Profile
;; Following is inspired by MLScroll (https://github.com/jdtsmith/mlscroll), intended
;; to replace mood-line-segment-scroll in mood-line (https://git.tty.dog/hpet_dog/mood-line).
(defun window-scroll-percentage ()
"Return current position in window, as a fraction, accounting for window height."
(let* ((end (window-end))
(total (point-max)))
(if (< end total)
(let* ((start (window-start)))
(/ (float start) (+ (- total end) start)))
@shayelkin
shayelkin / rg-gh.bash
Created November 26, 2025 11:17
A replacement for GitHub code search
#!/usr/bin/env bash
# rg-gh, a replacement for GitHub code search.
# --------------------------------------------
# Uses ripgrep (rg) to search, skim (sk) and bat to select a result,
# then prints a link to it on GitHub's website.
#
# To install the necessary dependencies:
# cargo install ripgrep -F pcre2
@shayelkin
shayelkin / threading.lisp
Created January 19, 2024 03:21
A lisp implementation of Clojure's threading macro
(defmacro -> (form &rest forms)
(reduce
(lambda (acc next)
(if (listp next)
(list* (car next) acc (cdr next))
(list next acc)))
forms
:initial-value form))
@shayelkin
shayelkin / aoc-2020-day-4.clj
Created December 5, 2020 04:55
AoC 2020 Day 4. Solve by generating a huge regex
;; AoC 2020 Day 4. Solve by generating a huge regex.
(require '[clojure.string :as s])
(def sample-passports
"ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
@shayelkin
shayelkin / is_pow_of_2.c
Last active July 26, 2019 07:57
Four methods to test if an integer is a power of two.
/**
* Four methods to test if an integer is a power of two.
* Compile with `-march=haswell`.
*/
#include <stdlib.h>
#include <stdio.h>
#include <immintrin.h>
#include <time.h>
@shayelkin
shayelkin / soduko.py
Last active May 21, 2019 18:18
Soduko solver. Everybody writes one.
#!/usr/bin/env python2
"""
Soduko solver. Everybody writes one.
"""
sample = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
all_bits = int('1'*9,2)
def load_puzzle(s):
@shayelkin
shayelkin / tomer-numbers.clj
Last active November 23, 2018 20:30
Ratio of non primes naturals <100 that only have primary divisors
;; https://twitter.com/sartanbegavhaum/status/1066051737650429952
(defn non-trivial-factors [n]
(filter #(zero? (mod n %)) (range 2 n)))
(def prime? (comp empty? non-trivial-factors))
(defn tomer-number? [n]
(when-let [facts (seq (non-trivial-factors n))]
(every? prime? facts)))
@shayelkin
shayelkin / camel->lisp.clj
Last active August 23, 2020 20:22
Convert camelCase to kebab-case
(s/defn camel->lisp :- s/Str
[s :- s/Str]
(->> s
(re-seq #("?:^\p{javaLowerCase}|\p{javaUpperCase}\p{javaLowerCase}+"))
(map string/lower-case)
(string/join "-")))
(s/defn keys-camel->list :- {s/Keyword s/Any}
[h :- {s/Keyword s/Any} & [recursive :- s/Bool]]
(into {} (map (fn [[k v]] [(-> k str (subs 1) camel->lisp)
@shayelkin
shayelkin / gmail_to_expensify.js
Created September 28, 2015 16:29
Automatically forward emails from Gmail's inbox to Expensify
// Forwards all emails in the inbox from `from_email` to Expensify, and archive them.
//
// To use:
// Add this function to a Google Apps Scripts project, and add a trigger (in Resources -> Current Project Triggers)
// to call it on a given interval.
// Make sure to specify the senders you'd like to forward!
function toExpensifyBySender(from_email) {
var threads = GmailApp.search('from:' + from_email + ' in:inbox');
@shayelkin
shayelkin / thumbtack-golf.py
Last active August 23, 2020 20:24
11 lines solution for https://www.thumbtack.com/engineering/pycon-2015/, based on https://gist.github.com/shayelkin/2397850f9b2c3df7e3aa (please excuse the golfing, I'm aiming for length)
import operator, itertools
def inspiration(*nums):
assert all(isinstance(x, int) and x > 0 for x in nums), "a deck of cards contains only integers"
ops = {operator.add: '+', operator.sub: '-', operator.mul: '*', operator.div: '/'}
possibilities = {reduce(lambda v, t: t[0](v,t[1]), zip(o, n[1:]), n[0]): (n, o) \
for n in itertools.permutations(nums[:-1]) for o in itertools.product(ops.keys(), repeat=len(nums)-2)}
try:
result_nums, result_ops = possibilities[nums[-1]]
return ' '.join('%s %s' % x for x in zip(result_nums, (ops[o] for o in result_ops))) + ' %s' % str(result_nums[-1])
except KeyError: