Skip to content

Instantly share code, notes, and snippets.

View tshddx's full-sized avatar

Thomas Shaddox tshddx

View GitHub Profile
@tshddx
tshddx / custom_hook_and_render_prop.js
Last active November 11, 2018 08:16
React custom hook and render prop
const renderPropFromHook = useHook => {
const Component = ({ children, ...rest }) => {
return children(useHook(rest));
};
return Component;
};
// `useCount` is my custom hook with a bunch of
// complicated hook usage inside.
const useCount = ({ initial }) => {
@tshddx
tshddx / LikeButton.js
Last active October 27, 2018 08:22
React LikeButton with Recompose and Redux connect
import React from 'react';
// The main component definition (and default explort) is a completely dumb
// component. All it knows is whether `likes` is true or false, and how and
// when to call the `setLiked` function. It doesn't know or care about where
// its two props come from.
const LikeButton = ({ liked, setLiked }) => {
return (
<button onClick={() => setLiked(!liked)} className={liked && 'liked'}>
{liked ? 'Click to unlike' : 'Click to like'}
@tshddx
tshddx / strftime_examples.py
Last active May 15, 2019 23:51
Python strftime examples
from time import strftime
from datetime import datetime
tt = datetime(2018, 4, 2).timetuple()
strftime('%b. %-d, %Y', tt)
# 'Apr. 10, 2018'
strftime('%B %-d, %Y', tt)
# 'April 10, 2018'
<html>
<style>
body {
margin: 0;
background-color: #111;
font-family: sans-serif;
}
.wrapper {
@tshddx
tshddx / preferred_numbers.rb
Created July 30, 2015 21:51
Preferred number generator
# https://en.wikipedia.org/wiki/Preferred_number
def preferred_numbers(x, precision=2)
root = 10 ** (1 / x.to_f)
return x.times.map {|i| (root ** i).round(precision)}
end
<<EXAMPLES
> preferred_numbers(2)
@tshddx
tshddx / recursive_enumerable_flatten.rb
Created March 31, 2015 00:53
Recursive Enumerable flatten
# Don't do this. It's fun, but stacks will overflow.
module Enumerable
def flatten_recursive(verbose=false, level=0)
car, cdr = self[0], self[1..-1]
is_enumerable = car.is_a?(Enumerable)
if verbose
indent = " " * level
if is_enumerable
puts "Recurring on Enumerable : #{indent} #{car.inspect}"
@tshddx
tshddx / kippt_to_pinboard.rb
Last active August 29, 2015 14:04
Pull bookmarks from Kippt API, upload via Pinboard API
# Instructions
# install required packages by running:
# gem install json typhoeus awesome_print
# Download this .rb into a new directory on your PC (called something like "pinboard" or whatever)
# Fill out KIPPT_USERNAME, KIPPT_API_TOKEN, and PINBOARD_AUTH_TOKEN (below) by changing xxx to the real value.
# Run the script by running
### Keybase proof
I hereby claim:
* I am baddox on github.
* I am shaddox (https://keybase.io/shaddox) on keybase.
* I have a public key whose fingerprint is 326A 3C60 FFEE 147E 9611 6560 413A 661B 220C B38D
To claim this, I am signing this object:
@tshddx
tshddx / lol.rb
Created June 2, 2014 23:34
Some Ruby lols
a = [1,2,3]
# => [1, 2, 3]
enum = a.map!
# => #<Enumerator: [1, 2, 3]:map!>
enum.map(&:to_s)
# => ["1", "2", "3"]
a
@tshddx
tshddx / ruby heredoc
Created May 8, 2014 01:58
ruby heredoc
irb(main):010:0> puts(<<X, <<Y)
irb(main):011:1" this is x
irb(main):012:1" X
irb(main):013:1" this is y
irb(main):014:1" Y
this is x
this is y
=> nil