Skip to content

Instantly share code, notes, and snippets.

View siaw23-retired's full-sized avatar

Emmanuel siaw23-retired

View GitHub Profile
@havenwood
havenwood / country_emoji.rb
Last active March 24, 2020 22:31
Country Emoji
COUNTRY_LETTERS = 'A'.upto('Z').with_index(127462).to_h.freeze
def country_emoji(iso)
COUNTRY_LETTERS.values_at(*iso.chars).pack('U*')
end
country_emoji('GB')
#=> "🇬🇧"
@havenwood
havenwood / cache.rb
Created March 20, 2020 20:13
Another example for xco on #ruby IRC
require_relative 'tuple_space'
class Cache
def initialize
@memory = TupleSpace.new(reaper_period_in_secs: 10, expires_in_secs: 60)
end
def get(request)
@memory[request]
end
@havenwood
havenwood / xco.rb
Created March 20, 2020 19:57
Example for xco on #ruby IRC
class Xco
attr_accessor :reaper
def initialize
@reaper = Automaton.new self
@reaper.start
end
class Automaton
def initialize(source, function: :reap, interval: 1)
@havenwood
havenwood / clock.rb
Created June 15, 2018 15:18
Three Fibers Dancing in a Clock
require 'fiber'
module Clock
DEFAULT_SECONDS = 4
module_function
def run seconds: DEFAULT_SECONDS
timer.resume tick, tock, seconds: seconds
end
@ljharb
ljharb / array_iteration_thoughts.md
Last active May 22, 2024 09:22
Array iteration methods summarized

Array Iteration

https://gist.github.com/ljharb/58faf1cfcb4e6808f74aae4ef7944cff

While attempting to explain JavaScript's reduce method on arrays, conceptually, I came up with the following - hopefully it's helpful; happy to tweak it if anyone has suggestions.

Intro

JavaScript Arrays have lots of built in methods on their prototype. Some of them mutate - ie, they change the underlying array in-place. Luckily, most of them do not - they instead return an entirely distinct array. Since arrays are conceptually a contiguous list of items, it helps code clarity and maintainability a lot to be able to operate on them in a "functional" way. (I'll also insist on referring to an array as a "list" - although in some languages, List is a native data type, in JS and this post, I'm referring to the concept. Everywhere I use the word "list" you can assume I'm talking about a JS Array) This means, to perform a single operation on the list as a whole ("atomically"), and to return a new list - thus making it mu

@staltz
staltz / introrx.md
Last active May 26, 2024 06:18
The introduction to Reactive Programming you've been missing
@MohamedAlaa
MohamedAlaa / tmux-cheatsheet.markdown
Last active May 26, 2024 17:22
tmux shortcuts & cheatsheet

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname