Skip to content

Instantly share code, notes, and snippets.

View Slackwise's full-sized avatar
🛸
Evangelizing Lisp

Adam Flanczewski Slackwise

🛸
Evangelizing Lisp
View GitHub Profile
@Slackwise
Slackwise / taconsole.js
Last active November 9, 2018 16:50
Taco your JS Console!
const randomIntOf = i => ~~(i * Math.random());
const randomOffsetFrom = i => (Math.random() * (i + 200) - 100);
function insertRandomFood() {
const taco = "\ud83c\udf2e";
const burrito = "\ud83c\udf2e";
const food = [taco, burrito];
const randomFood = food[randomIntOf(2)];
const int = randomIntOf(100);
const style = {
@Slackwise
Slackwise / schrodingersCat.js
Last active January 13, 2023 23:52
Function definition modeling Schrodinger's Cat.
const cat = {
get state() {
delete this.state;
this.state = Math.random() >= 0.5 ? 'Alive' : 'Dead';
return this.state;
}
};
@Slackwise
Slackwise / printMethodName.cs
Created August 20, 2018 21:35
Print the current method name for debugging purposes.(In my case, the debugger is crashing due to rogue pointers accessing the wrong address, but I don't know *where* it's occurring.
public void pmn()
{
try
{
var methodName = (new System.Diagnostics.StackTrace()).GetFrame(1).GetMethod();
System.Diagnostics.Debug.WriteLine(methodName);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Failed to print method name: ${ex.Message}");
@Slackwise
Slackwise / copyToClipboard.js
Last active June 30, 2018 01:40
Quick function to copy an element's text to the clipboard.
function copyToClipboard(element) {
if (element.select) {
element.select();
document.execCommand('copy');
} else {
let range = document.createRange();
range.selectNodeContents(outputText);
let selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
@Slackwise
Slackwise / cal.rb
Last active November 1, 2018 01:57
Implemented `/usr/bin/cal` (sans opts) in Ruby as a fun challenge my friend mentioned doing in an interview.
#!/usr/bin/env ruby
require 'date'
today = Date.today
first_day = Date.new(today.year, today.month, 1)
last_day = Date.new(today.year, today.month, -1) # A trick from JavaScript, heh...
first_week_blank_count = first_day.wday
last_week_blank_count = last_day.wday
@Slackwise
Slackwise / reduce.js
Last active May 12, 2019 12:14
Code golf reduce() in JavaScript.
// Impure
reduce = (f, i, l) => (n = [...l]).length ? r(f, f(i, n.pop()), n) : i
// Impure and minified
r=(f,i,l)=>(n=[...l]).length?r(f,f(i,n.pop()),n):i
// Pure
reduce = (reducerFunction, initialValue, enumerable) =>
enumerable.length
@Slackwise
Slackwise / fizzbuzz.clj
Last active September 29, 2020 19:27
FizzBuzz in Clojure, composed step-by-step.
(def fizzbuzz-nums (range 1 101))
(defn fizz? [n] (zero? (rem n 3)))
(defn buzz? [n] (zero? (rem n 5)))
(defn fizzbuzz? [n] (and (fizz? n) (buzz? n)))
(defn fizzbuzz [n]
(cond (fizzbuzz? n) "FizzBuzz"
(fizz? n) "Fizz"
(buzz? n) "Buzz"
@Slackwise
Slackwise / ondominsertfunction.js
Last active July 25, 2016 17:24
A simple JavaScript function to catch when elements are added to a specified container element.
/**
* A simple function to catch when elements are added to a specified container element.
* @author Most of this code is from a StackOverflow post I can't find right now. I wrapped it into a reusable function.
* @param {HTMLElement} containerElement - The container element to watch for added nodes.
* @param {function} eventHandlerFunction - The function to call when a node is added to the containerElement.
*/
function onDOMInsert(containerElement, eventHandlerFunction) {
if (typeof MutationObserver === 'function') {
var observer = new MutationObserver(function (m) {
for (var i = 0; i < m.length; i++) {
@Slackwise
Slackwise / reduce.scm
Last active March 16, 2018 11:42
Reduce in Scheme: my favorite function ever, written as reduced as possible, using only special forms.
(define (reduce f i l)
(if (null? l)
i
(f i (reduce f (car l) (cdr l)))))
var active = false;
function changeRefer(details) {
if (!active) return;
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'Referer') {
details.requestHeaders[i].value = 'http://www.google.com/';
break;
}