This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Heap<T> { | |
selectMax: (l: T, r: T) => boolean; | |
data: T[]; | |
constructor(selectMax: (l: T, r: T) => boolean) { | |
this.selectMax = selectMax; | |
this.data = []; | |
} | |
length(): number { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Given a start node and a function which provides an Iterable of adjacent nodes for any node, | |
* perform a breadth-first traversal of the graph and yield each visited node together with | |
* the minimum number of steps needed to reach that node from the start node. | |
*/ | |
function *bft<T>(start: T, adjacent: (node: T) => Iterable<T>): IterableIterator<[T, number]> { | |
const visited: Set<T> = new Set(); | |
const queue: T[] = [start]; | |
visited.add(start); | |
var depth = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lib | |
( playRepeatedly, | |
firstStep | |
) where | |
import System.IO | |
data GameStep = Guess { animalName :: String } | |
| Question { | |
question :: String, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
from urllib import request | |
import os | |
if not os.path.isfile('count_1w.txt'): | |
request.urlretrieve( | |
"https://norvig.com/ngrams/count_1w.txt", | |
"count_1w.txt") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Iterators = (function() { | |
var each, map, filter, reduce, toArray, toObject; | |
function _map(iter, f) { | |
return { | |
hasNext: iter.hasNext, | |
next: function() { return f(iter.next()); } | |
}; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.function.*; | |
public final class Tunnel<E extends Exception> { | |
public static <E extends Exception, R> R call(Class<? extends E> exceptionClass, Function<Tunnel<E>, R> f) throws E { | |
try { | |
return f.apply(new Tunnel<>()); | |
} catch (TunnelledException e) { | |
throw exceptionClass.cast(e.getCause()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.youdevise.lofty | |
import org.specs2.mutable._ | |
import scala.language.dynamics | |
trait State[S, A] { | |
val runState: Function[S, (S, A)] | |
def flatMap[B](f: A => Function[S, (S, B)]):Function[S, (S, B)] = (state:S) => { | |
val (newState, value) = runState(state) | |
f(value)(newState) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import urllib2 | |
import argparse | |
import json | |
parser = argparse.ArgumentParser(description="Fetch some JSON") | |
parser.add_argument("url") | |
args = parser.parse_args() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Nonchalantly<T> { | |
static <T, E extends Throwable> T invoke(Nonchalantly<T> f) throws E { | |
try { | |
return f.run(); | |
} catch (Throwable e) { | |
throw (E) e; | |
} | |
} | |
T run() throws Throwable; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String ofNullableExample(Map<String, String> map) { | |
return Optional.ofNullable(map.get(key)) | |
.orElseThrow(() -> new ItemNotFoundException(key)); | |
} | |
public String nullCheckingExample(Map<String, String> map) { | |
String valueAtKey = map.get(key); | |
if (valueAtKey == null) { | |
throw new ItemNotFoundException(key); | |
} |
NewerOlder