Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael-Keith Bernard SegFaultAX

View GitHub Profile
@SegFaultAX
SegFaultAX / redis.go
Created Jul 23, 2020
Handling SliceCmd in go-redis [Go]
View redis.go
package main
import (
"context"
"fmt"
"log"
"github.com/go-redis/redis/v8"
)
@SegFaultAX
SegFaultAX / restart_count.py
Created Feb 3, 2020
Singularity Restart Count [Python]
View restart_count.py
#!/usr/bin/env python
import re
import arrow
import argparse
import urllib.parse
import requests
CLUSTERS = {
@SegFaultAX
SegFaultAX / sql.py
Created Nov 14, 2019
Simple bulk inserts with lookups [sqlite] [sql] [python]
View sql.py
import sqlite3
class Column:
def __init__(self, key, column=None):
self.key = key
self.column = column or self.key
@property
def value(self):
return ":" + self.key
@SegFaultAX
SegFaultAX / optics.kt
Created Jul 30, 2019
Some basic optics [Kotlin]
View optics.kt
package com.mkbernard.craps
interface PIso<S, T, A, B> {
fun get(s: S): A
fun reverseGet(b: B): T
fun set(b: B): (S) -> T = { reverseGet(b) }
fun set(s: S, b: B): T = set(b)(s)
fun modify(f: (A) -> B): (S) -> T = { reverseGet(f(get(it))) }
fun modify(s: S, f: (A) -> B): T = modify(f)(s)
@SegFaultAX
SegFaultAX / either.kt
Last active Jul 30, 2019
Either [Kotlin]
View either.kt
sealed class Either<out E, out A> {
data class Left<E>(val left: E) : Either<E, Nothing>()
data class Right<A>(val right: A) : Either<Nothing, A>()
fun <R> fold(ifLeft: (E) -> R, ifRight: (A) -> R): R =
when (this) {
is Left -> ifLeft(left)
is Right -> ifRight(right)
}
@SegFaultAX
SegFaultAX / typeclasses.scala
Last active Jul 20, 2019
Basic typeclasses [Scala]
View typeclasses.scala
object typeclasses {
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
trait Apply[F[_]] extends Functor[F] {
def map2[A, B, C](fa: F[A], fb: F[B])(f: (A, B) => C): F[C]
def ap[A, B](ff: F[A => B])(fa: F[A]): F[B] =
map2(ff, fa)(_(_))
@SegFaultAX
SegFaultAX / depaginate.scala
Created Jul 15, 2019
Depagination with loopM exploration [Scala]
View depaginate.scala
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
object Depaginate extends App {
trait Monad[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
def pure[A](a: A): F[A]
def flatMap[A, B](fa: F[A])(k: A => F[B]): F[B]
}
@SegFaultAX
SegFaultAX / logic.rb
Last active May 7, 2019
Basic algorithm for normalizing a first order logic formula into conjunctive normal form [Ruby]
View logic.rb
module Formula
class Expr
end
class Lit < Expr
attr_reader :name
def initialize(name)
@name = name
end
@SegFaultAX
SegFaultAX / guestlib.py
Created Mar 14, 2019
vmGuestLib [Python] [ctypes]
View guestlib.py
from ctypes import CDLL, c_void_p, byref
from ctypes.util import find_library
# The following code fails on ubuntu 18.04, but succeeds on 14.04
lib = CDLL(find_library('guestlib'))
handle = c_void_p()
ret = lib.VMGuestLib_OpenHandle(byref(handle))
if ret != 0:
raise RuntimeError("failed to get handle")
@SegFaultAX
SegFaultAX / reader.scala
Last active Mar 22, 2019
Reader Monad [Scala]
View reader.scala
object ReaderMonad extends App {
case class Reader[-E, +A](run: E => A) {
def map[B](fn: A => B): Reader[E, B] =
dimap((e: E) => e)(fn)
def contramap[E0](fn: E0 => E): Reader[E0, A] =
dimap(fn)(a => a)
def dimap[E0, B](f: E0 => E)(g: A => B): Reader[E0, B] =
Reader(e0 => g(run(f(e0))))
You can’t perform that action at this time.