Skip to content

Instantly share code, notes, and snippets.

View beoliver's full-sized avatar

Benjamin E. Oliver beoliver

View GitHub Profile
@beoliver
beoliver / vigenere.py
Last active December 10, 2015 23:48
a (functional) python 3.x vigenere encoder / decoder.
# ben oliver 11.01.2013
# a (functional) python 3.x vigenere encoder / decoder.
# encode and decode functions are mathematical, no dictionary lookups.
from itertools import cycle
from functools import partial
from string import ascii_uppercase
def transform(crypt_function,k,s):
""" (Int -> Int -> Int) -> Char -> Char -> Char """
@beoliver
beoliver / booleans.js
Last active December 28, 2022 20:18
javascript boolean matches (and / nand / or / nor / xor / iff / all / any / none )
// a better name would be - if predicate is True then success, else not success.
// using basic logic we can now create logic functions that compute only as much as required
// anyBool :: (a -> Bool) -> Bool -> [a] -> Bool
function anyBool(pred,success,xs) {
for (var i = 0; i < xs.length; i++) {
if (pred(xs[i]) === success) {
return success }}
return !success }
@beoliver
beoliver / ifElse.js
Last active August 29, 2015 13:56
ifElse function
function ifElse(pred,true_fn,else_fn,xs) {
for (var i = 0; i < xs.length; i++) {
if (pred(xs[i]) === true) {
// return matched item, index, array to yes_func
return true_fn(xs[i],i,xs) }}
// return array to no_func
return else_fn(xs) }
// what does this do?
@beoliver
beoliver / tree paths
Last active August 29, 2015 14:02
rose tree paths
import Data.Maybe
data Tree a = Node a [Tree a] deriving (Show)
t = Node 1 [ Node 2 [ Node 3 [] ], Node 4 [], Node 5 [ Node 6 [] ] ]
paths :: Tree a -> [[a]]
paths (Node n []) = [[n]]
paths (Node n xs) = map ((:) n . concat . paths) xs
// nested classes | static classes
public class Main {
public static void main(String[] args) {
Outer a = new Outer(6);
Outer.InnerB b1 = a.new InnerB();
Outer.InnerB b2 = new Outer(7).new InnerB();
Outer.InnerC c1 = new Outer.InnerC(8);
@beoliver
beoliver / gist:0076789ae02cc6824682
Created April 25, 2015 23:33
Simple Java thread example
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
public static void main(String[] args) {
String[] words = {"a", "b", "c", "a", "a", "r", "b", "c", "a"};
SharedValue sharedValue = new SharedValue();
@beoliver
beoliver / gist:81cf7d62ac88270aba42
Created April 25, 2015 23:45
simple example of using 'synchronized'
public class Main {
public static void main(String[] args) {
String[] words = {"a", "b", "c", "a", "a", "r", "b", "c", "a"};
SharedValue sharedValue = new SharedValue();
for (int i = 0; i <= 6; i += 3) {
@beoliver
beoliver / gist:410b00290ee1eef18ddb
Created April 26, 2015 08:10
java threading 'synchronized', each thread calls shared container once
public class Main {
public static void main(String[] args) {
String[] words = {"a", "b", "c", "a", "a", "c", "b", "c", "a"};
SharedValue sharedValue = new SharedValue();
for (int i = 0; i <= 6; i += 3) {
Thread t = new Thread(new Worker(words,i,i+2,"a",sharedValue));
@beoliver
beoliver / gist:0d090b4df27a81669a7f
Created April 26, 2015 14:40
multithreaded searching of an ArrayList
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String[] words = {"c", "b", "a", "a", "a", "r", "b", "c", "a", "b", "a", "c"};
SynchronisedInteger sharedValue = new SynchronisedInteger();
ArrayList<Thread> threads = new ArrayList<>();
@beoliver
beoliver / gist:d492a643d3f662f7f06a
Last active August 29, 2015 14:20
async "map" over 2d array list
import java.util.ArrayList;
// next step is to change type from A to B,
// map :: (a -> b) -> [a] -> [b]
public class Main {
public static void main(String[] args) {
PartitionedArrayList<Integer> xs = new PartitionedArrayList<Integer>(3);