Skip to content

Instantly share code, notes, and snippets.

View pbalduino's full-sized avatar

Plínio Balduino pbalduino

View GitHub Profile
// Expectation: [1, 8, 6, 4, 3, 5, 7, 2]
var teams = [1, 2, 3, 4, 5, 6, 7, 8];
var matches = [];
for(var pos = 0; pos < teams.length / 2; pos++)
{
var even = pos % 2 === 0;
var ta = teams[pos];
var tb = teams[(teams.length - 1) - pos];
@pbalduino
pbalduino / install-java.sh
Created April 21, 2015 16:54
install-java.sh
#!/bin/sh
cd /home/amanda/Downloads
rm -rf /opt/jvm
rm -rf jre1.8.0_45
mkdir /opt/jvm
tar -zxf jre-8u45-linux-i586.tar.gz -C /opt/jdk
@pbalduino
pbalduino / primitive.clj
Last active August 29, 2015 14:19
Primitive
(def numeros (range 0 10000))
(def vetor (into-array Long/TYPE numeros))
(defmacro benchmark [code]
`(time (first (doall (repeatedly 1000 (fn [] ~code))))))
(benchmark (reduce + numeros))
; "Elapsed time: 123.946807 msecs"
(defn random-number [_]
(rand-int 22))
(defn a-validate [valor]
(> valor 10))
(defn a-errorhandler [ag ex]
(println "Error: " (.getMessage ex)))
(defn a-watcher [ref chave antigo novo]
@pbalduino
pbalduino / agent-watchers.clj
Last active August 29, 2015 14:14
Agent watchers
;; Primeiro criamos um agent a
(def a (agent 1))
;; A função watch deve ter quatro parâmetros: uma chave única,
;; a referência ao próprio agent, o valor antigo e o valor novo.
(defn a-watch [key ref old new]
(println "Watching" key ref old new))
;; vamos criar uma função que dobra o valor recebido e informa
;; quando está sendo executada
;; aqui eu crio um agent com o valor 1 e chamo de x
(def y (agent 1))
;; nossa função vai levar dez segundos a cada processamento
(defn triple [c]
(println "valor inicial:" c)
(Thread/sleep 10000)
(println "terminei")
(* c 3))
@pbalduino
pbalduino / macro-reader.clj
Created January 28, 2015 20:20
User defined macro reader
(dispatch-reader-macro \X xml-reader)
#X"
<compras>
<item>
<batata>
<preco>1.23</preco>
</batata>
</item>
</compras>"
@pbalduino
pbalduino / gen.clj
Last active August 29, 2015 14:13
Criando uma classe Java com Clojure
;; criei um arquivo chamado gen.clj
(ns capitulo08.gen
(:gen-class
:name "capitulo08.Uia"
:methods [[hello [] void]]))
(defn -hello [this]
(println "Olá!"))
;; agora no REPL:
@pbalduino
pbalduino / localstorage.js
Last active August 29, 2015 14:10
Small sample of HTML5 Local Storage
if(localStorage["demo.isRunning"] !== "true") {
alert("Let's write something on the local storage");
localStorage["demo.isRunning"] = true;
localStorage["demo.value"] = "It works!";
window.location.reload();
} else {
alert("Value of stored value is '" + localStorage["demo.value"] + "'");
@pbalduino
pbalduino / media4.cs
Created November 12, 2014 12:07
Cálculo de média usando IFs
private void btnCalcular_Click(object sender, EventArgs e)
{
// maior
if (txtNota0.Value > txtNota1.Value && txtNota0.Value > txtNota2.Value && txtNota0.Value > txtNota3.Value)
{
lblMaior.Text = txtNota0.Value.ToString();
}
else if (txtNota1.Value > txtNota0.Value && txtNota1.Value > txtNota2.Value && txtNota1.Value > txtNota3.Value)
{
lblMaior.Text = txtNota1.Value.ToString();