Skip to content

Instantly share code, notes, and snippets.

View masyukun's full-sized avatar

Matthew Royal masyukun

View GitHub Profile
@masyukun
masyukun / string2color.js
Created May 4, 2020 20:00
Color-hash any string to a stable color
// DONE: Translated to XQuery
if (!String.prototype.hashCode){
String.prototype.hashCode = function() {
var hash = 0, i, chr, len;
if (this.length == 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
@masyukun
masyukun / keybase.md
Created January 28, 2020 19:37
Keybase proof of identity for github

Keybase proof

I hereby claim:

  • I am masyukun on github.
  • I am masyukun (https://keybase.io/masyukun) on keybase.
  • I have a public key ASDa8I6gfiJNjNvyPi6hX-d-z-WW4c1wnQCKNRwoz6zcpgo

To claim this, I am signing this object:

@masyukun
masyukun / documentInventory.js
Last active January 15, 2020 01:07
MarkLogic document type inventorying script. This makes a list of the unique document types in the current database, and estimates how many of each of them there are.
/**
* Document Type Inventory in MarkLogic v0.1
*
* This version exists as a GitHub Gist at https://gist.github.com/masyukun/063f59a7f7d84eeaeeef2f23d05cad59
*
* @license
* Copyright (c) 2020 Matthew Royal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@masyukun
masyukun / recipeTools.js
Last active January 3, 2021 19:02
JavaScript functions to work with exported Cronometer recipes
/**
* Recipe Tools v0.1
*
* This version exists as a GitHub Gist at https://gist.github.com/masyukun/64a3490a464acaa3b9af6819f10dfbf4
*
* @license
* Copyright (c) 2020 Matthew Royal
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@masyukun
masyukun / treemap-rest.xqy
Created May 16, 2017 18:09
Generate a treemap data structure for visualization with highcharts of files in a MarkLogic database.
xquery version "1.0-ml";
module namespace treemap = "http://matthewroyal.com/marklogic/ferret/filetypetreemap";
declare variable $sampleSize := 100;
declare variable $sampleThreshold := 1000;
declare function treemap:get(
$context as map:map,
$params as map:map
xquery version "1.0-ml";
declare namespace p = "http://www.mycompany.com";
declare option xdmp:mapping "false";
(:~
Returns z-score for the specified confidence value
For confidence values not in the lookup table,
(:~
This module converts an XML document into a JSON document.
- Element ordering is preserved.
- Attributes and namespaces are ignored.
- Identical element names become JSON arrays.
Example usage:
let $uri := "somedoc.xml"
let $doc := fn:doc($uri)
return jsontools:jsonify($doc)
@masyukun
masyukun / solveCubicEquation
Created May 24, 2015 17:50
Solve a cubic polynomial for x, given coefficients a, b, c, d in standard form = ax^3 - bx^2 + cx - d = 0. Uses homegrown cubic root function sqrt3() because Xquery's math library cannot raise a negative number to a decimal power. http://www.math.vanderbilt.edu/~schectex/courses/cubic/
declare function local:solveCubicEquation($a as xs:double, $b as xs:double, $c as xs:double, $d as xs:double) {
let $big := ((-math:pow($b,3.0)) div (27.0 * math:pow($a,3.0))) + (($b * $c) div (6.0 * math:pow($a,2.0))) - ($d div (2.0 * $a))
let $small := ($c div (3.0 * $a)) - (math:pow($b,2.0) div (9.0 * math:pow($a,2.0)))
let $tail := ($b div (3.0 * $a))
let $x := math:pow($big + math:sqrt(math:pow($big,2.0) + math:pow($small,3.0)), (1.0 div 3.0)) + local:sqrt3( $big - math:sqrt(math:pow($big,2.0) + math:pow($small,3.0)) ) - $tail
return $x
};
@masyukun
masyukun / sqrt3
Created May 24, 2015 17:46
cube root estimation
(:
Estimate the cube root of a number.
WARNINGS: 1) Does not work well with many digits due to xs:double size limitation.
2) Does not generate complex numbers.
based on http://www4.wittenberg.edu/academics/mathcomp/bjsdir/CubeRootTalk.pdf
:)
declare function local:sqrt3($number as xs:double) {
let $answer := ()
@masyukun
masyukun / printAsset
Created September 6, 2013 00:29
Recursively prints the assets in the current Android project's AssetManager.
String repeatString(String string, int repetitions) {
StringBuilder sb = new StringBuilder();
if (repetitions <= 0) {
return sb.toString();
}
for (int ii = 0; ii <= repetitions; ++ii) {
sb.append(string);