Skip to content

Instantly share code, notes, and snippets.

Murilo Pereira mpereira

View GitHub Profile
View keybase.md

Keybase proof

I hereby claim:

  • I am mpereira on github.
  • I am mpereira (https://keybase.io/mpereira) on keybase.
  • I have a public key ASCLp1gBxEh02WiE0zRjHUtGLvQtjFOKmX9OVyNYsBCV_Qo

To claim this, I am signing this object:

@mpereira
mpereira / split_by.clj
Last active Jul 18, 2017
Splits coll by pred. Returns a vector with a vector where (pred item) returns true followed by a vector where (pred item) returns false.
View split_by.clj
(defn split-by
"Splits coll by pred. Returns a vector with a vector where (pred item) returns
true followed by a vector where (pred item) returns false.
Example:
(split-by pos? [0 1 2 -1 3 -2 4 -3])
=> [[1 2 3 4] [0 -1 -2 -3]]"
[pred coll]
(reduce (fn [split item]
(update split (if (pred item) 0 1) conj item))
@mpereira
mpereira / substring_offsets.clj
Last active Jun 16, 2017
Returns a vector of [start end] offsets for substrings in s.
View substring_offsets.clj
(defn substring-offsets
"Returns a seq of [start end] offsets for substrings in s."
[s substring]
(when-not (empty? substring)
(let [s (.toLowerCase s)
substring (.toLowerCase substring)
s-length (count s)
substring-length (count substring)]
(loop [offset 0
offsets nil]
@mpereira
mpereira / merge_sort.js
Last active May 14, 2017
Merge sort implementation in JavaScript.
View merge_sort.js
function merge(xs, leftStart, leftEnd, rightStart, rightEnd) {
var i = leftStart;
var j = rightStart;
var merged = [];
var currentLeft, currentRight;
// Merge.
while (i <= leftEnd || j <= rightEnd) {
currentLeft = xs[i];
currentRight = xs[j];
View find_pairs_sum_equal_k.clj
(defn find-pairs-sum-equal-k [xs k]
(let [x-indices (into {} (map-indexed (fn [idx x] [x idx]) xs))]
(keep (fn [[x idx]]
(if-let [x-complement-idx (get x-indices (- k x))]
(when (not= idx x-complement-idx)
[x (- k x)])))
x-indices)))
(find-pairs-sum-equal-k (range 0 20) 12)
;; => ([0 12] [7 5] [1 11] [4 8] [3 9] [12 0] [2 10] [11 1] [9 3] [5 7] [10 2] [8 4])
View resultados_parciais_presidente.md
$ curl -s 'http://divulga.tse.jus.br/2014/divulgacao/oficial/143/dadosdivweb/br/br-0001-e001431-w.js' -H 'Referer: http://divulga.tse.jus.br/oficial/index.html' -H 'Cache-Control: no-cache' | python -mjson.tool
{
    "a": "22212872",
    "c": "92334638",
    "cand": [
        {
@mpereira
mpereira / gist:9574586
Last active Aug 29, 2015
Determine if a point is within a triangle
View gist:9574586
(defn point-in-triangle? [[px py] [[p0x p0y] [p1x p1y] [p2x p2y]]]
(let [n (* 0.5
(+ (* (- p1y) p2x)
(* p0y (+ (- p1x) p2x))
(* p0x (- p1y p2y))
(* p1x p2y)))
sign (if (neg? n) -1 1)
s (* sign
(+ (* p0y p2x)
(- (* p0x p2y))
@mpereira
mpereira / nested_map_value_path.clj
Created Feb 27, 2014
Finding the path of a node in a tree.
View nested_map_value_path.clj
(defn map-zip [m]
(clojure.zip/zipper #(or (map? %) (map? (nth % 1)))
#(seq (if (map? %) % (nth % 1)))
(fn [x children]
(if (map? x)
(into {} children)
(assoc x 1 (into {} children))))
m))
(defn path [m value]
View core.cljs
(ns om-laboratory.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[clojure.string :as string]))
(enable-console-print!)
(defn index-of [coll val]
(first (keep-indexed #(if (= %2 val) %1) coll)))
View app.jsx
/** @jsx React.DOM */
'use strict';
var TodoApplication = React.createClass({
taskStatuses: {
all: 'all',
todo: 'todo',
done: 'done'
},
You can’t perform that action at this time.