Skip to content

Instantly share code, notes, and snippets.

View olivernn's full-sized avatar

Oliver Nightingale olivernn

View GitHub Profile
require 'delegate'
class Series < SimpleDelegator
def self.of(*xs)
new(xs)
end
def mean
@mean ||= sum / size.to_f
end
@olivernn
olivernn / set_test.rb
Last active April 5, 2017 13:21
Ruby Set#include? strangeness
require 'minitest/autorun'
require 'set'
class Foo
def initialize(s)
@s = s
end
def ==(other)
@s == other
{"version":"2.0.0-alpha.5","averageDocumentLength":10.5,"b":0.75,"k1":1.2,"fields":["name","content","person"],"documentVectors":[["-KgJdlp7WdeQO16yUyMd",[0,0.25774058577405856,1,0.25774058577405856,2,0.25774058577405856,3,0.25774058577405856,4,0.9112426035502958,5,0.18224852071005918,6,0.18224852071005918,7,0.9112426035502958,8,0.9112426035502958]],["-KgJhVbn3E_r8TqMyrP3",[5,0.22158273381294966,6,0.22158273381294966,9,0.2947368421052632,10,0.2947368421052632,11,1.1079136690647482,12,1.1079136690647482]]],"invertedIndex":[["ce",{"_index":2,"name":{"-KgJdlp7WdeQO16yUyMd":{}},"content":{"-KgJdlp7WdeQO16yUyMd":{}},"person":{}}],["connard",{"_index":1,"name":{"-KgJdlp7WdeQO16yUyMd":{}},"content":{"-KgJdlp7WdeQO16yUyMd":{}},"person":{}}],["droit",{"_index":10,"name":{"-KgJhVbn3E_r8TqMyrP3":{}},"content":{"-KgJhVbn3E_r8TqMyrP3":{}},"person":{}}],["et",{"_index":11,"name":{},"content":{"-KgJhVbn3E_r8TqMyrP3":{}},"person":{}}],["jacqu",{"_index":5,"name":{},"content":{},"person":{"-KgJdlp7WdeQO16yUyMd":{},"-KgJhVbn3E
@olivernn
olivernn / output
Created October 21, 2016 07:32
RubyWat Implicit String Concatenation
'abc'
[:program,
[[:string_literal, [:string_content, [:@tstring_content, "abc", [1, 1]]]]]]
'a' 'b' 'c'
[:program,
[[:string_concat,
[:string_concat,
[:string_literal, [:string_content, [:@tstring_content, "a", [1, 1]]]],
[:string_literal, [:string_content, [:@tstring_content, "b", [1, 5]]]]],
@olivernn
olivernn / Search.md
Created January 25, 2016 21:40
Lunr full text search on facets for faceted search

So many of your fields look like tags or facets, e.g. sector or market, so you full text search might not be the best way to search on these.

Instead if you had full text search on the tags themselves, e.g. full text search on all the possible values for sector or market, and then, with the resuts of this tag lookup, go and find companies that have this tag. The facted search, lookup by tags (or groups of tags) isn't really lunr's forte, but you could certainly use it for full text search of the tags.

var idx = lunr(function () {
  this.ref('id')
  this.field('name')
})
  
@olivernn
olivernn / lunr.contraction_filter.js
Created January 19, 2016 16:49
Better handling of English contractions in lunr.
lunr.contractionTrimmer = function (token) {
return token.replace(/('ve|n't|'d|'ll|'ve|'s|'re)$/, "")
}
lunr.Pipeline.registerFunction(lunr.stopWordFilter, 'contractionTrimmer')
var englishContractions = function (idx) {
idx.pipeline.after(lunr.trimmer, lunr.contractionTrimmer)
}
// Assuming a 'document' strucutre like below to represent a single icon
{
"id": "some-unique-id",
"tags": ["tag1", "tag2", "tag3"],
"title": "My Awesome Icon"
}
// You can then set up an index with the following, I put a boost on the tags, but it depends on your data
@olivernn
olivernn / pbcopy.plist
Created September 17, 2015 19:22
pbcopy plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>localhost.pbcopy</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/pbcopy</string>
</array>
@olivernn
olivernn / lunr.js
Created July 6, 2015 19:19
lunr-issue-162
/**
* lunr - http://lunrjs.com - A bit like Solr, but much smaller and not as bright - 0.5.10-issue162
* Copyright (C) 2015 Oliver Nightingale
* MIT Licensed
* @license
*/
;(function(){
/**
@olivernn
olivernn / README.md
Last active August 29, 2015 14:00
Paginated results

I think response and collection are the two classes where the bulk of the stuff is happenening, the importer is just an example of the external api. Actually looking back at this I think it could definitly be improved but I guess its an example of how to do it.

I recently did something similar, this time it was streaming a file from s3, but yielding the file line by line. The AWS SDK just gives you the s3 object chunk by chunk as its read from the socket, so I had to buffer and then yield as many lines as possible from each chunk. I returned an enumerator to do this rather than mixing in enumerable, I can't really share this code though, which is a shame cos I thought it was a nice implementation at the time.