Skip to content

Instantly share code, notes, and snippets.

View hailelagi's full-sized avatar
Caffeinated

Haile hailelagi

Caffeinated
View GitHub Profile
@hailelagi
hailelagi / slim-redux.js
Last active September 6, 2020 19:54 — forked from gaearon/slim-redux.js
Redux without the sanity checks in a single file. Don't use this, use normal Redux. :-)
/*Helper fns */
function mapValues(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
result[key] = fn(obj[key], key);
return result;
}, {});
}
function pick(obj, fn) {
return Object.keys(obj).reduce((result, key) => {
@hailelagi
hailelagi / reduxFromRx.js
Created May 16, 2020 12:38 — forked from abiodun0/reduxFromRx.js
ReduxFromRx
// A very simple wrapper that provides redux-like functionality using RxJS observables
// Redux is basically just an observable(subject), except that it's not generic enough to be used like one.
// By using RxJS observables, you can solve many of the problems that redux has, such as asynchronous effects,
// in a very simple way. Since it's using an observable, it's easy to combine and compose the store with other streams
// like events or ajax calls.
// This simple wrapper provides the same functionality as redux at only 23 lines of code
const createStore = function(name, reducer, initialState) {
const storeName = 'store:'+ name
@hailelagi
hailelagi / connect.js
Created May 20, 2020 07:00 — forked from gaearon/connect.js
connect.js explained
// connect() is a function that injects Redux-related props into your component.
// You can inject data and callbacks that change that data by dispatching actions.
function connect(mapStateToProps, mapDispatchToProps) {
// It lets us inject component as the last step so people can use it as a decorator.
// Generally you don't need to worry about it.
return function (WrappedComponent) {
// It returns a component
return class extends React.Component {
render() {
return (
@hailelagi
hailelagi / 1.js
Created July 17, 2020 10:07 — forked from getify/1.js
is Maybe a "monad?
// is Just(..) a monad? Well, it's a monad constructor.
// Its instances are certainly monads.
function Just(v) {
return { map, chain, ap };
function map(fn) {
return Just(fn(v));
}
function chain(fn) {
return fn(v);
}
@hailelagi
hailelagi / fibs_lazy.exs
Last active January 9, 2021 05:13
spices of fibonacci, the good, the bad and the ugly. (without doing matrix multiplication!)
defmodule Fibonacci do
def generate(limit) do
# Create infinite fibonacci stream
Stream.unfold({0, 1}, fn
{x, y} -> {x, {y, (x + y)}}
end)
|> Enum.take(limit)
end
end
@hailelagi
hailelagi / ExVoseAlias.ex
Created August 31, 2020 18:31
an Erlang/Elixir implementation of the Alias(Vose) algorithm with caching!!
defmodule Probability do
def bias_probability(weights) do
# Initialization
# does the probability exist in memory?
current_probability = try do
[weights: cached_probs] = :ets.lookup(:weight_probability, :weights)
cached_probs
rescue
ArgumentError -> cache(weights)
end
@hailelagi
hailelagi / ThinkAboutMonads.md
Created September 6, 2020 04:13 — forked from cscalfani/ThinkAboutMonads.md
How to think about monads

How to think about Monads

Initially, Monads are the biggest, scariest thing about Functional Programming and especially Haskell. I've used monads for quite some time now, but I didn't have a very good model for what they really are. I read Philip Wadler's paper Monads for functional programming and I still didnt quite see the pattern.

It wasn't until I read the blog post You Could Have Invented Monads! (And Maybe You Already Have.) that I started to see things more clearly.

This is a distillation of those works and most likely an oversimplification in an attempt to make things easier to understand. Nuance can come later. What we need when first learning something is a simple, if inaccurate, model.

This document assumes a beginner's knowledge of pure functional programming and Haskell with some brief encounters of Monads, e.g. [Functors, Applicatives, And

@hailelagi
hailelagi / lean_gen.txt
Created January 7, 2021 23:00
ruby generators reminder
# lean generator (add --api flag to skip javascript)
# TODO: configured with graphiql for dev mode in --api mode?
rails new web-app-name --database="postgresql" --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-storage --skip-action-cable --skip
-javascript --skip-webpack-install
# (full app with react + postgres )
rails new rails-react-graphql -T -d=postgresql --webpack=react
@hailelagi
hailelagi / .babelrc
Last active January 7, 2021 23:11
webpack configuration for react.js + phoenix
{
"presets": [
"@babel/preset-env", "@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread"
]
}
@hailelagi
hailelagi / how to become a hacker
Last active August 18, 2021 10:26
Feeling lost? Find your way back to the true path.
1. catb.org/esr/faqs/hacker-howto.html
2. http://norvig.com/21-days.html
3. http://www.paulgraham.com/gh.html