Skip to content

Instantly share code, notes, and snippets.

@hsavit1
hsavit1 / connect.js
Created June 24, 2017 15:20 — 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 (
@hsavit1
hsavit1 / upgrade.md
Created May 5, 2017 20:24 — forked from chrismccord/upgrade.md
Phoenix 1.2.x to 1.3.0 Upgrade Instructions

If you want a run-down of the 1.3 changes and the design decisions behidn those changes, check out the LonestarElixir Phoenix 1.3 keynote: https://www.youtube.com/watch?v=tMO28ar0lW8

To use the new phx.new project generator, you can install the archive with the following command:

$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez

Bump your phoenix dep

Phoenix v1.3.0 is a backwards compatible release with v1.2.x. To upgrade your existing 1.2.x project, simply bump your phoenix dependency in mix.exs:

@hsavit1
hsavit1 / head_tail.swift
Created August 7, 2016 04:41
Head and tail for Swift
extension Array {
var match : (head: T, tail: [T])? {
return (count > 0) ? (self[0],Array(self[1..<count])) : nil
}
}
func map<A,B>(f: A -> B, arr: [A]) -> [B] {
if let (head,tail) = arr.match {
return [f(head)] + map(f, tail)
} else {
@hsavit1
hsavit1 / States-v3.md
Last active July 29, 2016 18:37 — forked from andymatuschak/States-v3.md
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen conflate side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@hsavit1
hsavit1 / genserver.ex
Created July 28, 2016 22:38
Genserver Implementation
defmodule Server do
def start(callback_module, state \\ nil) do
parent = self
spawn fn ->
loop(callback_module, parent, state)
end
end
def loop(callback_module, parent, state) do
receive do
@hsavit1
hsavit1 / process_msging.ex
Created July 28, 2016 16:33
Small example of process messaging in elixir
defmodule Speaker do
def speak do
receive do
{:say, msg} ->
IO.puts(msg)
speak
_other ->
speak # throw away the message
end
end
@hsavit1
hsavit1 / possibly.ex
Created July 19, 2016 16:31 — forked from keathley/possibly.ex
Monads and Maybes in elixir
defprotocol Functor do
def fmap(data, func)
end
defprotocol Applicative do
def pure(data)
def run(wrapped_func, data)
end
defprotocol Monad do
@hsavit1
hsavit1 / YouTubePlayerViaTVJS.swift
Created July 6, 2016 00:34 — forked from nickv2002/YouTubePlayerViaTVJS.swift
Swift code to play YouTube videos on AppleTV tvOS via a TVJS call using XCDYouTubeKit
//
// Created by Nick Vance on 12/4/15.
// Copyright © 2015 ToWatchList. All rights reserved.
//
import AVKit
import XCDYouTubeKit
import TVMLKit
class YTPlayerViewController: AVPlayerViewController, AVPlayerViewControllerDelegate {
@hsavit1
hsavit1 / Ass.swift
Created August 13, 2015 17:16
Associated Objects and Method Swizzling in Swift
extension UIViewController {
private struct AssociatedKeys {
static var DescriptiveName = "nsh_DescriptiveName"
}
var descriptiveName: String? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.DescriptiveName) as? String
}
set {
@hsavit1
hsavit1 / bbtree.py
Created June 27, 2016 02:55 — forked from olomix/bbtree.py
Balanced binary tree in Python
#!/usr/bin/env python2.7
import random
import subprocess
class Node(object):
def __init__(self, key, value):
self.key = key
self.value = value