Skip to content

Instantly share code, notes, and snippets.

View mpereira's full-sized avatar

Murilo Pereira mpereira

View GitHub Profile
/** @jsx React.DOM */
'use strict';
var TodoApplication = React.createClass({
taskStatuses: {
all: 'all',
todo: 'todo',
done: 'done'
},
(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)))
@mpereira
mpereira / nested_map_value_path.clj
Created February 27, 2014 02:10
Finding the path of a node in a tree.
(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]
@mpereira
mpereira / gist:9574586
Last active August 29, 2015 13:57
Determine if a point is within a triangle
(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))
$ 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": [
        {
require 'test/unit'
require 'test/unit/ui/console/testrunner'
def scrubble(array)
array.dup.inject([]) do |scrubbled, element|
scrubbled << array.reject { |e| e == element }
end
end
class ScrubbleTest < Test::Unit::TestCase

If you want to do something like

Then the output should contain "\"bob saget\",1234123412"

This is the step definition you would probably write

Then /^the output should contain "(.*)$" do |text|
  Output.string.should be_include(text)
# encoding: utf-8
# Copyright (C) 2011 by Murilo Pereira <murilo@murilopereira.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
battle_cries = [{ king_leonidas: 'This is where we fight! This is where they die!' },
{ he_man: 'By the power of Greyskull... I have the powerrr!' },
{ sindel: 'You are pathetic and weak.' },
{ shang_tsung: 'Your soul is mine!' },
{ klingons: 'Today is a good day to die!' }]
def from_mortal_kombat?(battle_cry)
[:sindel, :shang_tsung].include?(battle_cry.keys.first)
end

Remove trailing whitespace, convert tabs to spaces and file format to unix (remove ugly ^M carriage returns) from files using a single command:

vim -es -c 'set et ts=2 | bufdo retab | %s/\s\+$//e | set ff=unix | w' -c 'q' FILES

You could get your FILES using find

vim -es -c 'set et ts=2 | bufdo retab | %s/\s\+$//e | set ff=unix | w' -c 'q' $(find scripts -name '*.sh' -type f)

If in doubt, ask man.