Skip to content

Instantly share code, notes, and snippets.

@alkhe
alkhe / fib.cr
Created May 11, 2020 17:56
Fibonacci in Crystal
while true
print "Compute nth Fibonacci number. (n < 94): "
input = gets
if input.nil?
puts "\nExiting."
exit
end
@alkhe
alkhe / math.js
Created October 24, 2019 07:55
Fast Sine and Cosine Using Taylor Series
const PI_OVER_2 = Math.PI / 2
function cos(x) {
const x_sq = x * x
let next_add = false
let sum = 1
let n = 1
let d = 1
let term = 1
@alkhe
alkhe / index.js
Created November 4, 2018 09:02
425
function Trie() {
this.trie = new Map
}
Trie.prototype.add = function (s) {
let branch = this.trie
for (let i = 0; i < s.length; i++) {
const c = s[i]
if (!branch.has(c)) {
const next_branch = new Map
@alkhe
alkhe / index.js
Created November 2, 2018 22:28
220
/**
* @param {number[]} nums
* @param {number} k
* @param {number} t
* @return {boolean}
*/
function containsNearbyAlmostDuplicate(nums, k, t) {
const tree = []
for (let i = 0; i < nums.length; i++) {
@alkhe
alkhe / min_pq.js
Last active November 2, 2018 00:28
Min-Priority Queue
const min_pq = () => {
const heap = []
const index_map = new Map
function get_set_for(x) {
if (index_map.has(x)) {
return index_map.get(x)
} else {
const s = new Set
index_map.set(x, s)
@alkhe
alkhe / ComboBox.jsx
Created April 3, 2018 03:19
React ComboBox
const ComboBoxStyle = styled('div')`
display: flex;
align-items: center;
select {
font-size: 0;
background: transparent url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 512'%3E%3Cpath fill='%23414850' d='M31.3 192h257.3c17.8 0 26.7 21.5 14.1 34.1L174.1 354.8c-7.8 7.8-20.5 7.8-28.3 0L17.2 226.1C4.6 213.5 13.5 192 31.3 192z'/%3E%3C/svg%3E") no-repeat right .5rem center;
background-size: 50%;
height: .5rem;
width: 1rem;
@alkhe
alkhe / stream.ts
Created March 28, 2018 08:14
React Streams
import * as React from 'react'
import $, { Stream, Subscription } from 'xstream'
import { createChangeEmitter, Emitter } from 'change-emitter'
import { UnsubscribingProducer, StreamComponentState } from '../typings'
type Homo<I, O> = (input: I) => O
export function event() {
const emitter = createChangeEmitter()
@alkhe
alkhe / pre-push
Last active March 15, 2018 19:13
Git pre-push hook for linting
#!/bin/sh
remote="$1"
url="$2"
while read local_ref local_sha remote_ref remote_sha
do
yarn lint || exit 1
done
@alkhe
alkhe / pp.js
Created December 14, 2017 21:39
palindrome permutations
function is_palindrome_permutation {
const buckets = new Map
const is_even = string.length & 1 === 0
for (let i = 0; i < string.length; i++) {
const c = string[i]
if (buckets.has(c)) {
buckets.set(c, !buckets.get(c))
} else {
@alkhe
alkhe / sum.js
Created December 6, 2017 00:03
Javascript Sum using Arrows
// lists and arrows are both constructed from reducing from array with cons/compose.
// they are monoids.
const flip = f => (y, x) => f(x, y)
const is = c => x => x === c
const get = k => x => x[k]
// lists