Skip to content

Instantly share code, notes, and snippets.

View SegFaultAX's full-sized avatar

Michael-Keith Bernard SegFaultAX

View GitHub Profile
@SegFaultAX
SegFaultAX / TableTest.java
Last active September 26, 2018 21:19
Functional relational joins [Java]
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
@SegFaultAX
SegFaultAX / Const.java
Last active June 20, 2018 00:49
Example of van Laarhoven Lenses [Java]
import java.util.function.Function;
public class Const<A, B> implements Functor<B> {
private final A value;
private Const(A value) {
this.value = value;
}
public static <A, B> Const<A, B> of(A value) {
@SegFaultAX
SegFaultAX / Coproduct.java
Last active May 15, 2018 15:56
Pattern matching via `Coproduct<A, B>` [Java]
package com.mkbernard.functional.examples;
import java.util.function.Function;
public abstract class Coproduct<A, B> {
public abstract boolean isLeft();
public abstract boolean isRight();
public abstract Left<A, B> left();
public abstract Right<A, B> right();
@SegFaultAX
SegFaultAX / Expr.java
Last active May 14, 2018 23:28
Example type-safe pattern matching [Java]
package com.mkbernard.functional.examples;
import java.util.List;
import java.util.function.Function;
import com.google.common.collect.ImmutableList;
public abstract class Expr<A> {
public abstract <T> T match(Function<Value<A>, T> value, Function<Add<A>, T> add);
@SegFaultAX
SegFaultAX / sensu.rb
Created March 13, 2018 00:08
Dead simple Sensu API wrapper [Ruby]
require 'rest-client'
require 'json'
module Sensu
class Server
attr_reader :name, :hostname, :port
def initialize(name, hostname, port = 4567)
@name = name
@hostname = hostname
@port = port
@SegFaultAX
SegFaultAX / creates.rb
Created January 18, 2018 00:45
Parse Graphite creates log and render as a tree [Ruby]
class Node
attr_reader :name
def initialize(name)
@name = name
@children = []
end
def children
@children.dup.freeze
@SegFaultAX
SegFaultAX / either.md
Last active January 12, 2018 01:56
data Either e a = Left e | Right a from Haskell in Python
@SegFaultAX
SegFaultAX / decorator.py
Created January 3, 2018 01:41
Decorator Example [Python]
def log_arguments(function_to_wrap):
def new_wrapped_function(*args, **kwargs):
name = function_to_wrap.func_name
print("Calling {} with args {} and kwargs {}".format(
name, args, kwargs))
return function_to_wrap(*args, **kwargs)
return new_wrapped_function
def add(a, b):
return a + b
@SegFaultAX
SegFaultAX / check-git-remote-version.rb
Last active December 19, 2017 01:36
Git Remote Version Check [Ruby] [Sensu]
#!/usr/bin/env ruby
#
# check-git-remote-version.rb
#
# DESCRIPTION:
# Ensure git repo is in-sync with remote
# This check verifies that the current version of a git repo reflects the
# latest version of a particular ref on a named remote repository.
#
# PLATFORMS:
@SegFaultAX
SegFaultAX / Safely.java
Last active July 28, 2017 16:45
Safely convert a function `T -> R throws E` to `T -> Optional<R>` [Java]
class Example {
<T, R, E extends Exception> Function<T, Optional<R>> safely(Function<T, R> fun, Class<E> exc) {
return v -> {
try {
return Optional.ofNullable(fun.apply(v));
} catch (Exception e) {
if (exc.isInstance(e)) {
return Optional.empty();
} else {
throw e;