Skip to content

Instantly share code, notes, and snippets.

@jxv
jxv / DepInjectBind.js
Created August 17, 2022 14:55
dependency injection via bind
function hello_world() { this.puts("hello world"); }
function add2(x) { this.puts(x + 2); }
function statements() {
$(hello_world)();
$(hello_world)();
$(add2)(2);
}
import qualified Data.Set as Set
neighbors :: (Int, Int) -> Set.Set (Int, Int)
neighbors (x, y) = Set.fromList [ (dx + x, dy + y) | dx <- [-1..1], dy <- [-1..1], dx /= 0 && dy /= 0 ]
advance :: Set.Set (Int, Int) -> Set.Set (Int, Int)
advance b = Set.foldr
(\c ->
let count = Set.size (Set.intersection b (neighbors c))
in if count == 3 || count == 2 then Set.insert c else id)
@jxv
jxv / Similar.hs
Created February 14, 2022 16:23
Similar (almost Eq but not quite)
import Prelude
import qualified Data.Set as Set
class Similar a where
(~~) :: a -> a -> Bool
(~~) x y = not (x /~ y)
(/~) :: a -> a -> Bool
(/~) x y = not (x ~~ y)
instance Ord a => Similar [a] where
#! /home/jxv/.local/bin/stack runghc
import qualified Data.ByteString as BS
-- import qualified Data.ByteString.IO as BS
import System.Environment
main :: IO ()
main = do
args <- getArgs
@jxv
jxv / Aeson.code-snippets
Created October 12, 2018 03:02
VSCode autocompleter with code snippets
{
"ToJSON & FromJSON instances": {
"scope": "haskell",
"prefix": "ins",
"body": [
"instance ToJSON $1",
"instance FromJSON $1"
]
}
}
@jxv
jxv / Authorization.hs
Created August 23, 2018 14:55
Generalized authorization engine (non-distributed)
-- http://theory.stanford.edu/~ataly/Papers/distributed-groups-plabs.pdf
type N = String -- Ordinary/local name
type Gn = String -- Group name, Gn maps to M
type B = [N]
type M = [B]
type P = [Either N G]
type L = [P]
-- data G = Gn Gn | GL L deriving (Show, Eq) -- Gn is excessive for demonstration purposes
@jxv
jxv / GcwZeroRepoScrap.hs
Last active December 2, 2017 22:17
gcw repo scraper
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
import Text.HTML.Scalpel
import Control.Applicative
import Data.Maybe
import qualified Data.Map as Map
import qualified Data.ByteString.Lazy as BL
import Data.Map (Map)
@jxv
jxv / gist:1037dbce18686b4cd607c0561540e515
Last active November 28, 2017 00:14
SQL haskell cheatsheet
-- Database.Persist.Types
data SelectOpt record
= Asc (EntityField record typ)
| Desc (EntityField record typ)
| OffsetBy Int
| LimitTo Int
data Filter record
= Filter { filterField :: EntityField record typ, filterValue :: Either typ [typ], filterFilter :: PersistFilter }
@jxv
jxv / keyMatch.js
Created February 4, 2017 08:06
key-match
// Pattern matching over pseudo sum types
// There exists a pattern for representing an Option sum type with Some and None tags:
// var option = {Some: "some's datum"}
// var option = {None: null}
// Or more generically,
// var sumType = { tag: datum }
@jxv
jxv / hello.hsfiles
Last active December 10, 2016 16:45
hello.hsfiles
{-# START_FILE {{name}}.cabal #-}
name:
{{name}}
version:
0.1.0.0
synopsis:
Insert synopsis
description:
Please see README.md
homepage: