Skip to content

Instantly share code, notes, and snippets.

View lovasoa's full-sized avatar
🎯
Focusing

Ophir LOJKINE lovasoa

🎯
Focusing
View GitHub Profile
@lovasoa
lovasoa / csv-docx-template-fill.js
Created August 5, 2015 15:40
Fill docx templates with values from a csv file
#!/usr/bin/env node
/**
@author Ophir LOJKINE
@license BSD_3Clause
**/
var fs = require('fs');
var csvparse = require('csv-parse');
var DocxTemplater = require('docxtemplater');
var util = require('util');
@lovasoa
lovasoa / russian-words-trie.json
Last active November 23, 2015 22:05
Russian words trie
This file has been truncated, but you can view the full file.
{"а":{"д":{"а":{"ж":{"и":{"о":{}}},"м":{"а":{"н":{"т":{"а":{},"е":{},"у":{},"о":{"м":{}}}}},"о":{"в":{"а":{}}}},"п":{"т":{"е":{"р":{"а":{"м":{"и":{}},"х":{}},"е":{},"у":{},"ы":{},"о":{"в":{},"м":{}}}},"а":{"ц":{"и":{"и":{},"й":{},"ю":{},"я":{"м":{"и":{}},"х":{}},"е":{"й":{}},"о":{"н":{"н":{"а":{"я":{}},"о":{"е":{},"й":{},"м":{"у":{}},"г":{"о":{}}},"у":{"ю":{}},"ы":{"е":{},"й":{},"м":{"и":{}},"х":{}}}}}}}},"и":{"в":{"е":{"н":{}},"н":{"а":{"я":{}},"о":{"е":{},"й":{},"м":{"у":{}},"ю":{},"г":{"о":{}}},"ы":{"е":{},"й":{},"м":{"и":{}},"х":{}},"у":{"ю":{}}}},"р":{"у":{"й":{"с":{"я":{}},"т":{"е":{"с":{"ь":{}}}}},"ю":{"т":{"с":{"я":{}}},"с":{"ь":{}},"щ":{"а":{"я":{"с":{"я":{}}}},"е":{"е":{"с":{"я":{}}},"й":{"с":{"я":{}}},"м":{"у":{"с":{"я":{}}},"с":{"я":{}}},"г":{"о":{"с":{"я":{}}}}},"и":{"е":{"с":{"я":{}}},"й":{"с":{"я":{}}},"м":{"и":{"с":{"я":{}}},"с":{"я":{}}},"х":{"с":{"я":{}}}},"у":{"ю":{"с":{"я":{}}}}}},"я":{"с":{"ь":{}}},"е":{"м":{"а":{"я":{}},"о":{"е":{},"й":{},"м":{"у":{}},"г":{"о":{}}},"с":{"я":{}},"у":{"ю":
@lovasoa
lovasoa / create-trie.js
Created November 23, 2015 22:09
Create a word trie from a dictionnary file
var fs = require("fs");
var dic = fs.readFileSync("/dev/stdin", "utf8").split("\n");
var trie = {};
for(var i=0; i<dic.length; i++) {
var w = dic[i];
var t = trie;
for(var j=0; j<w.length; j++) {
var l = w[j];
if(!t[l]) t[l] = {};
@lovasoa
lovasoa / partial-evaluation.js
Last active December 28, 2023 05:56
Partial-evaluation for javascript.
/** pe
* @argument f: the multiple-argument function to turn into a partially-evaluatable
* @returns : A single-argument function that applies its argument as the first argument of f, and returns the partially-evaluated function
* @exemple: pe((a,b)=>a+b)(9)(1) === 10
*/
function pe(f, context, args) {
if(!args) args = [];
if (args.length === f.length) return f.apply(context, args);
return function partial (a) {
var args_copy = args.concat.apply(args, arguments);
@lovasoa
lovasoa / memoize.js
Created December 15, 2015 22:15
Memoization implementation in javascript
function memoize(f) {
var dict = {};
return function() {
var args = JSON.stringify(arguments);
if(dict.hasOwnProperty(args)) return dict[args];
var res = f.apply(null, arguments);
dict[args] = res;
return res;
}
}
@lovasoa
lovasoa / coherent_selection.js
Created January 13, 2016 18:23
Forbid "incoherent" selection in a document (selection that can not be expressed as a new tag in the DOM tree)
document.onmousemove = function(evt) {
var s = window.getSelection();
if (s.rangeCount === 0) return;
while(s.rangeCount > 1) s.removeRange(s.getRangeAt(1));
r = s.getRangeAt(0);
if (r.startContainer.parentNode !== r.endContainer.parentNode) {
r.setEnd(r.startContainer, r.startContainer.length);
evt.preventDefault();
}
}
@lovasoa
lovasoa / fast-genetics.js
Created January 22, 2016 15:53
Fast genetic library in javascript
class Genetic {
constructor(conf) {
this.setConfiguration(conf);
this.population = new Int32Array(this.conf.genomeLength * this.conf.populationSize);
for (let i=0; i<this.conf.populationSize; i++) this.seedGenome(this.getGenome(i));
}
setConfiguration(conf) {
this.conf = Object.assign({
populationSize: 100,
@lovasoa
lovasoa / mapfilter.js
Created February 21, 2016 18:56
mapfilter function, just like map, but can also discard elements. Equivalent to array.map(fun).filter(n => n !== undefined)
function mapfilter(array, fun) {
// Equivalent to array.map(fun).filter(x => x !== undefined)
var res = [];
for(var i=0; i<array.length; i++) {
var r = fun(array[i], i, array);
if (r !== void 0) res.push(r);
}
return res;
}
@lovasoa
lovasoa / fromroman.js
Last active February 26, 2016 19:01
Write a number in roman numerals efficiently in javascript
function fromRoman(str) {
// Parse str as a roman numeral
var values = {I:1, V:5, X:10, L:50, C:100, D:500, M:1000};
str = str.toUpperCase();
var n = 0, v1 = 0, v2 = values[str[0]];
for (var k = 0; k < str.length; k++) {
var v1 = v2, v2 = k+1 < str.length ? values[str[k+1]] : 0;
n += (v2 > v1) ? -v1 : v1;
}
return n;
@lovasoa
lovasoa / trees.elm
Last active March 4, 2016 21:07
A little HTML tree editor in Elm, that I did as my first exercise in the language
import Html exposing (Html, Attribute, text, toElement, div, input)
import Html.Attributes exposing (..)
import Html.Events exposing (on, targetValue)
import Signal exposing (Address)
import StartApp.Simple as StartApp
import Debug
type Tree a
= Empty
| Node a (Tree a) (Tree a)