Skip to content

Instantly share code, notes, and snippets.

View v6ak's full-sized avatar

Vít Šesták v6ak

View GitHub Profile
// Mohli bychom se hádat, jestli tato funkce mění stav, ale referenčně transparentní rozhodně není:
def f = new java.io.File("/tmp").exists
// Tato funkce nemění stav, ale referenčně transparentní není:
def g(x: String) = new java.io.File(x).exists
object Foo{
var invocationCount: Int = 0
def h(x: Int){
@v6ak
v6ak / gist:7847548
Last active December 30, 2015 15:19
// Some plain classes with subclasses
class Foo
class SubFoo extends Foo
class SubSubFoo extends SubFoo
class SubSubSubFoo extends SubSubFoo
// A generic class
class Bar[T <: Foo](val foo: T) {
}
// Toto odsazení je divné:
case class User(nick: String,
name: String,
email: String)
// Spíš bych použil:
case class User(
nick: String,
name: String,
@v6ak
v6ak / gist:7958454
Last active December 31, 2015 08:09
Benchmark provedený kritizovaným způsobem.
$ java -jar clojure-1.5.1.jar
Clojure 1.5.1
user=> (defn plus-2-1 [n] (+ 2 n)) ; stary a nudny zpusob
#'user/plus-2-1
user=> (def plus-2-2 (partial + 2)) ; partial application
#'user/plus-2-2
user=> (def plus-2-3 (comp inc inc)) ; kompozice funkci
#'user/plus-2-3
user=> "první měření"
"první měření"
@v6ak
v6ak / plus-2-1.clj
Created December 14, 2013 12:33
Benchmark provedený doporučeným způsobem. Na každou variantu byla spuštena nová JVM. Verze Javu, OS i CPU jsou totožné s tímto benchmarkem: https://gist.github.com/v6ak/7958454
$ java -jar clojure-1.5.1.jar
Clojure 1.5.1
user=> (defn plus-2 [n] (+ 2 n)) ; stary a nudny zpusob
#'user/plus-2
user=> (time (dotimes [n 10000000] (plus-2 n)))
"Elapsed time: 144.448077 msecs"
nil
user=> (time (dotimes [n 10000000] (plus-2 n)))
"Elapsed time: 85.961736 msecs"
nil
object NewHelloWorld extends App{
println("Hello world!")
}
val key: PGPPublicKey = ...
def encrypt(plaintext: String): String = {
val baos = new ByteArrayOutputStream()
val literalDataGenerator = new PGPLiteralDataGenerator()
val comData = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZLIB) // I've also tried other compression algorithms
val bytes = plaintext.getBytes("utf-8")
val BufferSize = 1 << 16
import resource.managed // The "managed" method with for is just a nice replacement of try/catch/finally{x.close()}
for{
// An ugly hack for Bittorrent sync; hope to write more later
// separate function for easier debugging
show = function(x){console.log(["X:"+x.folders.map(function(x){return {folder: x, active_peers: x.peers.filter(function(peer){return peer.down_diff || peer.up_diff;})};}).filter(function(x){return x.active_peers.length > 0;}).map(function(x){return x.folder.name;})])}
var origRequest = utWebUI.request; utWebUI.request= function(a, e, d, b){return origRequest.call(this, a, function(f){e && e(f); console.log(f.folders);}, d, b); };
@v6ak
v6ak / fstab-part-largetmp
Created September 18, 2014 08:54
My optimizations for /var/log and /tmp/large
/dev/mapper/largetmp1 /tmp/large ext4 noauto,nosuid,noatime,delalloc,nobarrier,data=writeback,commit=36000 0 0
#include <iostream>
#include "json.h"
using namespace std;
int main() {
Json::Number num(3.14159265358972);
Json::String s("5.\" ' \1 0");
Json::Boolean t(true);
Json::Boolean f(false);