Skip to content

Instantly share code, notes, and snippets.

View martijndwars's full-sized avatar

Martijn Dwars martijndwars

View GitHub Profile
@martijndwars
martijndwars / rc4drop.js
Last active December 20, 2015 14:59
Nodes crypto module provides a rc4 cipher, but it lacks the ability to drop the first bytes from the keystream. This gist simply encrypts as many bytes as you want to drop, effectively causing those bytes from the keystream to get dropped. For full implementations of RC4Drop, please see: - https://github.com/gwjjeff/cryptojs - https://gist.githu…
crypto = require 'crypto'
class RC4
constructor: (@key, drop) ->
@cipher = crypto.createCipher 'rc4', key
if drop > 0
this.cipher.update new Buffer drop
encrypt: (data) ->
@cipher.update data
@martijndwars
martijndwars / valuation.r
Last active December 25, 2015 14:09
R code to calculate option value recursively backward in the binomial asset pricing model.
p <- 1/2;
u <- 2;
d <- 1/u;
r <- 1/4;
v <- function(n, s) {
if (n == 3) {
return(pmax(s-10, 0));
}
@martijndwars
martijndwars / OptionValue.scala
Last active January 4, 2016 12:29
Algorithm to value a path-independent American put option in the binomial model. Up-factor u=2, down-factor d=1/u, s=4 and expiry is at time N, such that the risk neutral probabilities p=q=1/2. The function g determines the intrinsic value (value if exercises immediately). By changing the function g one can value different kinds of American opti…
object OptionValue {
val N = 2
val u: Double = 2
val d: Double = 1/u
def main(args: Array[String]): Unit = {
println(v(0,4,(s: Double) => 0.0 max (4.0-s)))
}
def v(n: Double, s: Double, g: Double => Double): Double =
@martijndwars
martijndwars / combinations.str
Last active January 31, 2019 15:25
Permutations and combinations of a list in StrategoXT
/**
* Given a length m and elements xs, generate all combinations of length m
* from the elements in xs.
*
* @type (int, List(a)) -> List(List(a))
*/
combinations:
(0, _) -> [[]]
combinations:
@martijndwars
martijndwars / names.nab
Last active December 15, 2015 09:11
NaBL required reference
module names
namespaces
Global
binding rules
Class(c, _):
defines Global c
@martijndwars
martijndwars / Encrypted.java
Created March 6, 2016 21:51
Web push in Java
import java.security.PublicKey;
public class Encrypted {
private final PublicKey publicKey;
private final byte[] salt;
private final byte[] ciphertext;
public Encrypted(final PublicKey publicKey, final byte[] salt, final byte[] ciphertext) {
this.publicKey = publicKey;
this.salt = salt;
org.metaborg.core.analysis.AnalysisException: Analysis for eclipse:///MiniJava/tests/lab6/constraints/main-class/main-class-instantiation.spt failed
at org.metaborg.spoofax.core.analysis.legacy.StrategoAnalyzer.analyze(StrategoAnalyzer.java:203) ~[org.metaborg.spoofax.core_2.0.0.20160612-002403-master.jar:na]
at org.metaborg.spoofax.core.analysis.legacy.StrategoAnalyzer.analyze(StrategoAnalyzer.java:99) ~[org.metaborg.spoofax.core_2.0.0.20160612-002403-master.jar:na]
at org.metaborg.spoofax.core.analysis.legacy.StrategoAnalyzer.analyze(StrategoAnalyzer.java:1) ~[org.metaborg.spoofax.core_2.0.0.20160612-002403-master.jar:na]
at org.metaborg.core.analysis.AnalysisService.analyze(AnalysisService.java:27) ~[org.metaborg.core_2.0.0.20160612-002403-master.jar:na]
at org.metaborg.spoofax.core.analysis.SpoofaxAnalysisService.analyze(SpoofaxAnalysisService.java:17) ~[org.metaborg.spoofax.core_2.0.0.20160612-002403-master.jar:na]
at org.metaborg.spoofax.core.analysis.SpoofaxAnalysisService.analyze(SpoofaxAnalysis
### Keybase proof
I hereby claim:
* I am martijndwars on github.
* I am martijndwars (https://keybase.io/martijndwars) on keybase.
* I have a public key ASCjzLNxLHlexZfNDGu9sVMBtIxE-kax2HPwmJh5rPWDtgo
To claim this, I am signing this object:
@martijndwars
martijndwars / push.js
Created August 30, 2016 21:43
push.js
$(function () {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then(initialiseState);
} else {
console.warn('Service workers are not supported in this browser.');
}
});
function initialiseState() {
if (!('showNotification' in ServiceWorkerRegistration.prototype)) {
@martijndwars
martijndwars / notes.md
Last active September 27, 2016 17:59
IN4303 Lab 2 notes

This document explains some aspects of Spoofax that we got questions about. It also describes some common mistakes and pitfalls. Finally, it describes Spoofax' internals more in-depth, with the goal of clarifying some of its behaviour.

  • Disambiguation tests. Disambiguation tests are tests of the form test ... [[1+2+3]] parse to [[(1+2)+3]]. SPT will parse both fragments and compare the ASTs. A common mistake is to introduce a constructor when parsing parenthesized expressions, e.g. Exp.Parens = <(<Exp>)>. When you make this mistake, the AST for the second fragment will contain Parens nodes and hence be different from the AST for the first fragment.

The solution is to not introduce a constructor. This is semantically cleaner anyway, as a Parens node does not convey any semantic content (remember: the AST is a tree, not a string, and as such it already encodes what you would otherwise achieve by adding parenthesis). Spoofax will show an error "Missing bracket attribute or constructor name" whi