Skip to content

Instantly share code, notes, and snippets.

def busyLoop(time: Int, threads: Int) = Action { implicit r =>
val started = System.currentTimeMillis()
val workers = (0 until threads).map { _ =>
val out = new Thread({ () =>
val digest = MessageDigest.getInstance("SHA-256")
while (started + time * 1000 >= System.currentTimeMillis()) {
digest.update(UUID.randomUUID().toString.getBytes(StandardCharsets.US_ASCII))
}
})
out.start()
import * as Rx from "rxjs";
import * as CP from "child_process";
function spawn(cmd: string, args: string[] = []): Rx.Observable<ProcessOutput> {
return Rx.Observable.create(observer => {
let process = CP.spawn(cmd, args);
process.addListener("error", err => {
observer.error(err);
});
process.addListener("exit", code => {
lazy val generateBuildConfig = taskKey[Seq[File]]("Generates `build.conf`.")
generateBuildConfig := {
val dest = (resourceManaged in Compile).value / "build.conf"
val hash = Process("git rev-parse HEAD").!!.trim()
val time = java.time.Instant.now()
streams.value.log.info(s"Generating a build.conf: $dest")
IO.write(dest,
s"""build.info {
| hash: "$hash"
netsh interface portproxy add v4tov4 listenport=443 listenaddress=127.0.0.1 connectport=9443 connectaddress=127.0.0.1 protocol=tcp
netsh interface portproxy delete
import sys, os, itertools, collections, zipfile
if len(sys.argv) < 2:
sys.stderr.writelines("Usage: python ziptree.py <path-to-zip>")
sys.exit(-1)
Entry = collections.namedtuple('Entry', ['name', 'children'])
def tree_of_zip(path_to_zip, only_dir=False):
stack = [Entry("", [])]
import re
# Strong Password Detection
def is_strong_password(src):
return all(re.compile(e).search(src) for e in [
r'.{8}',
r'[a-z]',
r'[A-Z]',
r'[0-9]'
])
@hisui
hisui / FlatMap.scala
Last active September 13, 2017 00:03
flatMap for Flow of akka-streams
import akka.stream.Attributes
import akka.stream.FlowShape
import akka.stream.Inlet
import akka.stream.Outlet
import akka.stream.scaladsl.Flow
import akka.stream.stage.GraphStage
import akka.stream.stage.GraphStageLogic
import akka.stream.stage.InHandler
import akka.stream.stage.OutHandler
def waitForSIGTERM(cv: AnyRef = new Object()) {
Runtime.getRuntime.addShutdownHook(
new Thread(() => cv.synchronized(cv.notify()))
)
@tailrec
def loop(): Unit = try cv.synchronized(cv.wait())
catch {
case _: InterruptedException => loop()
}
loop()
@hisui
hisui / test.py
Last active September 3, 2017 08:28
from itertools import *
for e in takewhile(lambda e: e != 1, accumulate(chain([7], count()), lambda n, _: n // 2 if (n % 2) == 0 else n * 3 + 1)):
print(e)
@hisui
hisui / AutoCloseable2.java
Created June 27, 2017 04:06
As it closes, recursively closing every "AutoCloseable" field marked with the "Owns" annotation as well.
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.stream.Stream;
import java.util.stream.Collectors;
public interface AutoCloseable2 extends AutoCloseable {
@Override
public default void close() throws Exception {
for (Field field : getClass().getDeclaredFields()) {