Skip to content

Instantly share code, notes, and snippets.

@ZacLN
ZacLN / lex_identifier.jl
Created February 9, 2017 20:32
allocationless lexing
using Tokenize
import Tokenize.Lexers:peekchar, readchar, is_identifier_char, accept_batch, eof
import Tokenize.Tokens: FUNCTION, ABSTRACT, IDENTIFIER, BAREMODULE, BEGIN, BITSTYPE, BREAK, CATCH, CONST, CONTINUE, DO, ELSE, ELSEIF, END, EXPORT, FALSE, FINALLY, FOR, FUNCTION, GLOBAL, LET, LOCAL, IF, IMMUTABLE, IMPORT, IMPORTALL, MACRO, MODULE, QUOTE, RETURN, TRUE, TRY, TYPE, TYPEALIAS, USING, WHILE
function tryread(l, str, k, start_loc)
for s in str
c = readchar(l)
if c!=s
using Distributions, KernelDensity, QuadGK
function gini(x)
n = length(x)
xx = sort(x)
2*(sum(collect(1:n).*xx))/(n*sum(xx))-1
end
gini(d::Exponential) = 0.5
gini(d::Beta) = (2/d.α)*beta(d.α+d.β, d.α+d.β)/(beta(d.α,d.α)*beta(d.β, d.β))
import LanguageServer: get_docs, Location
import Base.REPLCompletions: get_value
function modnames(m::AbstractString, top)
s = Symbol(m)
eval(:(using $s))
M, exists = get_value(s, Main)
if !(s in keys(top))
modnames(M, top)
ns = Expr(:module, true, :Namespace, Expr(:block))
meth = Expr(:type, true, :Meth, Expr(:block, :parameters, :(file::AbstractString), :(line::Int)))
func = Expr(:type, true, :Func, Expr(:block, :(methods::Vector{Meth})))
push!(ns.args[3].args, Expr(:type, true, :Meth, Expr(:block, :parameters, :(file::AbstractString), :(line::Int))))
push!(ns.args[3].args, Expr(:type, true, :Func, Expr(:block, :(methods::Vector{Meth}))))
M = Core
using LanguageServer
import LanguageServer: Location, URIParser
import LanguageServer: get_docs, get_sym
const basenames = Symbol.(Base.REPLCompletions.completions("Base.",5)[1])
function cachemodule(modulename::AbstractString)
mdir = Pkg.dir(modulename)
!Base.isdir(mdir) && error("Module `$modulename` is not installed")
jlfiles = filter!(f->ismatch(r".jl$", f), readdir(joinpath(mdir,"src")))
# Structure
# client sends : message::String
# JSON.parse() : message::String -> message_json::Dict
# Request() : message_json::Dict -> req::Request{method,parametertype}
# Respond{m,pt}() : req::Request{m,pt} -> resp::Response{m,returntype}
# JSON.json() : resp::Response{m,returntype} -> retmessage::String
# return : retmessage::String
# A whole load of these for all parameter structures listed in the language-server protocol
#G.n::Int64
#G.d::Int64
#G.q::Int64
#G.grid::Array{Float64,G.n,G.d}
#G.lvl_s::Array{Float64,G.n,G.d}
#G.lvl_l::Array{Float64,G.q+1} indices partitioning 1:G.n
function basis_func(x::Float64,xij::Float64,mi::Float64) :inline
if (mi==1)
@ZacLN
ZacLN / junk
Last active August 29, 2015 14:06
slow code
#G.n::Int64
#G.d::Int64
#G.q::Int64
#G.grid::Array{Float64,G.n,G.d}
#G.lvl_s::Array{Float64,G.n,G.d}
#G.lvl_l::Array{Float64,G.q+1} indices partitioning 1:G.n
using Sparse