Skip to content

Instantly share code, notes, and snippets.

Avatar

Matthew Royal masyukun

View GitHub Profile
@masyukun
masyukun / string2color.js
Created May 4, 2020
Color-hash any string to a stable color
View string2color.js
// 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 Jan 28, 2020
Keybase proof of identity for github
View keybase.md

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 Jan 15, 2020
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.
View documentInventory.js
/**
* 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 Jan 3, 2021
JavaScript functions to work with exported Cronometer recipes
View recipeTools.js
/**
* 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
Generate a treemap data structure for visualization with highcharts of files in a MarkLogic database.
View treemap-rest.xqy
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
View statisticalSamplingQuery.xqy
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,
View jsonify.xqy
(:~
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
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/
View solveCubicEquation
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
cube root estimation
View sqrt3
(:
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 Sep 6, 2013
Recursively prints the assets in the current Android project's AssetManager.
View printAsset
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);