Skip to content

Instantly share code, notes, and snippets.

View torjusti's full-sized avatar
🤠
yee haw

Torjus Iveland torjusti

🤠
yee haw
  • Trondheim, Norway
View GitHub Profile
/**
* Create a function which will call the callback function
* after the given amount of milliseconds has passed since
* the last time the callback function was called.
*/
const idle = (callback, delay) => {
let handle;
return () => {
if (handle) {
import React, { Component } from 'react';
import { findDOMNode } from 'react-dom';
/**
* A higher order component which scrolls the user to the bottom of the
* wrapped component, provided that the user already was at the bottom
* of the wrapped component. Useful for chats and similar use cases.
* @param {number} treshold The required distance from the bottom required.
*/
const scrollToBottom = (treshold = 0) => WrappedComponent => {
package minegenkode;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class LightsOut {
private List<Integer> board;
public LightsOut() {
from math import exp
def f(x):
y = (x-12)*exp(5*x) - 8*(x+2)**2
return y
def g(x):
y = - x - 2*x**2 - 5*x**3 + 6*x**4
return y
@torjusti
torjusti / bb.js
Created September 21, 2017 10:28
<script type="text/javascript">
(function() {
var MathJax = document.createElement("script");
MathJax.async = true;
MathJax.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(MathJax, s);
})();
</script>
@torjusti
torjusti / mappings.js
Created May 23, 2017 22:23
Bijection which maps repetition-less permutations to a single integer in a minimal range, given the number of affected pieces.
const getIndex = (permutation, affected) => {
const indexes = affected.map(i => permutation.indexOf(i));
if (affected.length === 1) {
return permutation.indexOf(affected[0]);
}
let base = permutation.length;
let index = indexes[indexes.length - 1];
@torjusti
torjusti / cantor.js
Created March 29, 2017 01:06
Generalized Cantor pairing function
const cantor = (arr) => {
if (arr.length === 2) {
return 1 / 2 * (arr[0] + arr[1]) * (arr[0] + arr[1] + 1) + arr[1];
}
return cantor(
arr.slice(0, arr.length - 2)
.concat(
cantor([arr[arr.length - 1], arr[arr.length - 2]])
)
def is_leap_year(year):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
return False
days = ('man', 'tir', 'ons', 'tor', 'fre', 'lor', 'son')
@torjusti
torjusti / cas.js
Created February 1, 2015 21:16
Improvements to my old pseudo CAS
var NumberTheory = {};
/**
* @param {number} n
* @param {number} k
*/
NumberTheory.binomial = function(n, k) {
if (n === k) return 1;
if (k === 1) return n;
return binomial(n - 1, k) + binomial(n - 1, k - 1);
@torjusti
torjusti / cas.js
Created January 4, 2015 15:16
Simple equation solving hack in JavaScript
// Prototype that stores information about a given polynomial.
function Polynomial(data) {
// Support providing already existing Polynomials.
this.polynomial = data instanceof Polynomial ? data.polynomial : data;
}
// Rank all coefficients in the polynomial and return the largest.
Polynomial.prototype.getLargestCoefficient = function() {
var re = /(\d+)/g, match, max = 0;
while(match = re.exec(this.getPolynomial()))