Skip to content

Instantly share code, notes, and snippets.

View lnds's full-sized avatar

Eduardo Díaz lnds

View GitHub Profile
@lnds
lnds / parfor.swift
Last active March 31, 2016 14:35
Consulta en paralelo en swift
var reports = [ApiResult?]()
for i in 1...cities.count {
reports.append(nil)
}
let globalQueue = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)
dispatch_apply(cities.count, globalQueue) {
i in
let index = Int(i)+2 // cities start from 2
let city = cities[index]
let rep = callApi(city, apiKey:apiKey)
def parFetch(cities:List[String]) : Unit = {
val reports = (cities.par map (city => apiCall(city))).toList
printReports(reports)
}
@lnds
lnds / pweather.clj
Created March 31, 2016 15:03
pmap en clojure
((print-weather (sort-reports (pmap weather-api r))))
@lnds
lnds / weather.rs
Created March 31, 2016 15:28
channels in rust
let (tx, rx) = mpsc::channel();
for city in cities {
let tx = tx.clone();
let city = city.clone();
thread::spawn(move || {
let report = api_call(&city, &api_key);
tx.send(report).unwrap();
});
}
let mut reports : Vec<ApiResult> = Vec::with_capacity(cities.len());
@lnds
lnds / weather.go
Created March 31, 2016 15:31
Canales en go
ch := make(chan WeatherReport)
for _, city := range cities {
go fetch(city, ch) // fetch call api and put response in ch
}
for range cities {
rep := <- ch
reports = append(reports, rep)
}
@lnds
lnds / bug.kt
Created May 22, 2017 19:01
A java interop bug in Kotlin
/*
** In java remove() is overloaded
** for Class ArrayList<E>
** we have:
**
** public E remove(int index)
** Removes the element at the specified position in this list.
**
** public boolean remove(Object o)
** Removes the first occurrence of the specified element from this list
// Sha512 in Rust
#[macro_use]
extern crate itertools;
use sha2::{Digest, Sha512};
fn main() {
let target = Sha512::new().chain(b"help").result();
let alpha = "abcdefghijklmnopqrstuvwxyz";
let col = iproduct!(alpha.chars(), alpha.chars(), alpha.chars(), alpha.chars())
.map(|(a, b, c, d)| format!("{}{}{}{}", a, b, c, d))