Skip to content

Instantly share code, notes, and snippets.

// Go to twitter.com and paste this into the js console to block all verified users (slowly)
class CheckieBlock {
constructor() {
this.waitMs = 10*1000;
this.jitMs = 2*1000;
this.blockLimit = 1000;
this.seen = new Set();
}
sleep() {
@ejconlon
ejconlon / BacktrackingStateSearch.hs
Created November 5, 2022 17:32
Backtracking state with LogicT
-- | 'LogicT' is a great monad transformer for backtracking control,
-- but if you just layer with a 'State' monad, you won't backtrack state.
-- By that I mean at all choice points '(<|>)' or 'interleave', we will
-- save part of the state and reset it when retrying alternative branches.
module BacktrackingStateSearch
( TrackSt (..)
, Track
, observeManyTrack
, runManyTrack
) where
@ejconlon
ejconlon / ElabInj.idr
Created March 14, 2021 00:05
Idris2 elaboration for injectivity
-- From ShinKage on FP Slack
mapName : (String -> String) -> Name -> Name
mapName f (UN n) = UN (f n)
mapName f (MN n i) = (MN (f n) i)
mapName f (NS ns n) = (NS ns (mapName f n))
mapName f (DN n realn) = (DN (f n) realn)
mapName f (RF n) = RF (f n)
appTyCon : List String -> Name -> TTImp
appTyCon ns n = foldl (\tt, v => `(~(tt) ~(IBindVar EmptyFC v))) (IVar EmptyFC n) ns
module Inj
import Decidable.Equality
import Decidable.Order
-- These definitions would go in some stdlib
public export
interface Equivalence ty inj => Inj (ty : Type) (inj : ty -> ty -> Type) where
useInj : {x, y: ty} -> inj x y -> x = y
import scala.annotation.tailrec
import scala.collection.immutable.SortedMap
import scala.collection.mutable
import scala.language.higherKinds
import scala.util.Random
import scala.util.control.TailCalls.{TailRec, done}
sealed trait KArrow[F[_], A, B] extends Product with Serializable
object KArrow {
final case class KMap[F[_], A, B](f: A => B) extends KArrow[F, A, B]
-- | Fun inspired by Selective
-- http://hackage.haskell.org/package/selective
module FreeSelective where
import Control.Monad (MonadPlus (..))
import Data.Functor.Compose
import GHC.Exts (Constraint)
import Data.Map (Map)
import qualified Data.Map as Map
import Prelude
@ejconlon
ejconlon / Repro.hs
Created March 6, 2019 19:40
Fixed repro
#!/usr/bin/env stack
-- stack --resolver lts-13.9 --install-ghc runghc --package aeson --package tasty --package text
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
@ejconlon
ejconlon / Repro.hs
Last active March 6, 2019 16:33
Repro Aeson optional serialization error
#!/usr/bin/env stack
-- stack --resolver lts-13.9 --install-ghc runghc --package aeson --package tasty --package text
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE UndecidableInstances #-}
import Data.Aeson
@ejconlon
ejconlon / CopSpec.scala
Created May 12, 2018 18:04
Interpreting coproducts of functors
package example
import cats.~>
import org.scalatest.FunSuite
object CopSpec {
final case class Foo[+A](value: A)
final case class Bar[+A](value: A)
final case class Baz[+A](value: A)
@ejconlon
ejconlon / hkd.scala
Last active January 13, 2021 16:17
Higher-Kinded Data Pattern in Scala
import scala.languageFeature.higherKinds
import cats.{Applicative, Id, ~>}
object Shapes {
case class Child[F[_]](nickname: F[String], age: F[Int])
case class Adult[F[_]](job: F[String], innerChild: Child[F])
class ApplicativeTrans[F[_]](implicit applicativeF: Applicative[F]) extends ~>[Id, F] {
override def apply[A](value: A): F[A] = applicativeF.pure(value)